Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Getting wrong name in mouse hover property in Qt
Forum Updated to NodeBB v4.3 + New Features

Getting wrong name in mouse hover property in Qt

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 3 Posters 862 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    tushu
    wrote on last edited by tushu
    #1

    I am having QGraphicsScene which contains many digital gates ( AND,OR,NOR etc ) which are made up of Arc, Polyline, Circle, Straight line etc.
    I am implementing a feature like, when I hover mouse over input of Gates, it's name should be seen on tool tip

    Gate.PNG

    In above screen shot, there is a gate (i2) which has 2 inputs ( top input line is a[2] and bottom input line is a[3] )

    When I hover inside Red line, I get a[3] as hovering name on cursor tool tip.
    And when I hover outside Red line and inside of Blue line, I get a[2] as hovering name on cursor tool tip.

    But I am expecting, hovering info will be visible only when I hover on input lines of gates. 
    Why am I getting input line names when I hover around input lines ? And moreover, above hovering info is also wrong. 
    

    Here is my code :

    bool myViewer::eventFilter(QObject* watched, QEvent* event)
    {
      case QEvent::MouseMove:
       {
          QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event);
          QPointF mousePoint = _view->mapToScene(mouseEvent->pos());
          QGraphicsItem* SelectedItem = scene->itemAt(mousePoint, QTransform());
          if(SelectedItem)
          {
              myPoly* item = qgraphicsitem_cast<myPoly*>(SelectedItem);
              if(item)
              {
                  item->setToolTip(item->GetPolyLineName()); // GetPolyLineName gives name as a[2] or a[3]
              }
          }
       }
        break;
     }
    

    myPoly.h

    class myPoly : public QGraphicsPathItem {    
          public:
               QString &GetPolyLineName();  
       }
    
    1 Reply Last reply
    0
    • M Offline
      M Offline
      mchinand
      wrote on last edited by mchinand
      #2

      You probably want to implement a custom shape() method for your myPoly QGraphicsItems since the default implementation is just the boundingRect(). Your shape() method should only include the path of the lines of your objects plus maybe some additional thickness. Also, why aren't you just setting the toolTip of your items immediately after you create them (or in the constructor) rather than in this event filter?

      T 1 Reply Last reply
      2
      • M mchinand

        You probably want to implement a custom shape() method for your myPoly QGraphicsItems since the default implementation is just the boundingRect(). Your shape() method should only include the path of the lines of your objects plus maybe some additional thickness. Also, why aren't you just setting the toolTip of your items immediately after you create them (or in the constructor) rather than in this event filter?

        T Offline
        T Offline
        tushu
        wrote on last edited by
        #3

        @mchinand
        You are right. I had set tool tip when I create poly line. But there also I get this problem. So I thought I will tackle this problem using Mouse over event. But I could not.
        Can you throw some more light on how to use shape() method here ?

        M 1 Reply Last reply
        0
        • T tushu

          @mchinand
          You are right. I had set tool tip when I create poly line. But there also I get this problem. So I thought I will tackle this problem using Mouse over event. But I could not.
          Can you throw some more light on how to use shape() method here ?

          M Offline
          M Offline
          mchinand
          wrote on last edited by
          #4

          You really only have to create a custom shape() method for your myPoly class that returns a QPainterPath that corresponds to the lines of the item. As I mentioned above, the default implementation is just the boundingRect() which in your case includes a large area around your lines (at least in your example, for a single straight line, it would be sufficient). The tooltip should then be shown only when you hover over the lines of your myPoly items. From its description, "The shape is used for many things, including collision detection, hit tests, and for the QGraphicsScene::items() functions." https://doc.qt.io/qt-6/qgraphicsitem.html#shape

          T 1 Reply Last reply
          0
          • M mchinand

            You really only have to create a custom shape() method for your myPoly class that returns a QPainterPath that corresponds to the lines of the item. As I mentioned above, the default implementation is just the boundingRect() which in your case includes a large area around your lines (at least in your example, for a single straight line, it would be sufficient). The tooltip should then be shown only when you hover over the lines of your myPoly items. From its description, "The shape is used for many things, including collision detection, hit tests, and for the QGraphicsScene::items() functions." https://doc.qt.io/qt-6/qgraphicsitem.html#shape

            T Offline
            T Offline
            tushu
            wrote on last edited by
            #5

            @mchinand
            I tried this way :

            ........
            .......
             myPoly* _poly = new myPoly();
             poly->DrawPolyline(co -ordinates);    
            poly->SetPolyName(poly line name );   
            poly->shape();
            poly->setToolTip(poly line name);
            scene->addItem(static_cast<QGraphicsPathItem*>(poly)); // adding poly line in scene
            

            myPoly.cpp

            QPainterPath myPoly::shape() const
            {
                //QRectF rect(start_p, end_p).normalized();
                QRectF rect;
                // increase the rect beyond the width of the line
                rect.adjust(-2, -2, 2, 2);
            
                QPainterPath path;
                path.addRect(rect);
            
                return path;    // return the item's defined shape
            }
            

            But above way is not working.
            Where am I wrong ?

            mrjjM 1 Reply Last reply
            0
            • T tushu

              @mchinand
              I tried this way :

              ........
              .......
               myPoly* _poly = new myPoly();
               poly->DrawPolyline(co -ordinates);    
              poly->SetPolyName(poly line name );   
              poly->shape();
              poly->setToolTip(poly line name);
              scene->addItem(static_cast<QGraphicsPathItem*>(poly)); // adding poly line in scene
              

              myPoly.cpp

              QPainterPath myPoly::shape() const
              {
                  //QRectF rect(start_p, end_p).normalized();
                  QRectF rect;
                  // increase the rect beyond the width of the line
                  rect.adjust(-2, -2, 2, 2);
              
                  QPainterPath path;
                  path.addRect(rect);
              
                  return path;    // return the item's defined shape
              }
              

              But above way is not working.
              Where am I wrong ?

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by mrjj
              #6

              hi
              is mypoly a qgraphicspathitem ?

              maybe you can use to return a QPainterPath that is just the lines
              https://doc.qt.io/qt-6/qgraphicspathitem.html#path

              Else you must use the points in your poly to construct a matching shape in a QPainterPath

              Currently you take a empty rect and offset it by 2 but it wont be where the actual lines are and most like not trigger a hit.

              T 2 Replies Last reply
              0
              • mrjjM mrjj

                hi
                is mypoly a qgraphicspathitem ?

                maybe you can use to return a QPainterPath that is just the lines
                https://doc.qt.io/qt-6/qgraphicspathitem.html#path

                Else you must use the points in your poly to construct a matching shape in a QPainterPath

                Currently you take a empty rect and offset it by 2 but it wont be where the actual lines are and most like not trigger a hit.

                T Offline
                T Offline
                tushu
                wrote on last edited by
                #7

                @mrjj
                You are right. myPoly is a QGraphicsPathItem.

                class myPoly : public QGraphicsPathItem
                

                You mentioned that , I must use the points in my poly to construct a matching shape.
                I did not get , how to do this.
                In my shape() code there is a commented line :

                //QRectF rect(start_p, end_p).normalized();
                

                Is this line doing the same as you are expecting ?
                But what is start_p and end_p , I am not understanding so I commented it.
                Can you help me ?

                1 Reply Last reply
                0
                • mrjjM mrjj

                  hi
                  is mypoly a qgraphicspathitem ?

                  maybe you can use to return a QPainterPath that is just the lines
                  https://doc.qt.io/qt-6/qgraphicspathitem.html#path

                  Else you must use the points in your poly to construct a matching shape in a QPainterPath

                  Currently you take a empty rect and offset it by 2 but it wont be where the actual lines are and most like not trigger a hit.

                  T Offline
                  T Offline
                  tushu
                  wrote on last edited by
                  #8

                  @mrjj

                  I solved the issue.

                  QRectF rect(start_p, end_p).normalized();
                  

                  Here start_p and end_p were start point and end point of line.
                  Thanks for your comment. Because of it, I understood what mistake I made.
                  Thank you again.

                  1 Reply Last reply
                  1

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved