Hello,

I want to code a programm with a QTreeWidget. In that QTreeWidget I want to add a item with Right-Click and open a contextmenu with the option "add item", "remove item", aso..
I have used a QMainWindow where I set a new Class of QTreeWidget as the central widget.

That works fine, the TreeWidget appears in the MainWindow.

Now I would like to make a contextmenu with right-click in the Treewidget. Here is my code from my QTreeWidget:


Here the code of the cabletreewidget.h:
Qt Code:
  1. #ifndef CABLETREEWIDGET_H
  2. #define CABLETREEWIDGET_H
  3.  
  4. #include <QTreeWidget>
  5.  
  6. class CableTreeWidget : public QTreeWidget
  7. {
  8. Q_OBJECT
  9.  
  10. public:
  11. CableTreeWidget();
  12. ~CableTreeWidget();
  13.  
  14. private:
  15.  
  16. protected slots:
  17. void ShowContextMenu( QPoint& );
  18.  
  19. };
  20.  
  21. #endif // CABLETREEWIDGET_H
To copy to clipboard, switch view to plain text mode 

And here the code of the cpp-File:
Qt Code:
  1. #include "cableTreeWidget.h"
  2. #include <QtCore>
  3. #include <QMenu>
  4.  
  5.  
  6. CableTreeWidget::CableTreeWidget()
  7. {
  8. this->setContextMenuPolicy(Qt::CustomContextMenu);
  9. connect(this, SIGNAL(customContextMenuRequested(const QPoint&)),
  10. this, SLOT(ShowContextMenu(QPoint&)));
  11. }
  12.  
  13. CableTreeWidget::~CableTreeWidget()
  14. {
  15.  
  16. }
  17.  
  18.  
  19. void CableTreeWidget::ShowContextMenu(QPoint &pos)
  20. {
  21.  
  22. QPoint globalPos = this->mapToGlobal( pos );
  23.  
  24. QMenu myMenu;
  25. myMenu.addAction("Menu Item 1");
  26. // ...
  27.  
  28. QAction* selectedItem = myMenu.exec(globalPos);
  29. }
To copy to clipboard, switch view to plain text mode 

I found that code at the internet. There is no more code at this time.

Ok, the compilert works fine, no problems, no warnings. But when I start the programm and right-click on the TreeWidget, unfortunately a contextmenu doesnt appears. Can anyone tell me what's wrong?

Thank you!

Greetings