我有一个QGraphicsView如何显示一个QGraphicsScene,其中包含一个QGraphicsItem。我的项目实现了触发QToolTip的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");
}第三,给出的解决方案here也能工作。不过,我不想显示坐标。工具提示的内容是静态的。
通过创建两个工具提示或第二次更新提示的位置,我如何使这个工作不发生所描述的闪烁?
发布于 2017-06-24 13:56:19
创建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();https://stackoverflow.com/questions/44736503
复制相似问题