Results 1 to 4 of 4

Thread: Problems with contextmenu in a QTreeWidget

  1. #1
    Join Date
    Jul 2012
    Posts
    7
    Qt products
    Qt4
    Platforms
    Windows

    Default Problems with contextmenu in a QTreeWidget

    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

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    3
    Thanked 453 Times in 435 Posts
    Wiki edits
    15

    Default Re: Problems with contextmenu in a QTreeWidget

    While making connection the oder and type of the parameters in the signature of the slot and signal should be same. Note that QPoint and const QPoint are considered as different types. This you could have figured out by looking at the warning messages in application output.
    Qt Code:
    1. //cabletreewidget.h
    2. #ifndef CABLETREEWIDGET_H
    3. #define CABLETREEWIDGET_H
    4.  
    5. #include <QTreeWidget>
    6.  
    7. class CableTreeWidget : public QTreeWidget
    8. {
    9. Q_OBJECT
    10.  
    11. public:
    12. CableTreeWidget();
    13. ~CableTreeWidget();
    14.  
    15. private:
    16.  
    17. protected slots:
    18. void ShowContextMenu(const QPoint & ); //<<<<<<<<<<< added const
    19.  
    20. };
    21.  
    22. #endif // CABLETREEWIDGET_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. //cabletreewidget.cpp
    2. #include "cableTreeWidget.h"
    3. #include <QtCore>
    4. #include <QMenu>
    5.  
    6.  
    7. CableTreeWidget::CableTreeWidget()
    8. {
    9. this->setContextMenuPolicy(Qt::CustomContextMenu);
    10. connect(this, SIGNAL(customContextMenuRequested(const QPoint&)),
    11. this, SLOT(ShowContextMenu(const QPoint&))); //<<<<<<<<<<< added const
    12. }
    13.  
    14. CableTreeWidget::~CableTreeWidget()
    15. {
    16.  
    17. }
    18.  
    19.  
    20. void CableTreeWidget::ShowContextMenu(const QPoint &pos) //<<<<<<<<<<< added const
    21. {
    22.  
    23. QPoint globalPos = this->mapToGlobal( pos );
    24.  
    25. QMenu myMenu;
    26. myMenu.addAction("Menu Item 1");
    27. // ...
    28.  
    29. QAction* selectedItem = myMenu.exec(globalPos);
    30. }
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  3. #3
    Join Date
    Jul 2012
    Posts
    7
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problems with contextmenu in a QTreeWidget

    Hello,

    thanks for your reply!

    I tried your solution, but the contextmenu doenst work

    Anyone knows the problem?

  4. #4
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    3
    Thanked 453 Times in 435 Posts
    Wiki edits
    15

    Default Re: Problems with contextmenu in a QTreeWidget

    I tried your solution, but the contextmenu doenst work
    Then you did not understand my reply, please read it again.

    Anyone knows the problem?
    The problem is already solved in the earlier post, you just have to read the reply carefuly.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

Similar Threads

  1. Replies: 0
    Last Post: 5th August 2013, 08:38
  2. QTreeWidget Drag and Drop InternalMove problems
    By dysplaced in forum Qt Programming
    Replies: 0
    Last Post: 29th June 2012, 12:43
  3. QTreeWidget Problems
    By Ryan Riffle in forum Qt Programming
    Replies: 1
    Last Post: 16th January 2011, 21:44
  4. Replies: 3
    Last Post: 22nd March 2010, 14:50
  5. contextmenu with MDI
    By Thoosle in forum Qt Programming
    Replies: 1
    Last Post: 1st December 2006, 08:29

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.