Results 1 to 5 of 5

Thread: Custom QTreeWidgetItem context menu?

  1. #1
    Join Date
    Sep 2007
    Posts
    14
    Thanks
    5
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Custom QTreeWidgetItem context menu?

    I am trying to create QTreeWidgetItems with custom context menus. I would like to have QTreeWidget and QTreeWidgetItem derived classes like the following example:

    Qt Code:
    1. class QCustomTreeWidget;
    2.  
    3. class QCustomTreeItem : public QTreeWidgetItem
    4. {
    5. public:
    6. QCustomTreeItem(QCustomTreeWidget *view);
    7. QCustomTreeItem(QCustomTreeItem *parent);
    8. virtual ~QCustomTreeItem();
    9.  
    10. virtual void onContextMenu(QContextMenuEvent *event) = 0;
    11. };
    12.  
    13. class QMyPluginItem : public QCustomTreeItem
    14. {
    15. public:
    16. QMyPluginItem(QCustomTreeWidget *view);
    17. QMyPluginItem(QCustomTreeItem *parent);
    18. virtual ~QMyPluginItem();
    19.  
    20. virtual void onContextMenu(QContextMenuEvent *event)
    21. {
    22. // create the context menu
    23. QMenu* ContextMenu = new QMenu();
    24.  
    25. QAction *actCustom = new QAction("Custom Action", ContextMenu);
    26. ContextMenu->addAction(actCustom);
    27. connect(actCustom, SIGNAL(triggered(void)), this, SLOT(onCustomAction(void)));
    28.  
    29. ContextMenu->exec( event->globalPos() );
    30.  
    31. delete ContextMenu;
    32. }
    33.  
    34. private slots:
    35. void onCustomAction();
    36. };
    37.  
    38.  
    39. class QCustomTreeWidget : public QTreeWidget
    40. {
    41. Q_OBJECT
    42. public:
    43. QCustomTreeWidget(QWidget *parent = 0);
    44. virtual ~QCustomTreeWidget();
    45.  
    46. protected:
    47. virtual void contextMenuEvent(QContextMenuEvent *event)
    48. {
    49. QCustomTreeItem* selected =
    50. reinterpret_cast<QCustomTreeItem*>(currentItem());
    51.  
    52. if ( 0 != selected )
    53. selected->onContextMenu(event);
    54. };
    55. };
    To copy to clipboard, switch view to plain text mode 

    I know that Singals and Slots can only work on QObjects. Looking at the documentation and the Qt source, QTreeWidgetItem is not a subclass of QObject (or anything). I dislike multiple inheritance very much, but tried deriving QCustomTreeItem from both QTreeWidgetItem and QObject to add Signal and Slot functionality. I got the following warning:

    Warning: Class QCustomTreeItem inherits from two QObject subclasses QTreeWidgetItem and QObject. This is not supported!
    If these did not have to be customizable plugin objects, I might just bite the bullet and put a big switch block in QCustomTreeWidget::contextMenuEvent().

    Is there a way make the above model work?

    Thanks!

  2. #2
    Join Date
    Sep 2007
    Location
    Szczecin, Poland
    Posts
    153
    Thanks
    7
    Thanked 11 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Custom QTreeWidgetItem context menu?

    This error means, that QTreeQWidgetItem is QObject internally...

    anyhow, i think using contextMenu events is bad idea in this case(because it's not enougth elastic mechanism). I would create MyTreeWidgetItem with setMenu(QMenu*) / getMenu methods, and override mouseClick method(of a tree) to call getMenu()->exec() for clicked item. It's easier way in my opinion.
    Last edited by mchara; 1st October 2007 at 08:26.

  3. #3
    Join Date
    Sep 2007
    Posts
    14
    Thanks
    5
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Custom QTreeWidgetItem context menu?

    All I really see that doing is moving the execution of the menu into QCustomTreeWidget. I would still need a way to connect the QCustomTreeWidget:nCustomAction(); slot to actCustom::triggered().

    I'll probably end up making a QObject derived class that spits out the tree-node when needed. Then I'll have something to signal/slot.

  4. #4
    Join Date
    Oct 2009
    Posts
    35
    Thanks
    12
    Qt products
    Qt4 Qt/Embedded Qt Jambi
    Platforms
    Unix/X11

    Default Re: Custom QTreeWidgetItem context menu?

    Hi,u try this below code

    //action method.
    void XXXX::XXX()
    {


    action_New = new QAction("&New", ui->treeWidget);
    action_Open = new QAction("&Open", ui->treeWidget);

    //add the code at slots
    connect(action_New, SIGNAL(triggered()), this, SLOT(on_action_New_triggered()));
    connect(action_Open, SIGNAL(triggered()), this, SLOT(on_action_Open_triggered()));


    ui->treeWidget->addAction(action_New);
    ui->treeWidget->addAction(action_Open);
    ui->treeWidget->setContextMenuPolicy(Qt::ActionsContextMenu);

    }

  5. #5
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Custom QTreeWidgetItem context menu?

    I guess one could also useQTreeWidget::itemClicked to provide menus for items.

Similar Threads

  1. Context Menu on QTableWidget
    By ankurjain in forum Qt Programming
    Replies: 9
    Last Post: 17th December 2009, 09:52
  2. Replies: 4
    Last Post: 25th June 2007, 20:40
  3. Misplaced Context Menu
    By kandalf in forum Qt Programming
    Replies: 6
    Last Post: 18th February 2007, 04:28
  4. image for a custom menu Item.......
    By Naveen in forum Qt Programming
    Replies: 2
    Last Post: 23rd February 2006, 09:28
  5. Q3TextEdit custom context menu
    By bcteh_98 in forum Qt Programming
    Replies: 1
    Last Post: 15th February 2006, 21:00

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.