PDA

View Full Version : Can not invoke contextMenuEvent in QGraphicsView



chiaminhsu
27th April 2013, 13:29
Hi experts,

I code a TabletCanvas inherited from QGraphicsView, and overwrite contextMenuEvent() to bring the item to front in the scene.

However, right click on TabletCanvas cant invokes contextMenuEvent(),
even I disable all mouse events.

Can you tell me what's wrong with it ? Thank your review and answer.




class TabletCanvas : public QGraphicsView {
..

TabletCanvas()
{
zorderAct= new QAction(tr("bring to front"), this);
connect(zorderAct, SIGNAL(triggered()), this, SLOT(itemZorder()));
addAction(zorderAct);
}

virtual void contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
{
QMenu menu(this);
menu.addAction(zorderAct);
menu.exec(event->screenPos());
}

//virtual void mouseMoveEvent(QMouseEvent *event);
//virtual void mousePressEvent(QMouseEvent *event);
//virtual void mouseReleaseEvent(QMouseEvent *event);
//virtual void mouseDoubleClickEvent(QMouseEvent *event);
};

d_stranz
28th April 2013, 18:10
Commenting out mouse event handlers does not disable them. It simply means that they are processed by the QGraphicsView event handlers instead. Why don't you read the documentation for QWidget::contextMenuEvent() and maybe you will discover why you are not seeing context menu events in your widget.

chiaminhsu
29th April 2013, 03:22
Hi d_stranz,

Thank your suggestion.

I review the document you said.
Then I set the context menu policy as Qt::ActionsContextMenu
and DO NOT overwrite the contextMenuEvent().

It works ;)



TabletCanvas::TabletCanvas()
{
QAction* zorderAct= new QAction(tr("bring to front"), this);
connect(zorderAct, SIGNAL(triggered()), this, SLOT(itemZorder()));
addAction(zorderAct);
setContexMenuPolicy(Qt::ActionsContextMenu);
}


Although I still not understand why Qt::DeaultContextMenu and overwrite contextMenuEvent can not work.
It should be another solution in the document :confused:

d_stranz
29th April 2013, 21:55
Although I still not understand why Qt:: DefaultContextMenu and overwrite contextMenuEvent can not work.
It should be another solution in the document

Because in your original code, you did not overwrite contextMenuEvent( QContextMenuEvent * ). You created a method called void contextMenuEvent( QGraphicsSceneContextMenuEvent * ), which is not the same as void contextMenuEvent( QContextMenuEvent * ) and so it does not match the signature required for the event handler. Change the argument to your event handler definition, and try that. Inside the event handler, you can qobject_cast<QGraphicsSceneContextMenuEvent *>( event ) to get the QGraphicsSceneContextMenuEvent pointer:



// TabletCanvas.h

class TabletCanvas : public QGraphicsView
{
//...

protected:
void contextMenuEvent( QContextMenuEvent * event );
// ...
};

// TabletCanvas.cpp

TabletCanvas::TabletCanvas( QWidget * parent )
: QGraphicsView( parent )
{
setContextMenuPolicy( Qt:DefaultContextMenu );
// ...
}

void TabletCanvas::contextMenuEvent( QContextMenuEvent * event )
{
QGraphicsSceneContextMenuEvent * sEvent = qobject_cast< QGraphicsSceneContextMenuEvent * >( event );
if ( sEvent )
{
// Have fun
}
}