Reputation: 380
I have a QGraphicsView
inside the MainWindow
that has implemented a QGraphicScene
. I need to pop up a widget when I right click the mouse on a certain portion of the QGraphicScene
. The parent of the widget needs to be MainWindow
.
My problem is that I need to verify the validity of the portion on witch I clicked inside a mousePressEvent
in QGraphicScene
and to pop up the widget at the exact same location but the coordinates of the QGraphicScene
and MainWindow
are obvious not the same. For that I use a custom signal that trigger a slot inside MainWindow
and get the coordinates from the mousePressEvent
of the MainWindow
. The problem is that the mouseEvent
from the QGraphicsScene
is triggered before the mouseEvent
from MainWindow
. This makes perfect sense and works if I right click twice but I need it to work from the first right click.
I can not implement a filter or change the focus because I have tons of events in the application.
QGraphicScene:
void CGraphicScene :: mousePressEvent(QGraphicsSceneMouseEvent *event)
{
if(event -> button() == Qt::RightButton)
{
//test stuff
emit signalChangeContextualMenuPosition();
m_contextualMenu -> show();
}
}
MainWindow:
CGraphicScene *scene;
CContextualMenu *m_contextualMenu;
m_contextualMenu = new CContextualMenu(this);
m_contextualMenu ->close();
scene = new CGraphicScene(m_contextualMenu);
ui->gvInterface -> setScene(scene);
connect(scene, SIGNAL(signalChangeContextualMenuPosition()), this, SLOT(openPopUp()));
void MainWindow :: openPopUp()
{
m_contextualMenu ->move(m_xCoordPopMenu, m_yCoordPopMenu);
}
void MainWindow :: mousePressEvent(QMouseEvent *event)
{
if(event -> button() == Qt::RightButton)
{
m_xCoordPopMenu = event -> x();
m_yCoordPopMenu = event -> y();
}
}
Upvotes: 0
Views: 495
Reputation: 2210
Use the QGraphicsView::mapFromScene()
to map scene coordinates to the view widget coordinates and then QWidget::mapToParent()
to map the coordinates to it's parent widget, which is probably your main window. You can also find useful the method QWidget::mapTo()
.
Upvotes: 2