at the first you need to set context menu policy for QTreeWidget
...
m_tree->setContextMenuPolicy(Qt::CustomContextMenu);
...
...
m_tree->setContextMenuPolicy(Qt::CustomContextMenu);
...
To copy to clipboard, switch view to plain text mode
then connect customContextMenuRequested with the slot which will create a context menu
...
connect(m_tree,
SIGNAL(customContextMenuRequested
(const QPoint &)),
SLOT(showContextMenu
(const QPoint &)));
...
...
connect(m_tree, SIGNAL(customContextMenuRequested(const QPoint &)), SLOT(showContextMenu(const QPoint &)));
...
To copy to clipboard, switch view to plain text mode
and last you need to create that slot
void MyWidget
::showContextMenu(const QPoint &pos
) {
if (!item)
return;
QMenu menu
(tr
("Context menu"),
this);
//add needed actions
menu.exec(m_tree->viewport()->mapToGlobal(pos));
}
void MyWidget::showContextMenu(const QPoint &pos)
{
QTreeWidgetItem *item = m_tree->itemAt(pos);
if (!item)
return;
QMenu menu(tr("Context menu"), this);
//add needed actions
menu.exec(m_tree->viewport()->mapToGlobal(pos));
}
To copy to clipboard, switch view to plain text mode
Bookmarks