PDA

View Full Version : Context Menu not working



waynew
10th January 2010, 03:01
Right click on the view, nothing happens, here's the code:



view = new QTableView();
connect(view, SIGNAL(customContextMenuRequested ( const QPoint &)), this, SLOT(viewMenu(const QPoint &)));

void MainWindow::viewMenu(const QPoint & pos)
{
QMenu *viewMenu = new QMenu("Edit Log", this);
viewMenu->setContextMenuPolicy(Qt::CustomContextMenu);
viewMenu->addAction("Edit");
viewMenu->popup(pos);
}


viewMenu is declared as a slot.

so, what have I done wrong?

rainspider
10th January 2010, 04:48
setContextMenuPolicy(Qt::CustomContextMenu);
This function must be called before emitting signal (customContextMenuRequested).
eg:

view = new QTableView();
view->setContextMenuPolicy(Qt::CustomContextMenu);
connect(view, SIGNAL(customContextMenuRequested ( const QPoint &)), this, SLOT(viewMenu(const QPoint &)));


and the pos must be translated form widget coordinates to screen coordinates.more information in Qt document,Please look for
customContextMenuRequested.