Context menu position problem
-
Hello!
I had installed a contextMenu in a QGroupBox. This is the slot that receives the signal:
void MainWindow::showGroupsMenu(const QPoint &point) { QGroupBox* group = qobject_cast<QGroupBox*>(sender()); qDebug() << "Group: " << group->title(); QMenu contextMenu(tr("Context menu"), group); QAction action1("Add camera", this); // connect(&action1, SIGNAL(triggered()), this, SLOT(removeDataPoint())); contextMenu.addAction(&action1); qDebug() << "Pos: " << point; contextMenu.exec(mapToGlobal(point)); }
The problem here is the execution position. It always puts the context menu in the topLeft side of the window and my QGroupBox is in the middle of the screen, for example. How can I fix it??
Thank you very much!!
-
Hello!
I had installed a contextMenu in a QGroupBox. This is the slot that receives the signal:
void MainWindow::showGroupsMenu(const QPoint &point) { QGroupBox* group = qobject_cast<QGroupBox*>(sender()); qDebug() << "Group: " << group->title(); QMenu contextMenu(tr("Context menu"), group); QAction action1("Add camera", this); // connect(&action1, SIGNAL(triggered()), this, SLOT(removeDataPoint())); contextMenu.addAction(&action1); qDebug() << "Pos: " << point; contextMenu.exec(mapToGlobal(point)); }
The problem here is the execution position. It always puts the context menu in the topLeft side of the window and my QGroupBox is in the middle of the screen, for example. How can I fix it??
Thank you very much!!
-
@ivanicy
Change themapToGlobal(point)
to the desired position if it's not already correct/what you want? -
@JonB point contains the relative position in the QGroupBox and I want to show the contextMenu in the absolute position
@ivanicy
You don't say whether you are claiming thepoint
received in the function is correct? Are you saying it;s always(0, 0)
, or what?Anyway, if you need to add the
QGroupBox
coords to it, maybe you need something likegroup->mapToGlobal()
, e.g. doesgroup->mapToGlobal(QPoint(0,0))
at least put it at top left of group box? -
@ivanicy
You don't say whether you are claiming thepoint
received in the function is correct? Are you saying it;s always(0, 0)
, or what?Anyway, if you need to add the
QGroupBox
coords to it, maybe you need something likegroup->mapToGlobal()
, e.g. doesgroup->mapToGlobal(QPoint(0,0))
at least put it at top left of group box?@JonB Let me try to explain it with a picture.
Now, with my code, when I right click hover a QGroupBox, the context menu appears in the topleft corner and there is a displacement in function of the relative coordinates of the QGroupBox (they are between 0 and QGroupBox size). I need to put the context menu in the right click place.
I hope I explain better this time.
Thank you very much for your time!!!
-
@JonB Let me try to explain it with a picture.
Now, with my code, when I right click hover a QGroupBox, the context menu appears in the topleft corner and there is a displacement in function of the relative coordinates of the QGroupBox (they are between 0 and QGroupBox size). I need to put the context menu in the right click place.
I hope I explain better this time.
Thank you very much for your time!!!
-
@ivanicy
Did you trygroup->mapToGlobal(QPoint(0,0))
to see if that places it at top left of group box? -
@JonB This shows the context menu in the topleft corner of the screen, not on the QGroupBox
@ivanicy
May I verify you are tryinggroup->mapToGlobal(QPoint(0, 0))
and not justmapToGlobal(QPoint(0, 0))
? Because https://doc.qt.io/qt-5/qwidget.html#mapToGlobal statesTranslates the widget coordinate pos to global screen coordinates. For example,
mapToGlobal(QPoint(0,0))
would give the global coordinates of the top-left pixel of the widget. -
@ivanicy
May I verify you are tryinggroup->mapToGlobal(QPoint(0, 0))
and not justmapToGlobal(QPoint(0, 0))
? Because https://doc.qt.io/qt-5/qwidget.html#mapToGlobal statesTranslates the widget coordinate pos to global screen coordinates. For example,
mapToGlobal(QPoint(0,0))
would give the global coordinates of the top-left pixel of the widget. -
@JonB This was the solution!!
menuContext->exec(group->mapToGlobal(point));
works!!
Thank you very much!
@ivanicy
But that's what I said you needed, i.e.group->mapToGlobal(QPoint(0, 0))
, to make it relative togroup
, as per the docs! You should not say[Me] Did you try
group->mapToGlobal(QPoint(0,0))
to see if that places it at top left of group box?
[You] @JonB This shows the context menu in the topleft corner of the screen, not on the QGroupBoxif you did not copy & paste what I suggested, I suspected you were only trying
mapToGlobal(QPoint(0, 0))
, it makes it very hard to help people if they tell you they tried what was suggested but in fact did not! -
@ivanicy
But that's what I said you needed, i.e.group->mapToGlobal(QPoint(0, 0))
, to make it relative togroup
, as per the docs! You should not say[Me] Did you try
group->mapToGlobal(QPoint(0,0))
to see if that places it at top left of group box?
[You] @JonB This shows the context menu in the topleft corner of the screen, not on the QGroupBoxif you did not copy & paste what I suggested, I suspected you were only trying
mapToGlobal(QPoint(0, 0))
, it makes it very hard to help people if they tell you they tried what was suggested but in fact did not! -
@JonB Yes, sorry friend, you are right. I didn't read the "group" word the first time. Sorry again