for using context menu you should do next steps:
1. set for your widget next property
...
widget->setContextMenuPolicy(Qt::CustomContextMenu);
...
...
widget->setContextMenuPolicy(Qt::CustomContextMenu);
...
To copy to clipboard, switch view to plain text mode
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 &)));
...
...
connect(widget SIGNAL(customContextMenuRequested(const QPoint &)),
SLOT(showContextMenuForWidget(const QPoint &)));
...
To copy to clipboard, switch view to plain text mode
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));
}
void MainWindow::showContextMenuForWidget(const QPoint &pos)
{
QMenu contextMenu(tr("Context menu"), this);
contextMenu.addAction(new QAction(tr("Hello"), this));
contextMenu.exec(mapToGlobal(pos));
}
To copy to clipboard, switch view to plain text mode
Bookmarks