1

I managed to create a context menu that gets activated after a right click on each item of a QTreeWidget tree:

contextMenu = new QMenu(ui->treeWidget);
ui->treeWidget->setContextMenuPolicy(Qt::ActionsContextMenu);

addElement = new QAction("Add Element",contextMenu);
deleteElement = new QAction("Delete Element",contextMenu);

ui->treeWidget->addAction(addElement);
ui->treeWidget->addAction(deleteElement);

connect(addElement,    SIGNAL(triggered()), this, SLOT(addElementHandler()));
connect(deleteElement, SIGNAL(triggered()), this, SLOT(deleteElementHandler()));

My intention is to add new items under another in the tree or delete them by right clicking on a specific item using this context menu.

However I'm not sure how to realize from the handlers on exactly which item of the tree the right click was made.

Could you please give me a clue?

Thanks in advance!

4

1 に答える 1

2

If you are not going to change the TreeWidget selection behavior or set the current item by your own - you can use just the native behavior. While context menu requesting the tree selects the item, on which the right click was performed and that is the currentItem. So in addElementHandler slot the currentItem() will give you exact item you want.

于 2012-02-27T07:43:13.727 に答える