当鼠标悬停在Qt中的QGraphicsItem上时,如何显示除鼠标指针之外的一些文本?

发布于 2025-01-16 10:10:09 字数 1729 浏览 3 评论 0原文

我有一个 QGraphicsView,其中包含许多矩形和折线。一旦我通过鼠标单击在视图上单击它们,我想打印每个对象名称(每个矩形、折线都有名称)。我这样做了并且工作得很好。
但现在我想通过鼠标悬停来做到这一点。这意味着,如果我将鼠标悬停在特定对象上,除了光标之外,它还应该显示其名称。
我尝试过,但

 I am not understanding, on hovering, how that particular object should get selected ?       
 And how to print its name besides cursor point ?        

我尝试了这种方式:

bool myClass::eventFilter(QObject *watched, QEvent *event)
{
    bool filterEvent = false;
    switch(event->type())
    {
        case QEvent::MouseButtonPress:
        {....}
        case QEvent::MouseButtonRelease:
        {...}
        case QEvent::Enter:
        {
           QMouseEvent * mouseEvent = static_cast<QMouseEvent *>(event);
           QPointF po = _view->mapToScene(mouseEvent->pos());
           FindNamesOverHover(po);
        }
          return true;

       default:
           break;
    }
    return filterEvent;
}       
      
void myClass::FindNamesOverHover(QPointF p)
{
    QGraphicsRectItem* rItem = qgraphicsitem_cast<QGraphicsRectItem*>(_scene->itemAt(p,QTransform()));
    if(rItem)
    {
      // some logic 
         qDebug()<< "Instance name is " << i->Name();
    }
    
}

    

类 myClass 的构造函数

myClass::myClass(QWidget* parent) :
    QDockWidget(parent)
{
    scene = new QGraphicsScene(this);
    view = new QGraphicsView(this);
    view->setScene(_scene);     
    view->viewport()->installEventFilter(this);
}     

现在仅适用于上面的代码,

When I select object through mouse click and then again hover over it.
Then it prints its name on console.  

但我只想悬停(不通过鼠标单击进行选择)将显示除光标点之外的其名称。
有人可以帮助我吗?

I have a QGraphicsView, which contains many rectangle and polylines. I wanted to print every object names ( every rectangle, polylines have names) once I clicked them on view through mouse click. I did that and worked perfectly.
But now I want to do that over mouse hovering. It means, if I hover a mouse over particular object, it should show its name besides cursor.
I tried that, but

 I am not understanding, on hovering, how that particular object should get selected ?       
 And how to print its name besides cursor point ?        

I tried this way:

bool myClass::eventFilter(QObject *watched, QEvent *event)
{
    bool filterEvent = false;
    switch(event->type())
    {
        case QEvent::MouseButtonPress:
        {....}
        case QEvent::MouseButtonRelease:
        {...}
        case QEvent::Enter:
        {
           QMouseEvent * mouseEvent = static_cast<QMouseEvent *>(event);
           QPointF po = _view->mapToScene(mouseEvent->pos());
           FindNamesOverHover(po);
        }
          return true;

       default:
           break;
    }
    return filterEvent;
}       
      
void myClass::FindNamesOverHover(QPointF p)
{
    QGraphicsRectItem* rItem = qgraphicsitem_cast<QGraphicsRectItem*>(_scene->itemAt(p,QTransform()));
    if(rItem)
    {
      // some logic 
         qDebug()<< "Instance name is " << i->Name();
    }
    
}

    

Constructor of class myClass

myClass::myClass(QWidget* parent) :
    QDockWidget(parent)
{
    scene = new QGraphicsScene(this);
    view = new QGraphicsView(this);
    view->setScene(_scene);     
    view->viewport()->installEventFilter(this);
}     

Now above code works only,

When I select object through mouse click and then again hover over it.
Then it prints its name on console.  

But I want only hovering ( no selection through mouse click) will show its name besides cursor point.
Can any one help me ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苏别ゝ 2025-01-23 10:10:09

您可以覆盖 QGraphicsScene.mouseMoveEvent 并在其中使用 QGraphicsScene.itemAt 来查询光标下的项目。要显示,您可以使用QGraphicsTextItem,只需将其移动到正确的位置并应用文本即可。

You can override QGraphicsScene.mouseMoveEvent and inside it use QGraphicsScene.itemAt to query for item under cursor. To display you can use QGraphicsTextItem, just move it to correct position and apply text.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文