PDA

View Full Version : QTreeWidgetItem have contextMenu ?



yunpeng880
20th February 2009, 06:56
i need help.
i want QTreeWidgetItem to show contextMenu on it be click, can somebody tell me how to make it?
thanks

aamer4yu
20th February 2009, 07:03
You will need to override treewidget context menu function.
In this check if any item is under it and show menu based on the item.
You can also ContextMenu ready in items, and from treewidget contextmenu function, get this menu and show it.

or may be you can call some treewidget item function from treewidget contextmenu event.

Hope something works for you :)

spirit
20th February 2009, 07:05
at the first you need to set context menu policy for QTreeWidget


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

then connect customContextMenuRequested with the slot which will create a context menu


...
connect(m_tree, SIGNAL(customContextMenuRequested(const QPoint &)), SLOT(showContextMenu(const QPoint &)));
...

and last you need to create that slot


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));
}

yunpeng880
27th February 2009, 05:59
thanks everyone,i have work out the question follow your

riarioriu3
24th September 2012, 20:17
Thanks for such a nice reply . But here i am facing some problem like the action is being performed when we are clicking on any part of the UI .
My requirement is whenever the user click on that menuitem , that time only the action shall be performed .

ChrisW67
25th September 2012, 03:56
So, you resurrected a 3.5 year old thread to tell us that something in a program you don't describe does something you don't expect when you click somewhere. Don't be surprised when no magic bullet answer is forthcoming.