I would like to popup a menu, when user clicks on an object in QTreeWidgetItem. I though about catching signal contextMenuRequested from QWidget and then retrieving index from the view using itemAt. But this doesn't seem very pretty. Is there any easier way to be able to call a menu on an item inside a view?
A:
I'm using something like this:
self.widget_layers.setContextMenuPolicy(Qt.ActionsContextMenu)
removeLayerAction = QAction("Remove selected layer", self)
self.connect(removeLayerAction, SIGNAL('triggered()'), self.layers_widget_controller.remove_selected_layer)
and check which item triggered the signal by:
selected_item = self.main_window.widget_layers.selectedItems()[0]
piobyz
2009-11-20 20:10:52
+3
A:
Write your own custom ItemDelegate and handle the click event in QAbstractItemDelegate::editorEvent
.
You can retreive the data in the cell from the QModelIndex.
In C++ it would look like this:
class ItemDelegate: public QItemDelegate
{
public:
ItemDelegate(ContextMenuHandler *const contextMenu, QObject *const parent )
: QItemDelegate(parent)
, m_contexMenu(contextMenu)
{
}
bool editorEvent(
QEvent * event,
QAbstractItemModel * model,
const QStyleOptionViewItem & option,
const QModelIndex & index )
{
if((event->type()==QEvent::MouseButtonPress) && index.isValid())
{
QMouseEvent *const mouseEvent = qobject_cast<QMouseEvent>(event);
if(mouseEvent && (mouseEvent->button()==Qt::RightButton))
{
return m_contexMenu->showContextMenu(mouseEvent->pos(), index);
}
}
}
ContextMenuHandler *const m_contextMenu;
};
treeWidget->setItemDelegate(new ItemDelegate(contextMenuHandler,treeWidget));
TimW
2009-11-21 11:50:48
Thanks, this seems quite pretty :-) Event handled directly in the item.
gruszczy
2009-11-23 09:59:10