Chart and Legend
-
Hi,
after i finished learning path "Introduction to Qt Widgets with KDAB" im trying to create own QT Application wit QT Creator.
Now im stuck in an issue trying to show context menu when user make right click on the chart, my goal is to show a context menu when a user has
used mouse right click on a Chart Legend, but im not able to find a chart legend position, or how to check what is under my cursor currently,
for this i used event filter, this part works, but how to catch mouse right click on QLegend, is this possible?This is how im try to connect to QLegend:
connect(m_Chart->legend(), &QLegend::clicked, this, &MyChartView::onLegendClicked);Edit: i saw there is no signal "clicked", but QLegendMarker has one, if my first is not possible to use, can we use for this QLegendMarker's signal "clicked" to catch the right mouse event?
Thanks for any help
-
You have two options:
- Inherit
QLegendand code your own virtualMyLegend::mousePressEvent(QGraphicsSceneMouseEvent *)function; - Use
QLegend::installEventFilterfunction.
You have call it in the window constructor withthisas param.
Then in your window you have implement function (this is not compiled and not tested code):
bool MyWindowWithLegend::eventFilter(QObject* o, QEvent* e) { if(o != myLegendObject) return false; // Event not recognized - propagate it to next widget. QMouseEvent* e2 = dynamic_cast<QMouseEvent*>(e); if(!e2) return false; // Event not recognized - propagate it to next widget. if(e2->button() == Qt::RightButton) { // Your popup code here return true; // Event recognized and used properly - do no propagate it to next widget. } return false; // Event not recognized - propagate it to next widget. } - Inherit
-
You have two options:
- Inherit
QLegendand code your own virtualMyLegend::mousePressEvent(QGraphicsSceneMouseEvent *)function; - Use
QLegend::installEventFilterfunction.
You have call it in the window constructor withthisas param.
Then in your window you have implement function (this is not compiled and not tested code):
bool MyWindowWithLegend::eventFilter(QObject* o, QEvent* e) { if(o != myLegendObject) return false; // Event not recognized - propagate it to next widget. QMouseEvent* e2 = dynamic_cast<QMouseEvent*>(e); if(!e2) return false; // Event not recognized - propagate it to next widget. if(e2->button() == Qt::RightButton) { // Your popup code here return true; // Event recognized and used properly - do no propagate it to next widget. } return false; // Event not recognized - propagate it to next widget. }@Jacek-Marcin-Jaworski
hi, thank you for your answer, i will try to implement tomorrow.Regards
pixel - Inherit
-
The
clicked()signal is usually for the left click. However, for QWidgets you cansetContextMenuPolicy()toQt::CustomContextMenu. Then you should connect tocustomContextMenuRequested(). This signal will even give you the position where to show the context menu. -
P pixel has marked this topic as solved