Getting wrong name in mouse hover property in Qt
-
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 tipIn 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(); }
-
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? -
@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 ? -
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
-
@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 ? -
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#pathElse 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.
-
@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 ?