PDA

View Full Version : QListWidget context menu using Designer



palsa
31st August 2008, 14:39
I would like to create a context menu for a QListWidget, while continuing to use Designer for creating the user interface. At the moment it looks like I need to subclass QListWidget and implement void QWidget::contextMenuEvent ( QContextMenuEvent * event ).

The problem is, does this mean I can no longer use Designer generated code and have to implement the UI handcoding?

spirit
31st August 2008, 14:57
for using context menu you should do next steps:
1. set for your widget next property


...
widget->setContextMenuPolicy(Qt::CustomContextMenu);
...

2. then create slot which will create and show context menu and connect it with signal customContextMenuRequested(const QPoint &)


...
connect(widget SIGNAL(customContextMenuRequested(const QPoint &)),
SLOT(showContextMenuForWidget(const QPoint &)));
...

3. example of showContextMenuForWidget slot


void MainWindow::showContextMenuForWidget(const QPoint &pos)
{
QMenu contextMenu(tr("Context menu"), this);
contextMenu.addAction(new QAction(tr("Hello"), this));
contextMenu.exec(mapToGlobal(pos));
}