Results 1 to 2 of 2

Thread: problem in treewidget context menu

  1. #1
    Join Date
    Oct 2007
    Posts
    201
    Thanks
    19
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default problem in treewidget context menu

    Hi all
    I'm writting an application where i'm adding elements to tree widget. if i right click on that treewidget a pop up menu shoud open. which having option to addparent or add child to parent. below is my code but its not working.i put a debug statment to capture the action but its showing the action is empty.

    Qt Code:
    1. Headre file---
    2. private slot:
    3. void addparent();
    4. void addchild();
    5. QMenu contextMenu;
    6. void ContextMenuDisplayMethod(const QPoint &pt);
    7.  
    8. In constructor---
    9. connect(ui->treeWidget, SIGNAL(customContextMenuRequested(const QPoint &)),
    10. this, SLOT(ContextMenuDisplayMethod(const QPoint &)));
    11.  
    12. in cpp file---
    13.  
    14. void ContextMenuDisplayMethod(const QPoint &pt)
    15. {
    16. QAction action1(tr("Add parent"),ui->treeWidget);
    17. QAction action2(tr("Add child"),ui->treeWidget);
    18. QList<QAction *> actionsList;
    19. actionsList.append(&action1);
    20. actionsList.append(&action2);
    21.  
    22. contextMenu.addAction(&action1);
    23. contextMenu.addAction(&action2);
    24.  
    25. contextMenu.popup(QCursor::pos());
    26. contextMenu.exec(actionsList,pt);
    27.  
    28. qDebug()<<"menu slected : "<<contextMenu.menuAction()->text();// showing Action clicked: ""
    29. connect(&action1, SIGNAL(triggered()),
    30. this, SLOT(addparent()));
    31. connect(&action2, SIGNAL(triggered()),
    32. this, SLOT(addchild()));
    33.  
    34. }
    To copy to clipboard, switch view to plain text mode 



    plz help..
    Last edited by phillip_Qt; 12th November 2009 at 11:01. Reason: error correction
    Cheers,
    Phillip



    --- Please post the solution you got to solve your problem. It may help others.

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: problem in treewidget context menu

    you should allocate actions in the heap, but you create them in the stack.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

Similar Threads

  1. Context Menu on QTableWidget
    By ankurjain in forum Qt Programming
    Replies: 9
    Last Post: 17th December 2009, 09:52
  2. Custom context menu in QTreeView
    By ttvo in forum Qt Programming
    Replies: 5
    Last Post: 3rd April 2009, 22:29
  3. QTreeWidget Context Menu
    By ^NyAw^ in forum Qt Programming
    Replies: 2
    Last Post: 11th May 2007, 11:24
  4. Menu problem using Designer
    By JimBrown in forum Qt Tools
    Replies: 1
    Last Post: 19th February 2007, 21:47
  5. context menu advice
    By larry104 in forum Qt Programming
    Replies: 1
    Last Post: 4th October 2006, 07:55

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.