Results 1 to 6 of 6

Thread: Custom context menu in QTreeView

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2009
    Posts
    47
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Custom context menu in QTreeView

    Hi all,

    I have a QTreeView and QStandardItemModel and want to add a context menu so that new child node(s) can be added to the current node.

    At construction, I'd like to have three empty tree nodes
    <checkbox><icon>Rectangles
    <checkbox><icon>Circles
    <checkbox><icon>Squares

    and to enable a right click on each of those nodes to add/delete/modify. I started with an ActionsContextMenu but then I need the node's index, so I successfully created a custom context menu.
    Qt Code:
    1. ...
    2. // set context menu policy
    3. projectTreeView->setContextMenuPolicy(Qt::CustomContextMenu);
    4. ...
    5.  
    6. // initialize the model with three empty nodes
    7. model->setHorizontalHeaderItem(0, new QStandardItem());
    8. m_dm[0] = new QStandardItem(QIcon(":/images/myIcon.png"), "Rectangles");
    9. m_dm[1] = new QStandardItem(QIcon(":/images/myIcon.png"), "Circles");
    10. m_dm[2] = new QStandardItem(QIcon(":/images/myIcon.png"), "Squares");
    11. for (int i=0; i<3; ++i) {
    12. model->appendRow(m_dm[i]);
    13. }
    14. m_ui.projectTreeView->setModel(model);
    15. ...
    16.  
    17. // create an add action and connect it to a signal
    18. m_addAction = new QAction(tr("Add new"), m_ui.projectTreeView);
    19. connect(m_addAction, SIGNAL(triggered()), this, SLOT(addObject()));
    20.  
    21. // connect custom context menu
    22. connect(m_ui.projectTreeView, SIGNAL(customContextMenuRequested( const QPoint& )), this, SLOT(showContextMenu(const QPoint &)));
    23.  
    24. ...
    25. void showContextMenu(const QPoint& pnt)
    26. {
    27. QList<QAction *> actions;
    28. if (m_ui.projectTreeView->indexAt(pnt).isValid()) {
    29. actions.append(m_addAction);
    30. }
    31. if (actions.count() > 0)
    32. QMenu::exec(actions, m_ui.projectTreeView->mapToGlobal(pnt));
    33. }
    34.  
    35. ...
    36. void MainWindow::addObject()
    37. {
    38. // HOW am I going to get access to the node's index to insert a new child row???
    39. }
    To copy to clipboard, switch view to plain text mode 

    I got the code to compile and the addObject() method is invoked properly. My question is how am I going to get access to the corresponding node index to insert a child node appropriately?

    Thanks in advance.
    TNG

  2. The following user says thank you to ttvo for this useful post:

    oberlus (20th August 2011)

Similar Threads

  1. Custom QTreeWidgetItem context menu?
    By AaronMK in forum Qt Programming
    Replies: 4
    Last Post: 1st February 2010, 04:42
  2. Replies: 7
    Last Post: 23rd March 2009, 21:01
  3. Shortcut key for context menu
    By darshan.hardas in forum Qt Programming
    Replies: 1
    Last Post: 28th December 2008, 20:32
  4. Replies: 4
    Last Post: 25th June 2007, 20:40
  5. Q3TextEdit custom context menu
    By bcteh_98 in forum Qt Programming
    Replies: 1
    Last Post: 15th February 2006, 21:00

Tags for this Thread

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.