簡體   English   中英

QToolTip應該跟隨在QGraphicsItem上方的鼠標時的怪異行為

[英]Weird behaviour of QToolTip when it should follow the mouse who is over a QGraphicsItem

我有一個QGraphicsView如何顯示一個QGraphicsScene ,其中包含QGraphicsItem 我的項目實現了hoverMoveEvent(...)方法,該方法可以觸發QToolTip

我希望工具提示在該項目上方移動時跟隨鼠標。 但是,僅當我執行以下兩項操作之一時,此方法才有效:

  • 要么創建兩個QToolTip ,其中第一個只是一個虛擬對象,然后立即被第二個覆蓋。
  • 其次,例如通過將rand()放入提示文本中,使提示的內容隨機化。

此實現無法正常工作。 它使工具提示出現,但不跟隨鼠標。 就像它意識到它的內容沒有更改並且不需要任何更新一樣。

void MyCustomItem::hoverMoveEvent(QGraphicsSceneHoverEvent *mouseEvent)
{
    QToolTip::showText(mouseEvent->screenPos(), "Tooltip that follows the mouse");
}

此代碼創建所需的結果。 工具提示跟隨鼠標。 缺點是,由於創建了兩個工具提示,因此您會看到輕微的閃爍。

 void MyCustomItem::hoverMoveEvent(QGraphicsSceneHoverEvent *mouseEvent)
    {
QToolTip::showText(mouseEvent->screenPos(), "This is not really shown and is just here to make the second tooltip follow the mouse.");
        QToolTip::showText(mouseEvent->screenPos(), "Tooltip that follows the mouse");
    }

第三, 此處介紹的解決方案也有效。 但是,我不想顯示坐標。 工具提示的內容是靜態的...

通過創建兩個工具提示或第二次更新提示的位置,如何在沒有上述閃爍的情況下進行這項工作?

創建QTooltip是,一旦您移動鼠標QTooltip消失,為了避免這種行為,您可以使用QLabel並啟用Qt::ToolTip標志。 在您的情況下:

。H

private:
    QLabel *label;

的.cpp

MyCustomItem::MyCustomItem(QGraphicsItem * parent):QGraphicsItem(parent)
{
    label = new QLabel;
    label->setWindowFlag(Qt::ToolTip);
    [...]
}

在要顯示消息的位置之后,如果要在hoverMoveEvent ,則應放置以下代碼。

label->move(event->screenPos());
label->setText("Tooltip that follows the mouse");
if(label->isHidden())
    label->show();

要隱藏它,您必須使用:

label->hide();

看到這個: 如何使QToolTip消息持久化?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM