Results 1 to 6 of 6

Thread: Custom context menu in QTreeView

  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)

  3. #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: Custom context menu in QTreeView

    try to use QAbstractItemView::currentIndex.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

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

    Default Re: Custom context menu in QTreeView

    Thanks. Follow-up questions,
    1) I want to setCheckable on the tree top level nodes to true when it has children + when check/uncheck a parent's checkbox, all children will be checked/unchecked accordingly. Do I have to subclass QStandardItem?

    2) Children node always has a checkbox or radiobox, and toggling child node checkbox/radiobox won't change its parent checked state. Do I have to subclass QStandardItem?

    3) when create a child node, I want to use a different default icon and/or naming convention by the parent's type (i.e. Squares, Circles, ...). I don't want to just rely on the name of the parent's node if I don't have to. If so, can I use QStandardItem then somehow utilizing Qt role/type OR do I need to subclass QStandardItem?

    In general, I want to avoid subclassing QStandardItem, QStandardItemModel, and QTreeView unless I have to.
    TNG

  5. #4
    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: Custom context menu in QTreeView

    Quote Originally Posted by ttvo View Post
    Thanks. Follow-up questions,
    1) I want to setCheckable on the tree top level nodes to true when it has children + when check/uncheck a parent's checkbox, all children will be checked/unchecked accordingly. Do I have to subclass QStandardItem?
    try this code
    Qt Code:
    1. ...
    2. tree->setColumnCount(1);
    3.  
    4. root->setCheckState(0, Qt::Unchecked);
    5. tree->insertTopLevelItem(0, root);
    6.  
    7. for (int i = 0; i < 10; ++i) {
    8. QTreeWidgetItem *item = new QTreeWidgetItem(root, QStringList(QString("item: %1").arg(i)));
    9. item->setCheckState(0, Qt::Unchecked);
    10. for (int j = 0; j < 10; ++j) {
    11. QTreeWidgetItem *itm = new QTreeWidgetItem(item, QStringList(QString("item: %1").arg(i)));
    12. itm->setCheckState(0, Qt::Unchecked);
    13. }
    14. }
    15.  
    16. connect(tree, SIGNAL(itemChanged(QTreeWidgetItem *, int)), SLOT(itemChanged(QTreeWidgetItem *, int)));
    17. ....
    18. void MyWidget::itemChanged(QTreeWidgetItem *item, int)
    19. {
    20. changeItemState(item);
    21. }
    22.  
    23. void MyWidget::changeItemState(QTreeWidgetItem *item)
    24. {
    25. if (!item)
    26. return;
    27.  
    28. for (int i = 0; i < item->childCount(); ++i) {
    29. QTreeWidgetItem *child = item->child(i);
    30. child->setCheckState(0, item->checkState(0));
    31. changeItemState(child);
    32. }
    33. }
    To copy to clipboard, switch view to plain text mode 
    I don't test it, but hope you got idea.
    PS. I don't see any reason to subclass QStandardItem in second and third points.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  6. The following user says thank you to spirit for this useful post:

    ttvo (3rd April 2009)

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

    Default Re: Custom context menu in QTreeView

    I added

    Qt Code:
    1. connect(model, SIGNAL(itemChanged(QStandardItem * item)), SLOT(myItemChanged(QStandardItem *item)));
    2.  
    3. // where QStandardItemModel *model = new QStandardItemModel();
    To copy to clipboard, switch view to plain text mode 

    from my MainWindow (inherits from QMainWindow) and
    Qt Code:
    1. void MainWindow::myItemChanged(QStandardItem *item)
    2. {
    3. changeItemState(item);
    4. }
    5.  
    6. void MainWindow::changeItemState(QStandardItem *item)
    7. {
    8. if (!item)
    9. return;
    10. for (int i=0; i<item->rowCount(); ++i) {
    11. QStandardItem *child = item->child(i);
    12. child->setCheckable(item->checkState());
    13. changeItemState(child);
    14. }
    15. }
    To copy to clipboard, switch view to plain text mode 
    similar to your example but the two methods weren't invoked at all when I toggle the checkbox.
    TNG

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

    Default (Fixed) Custom context menu in QTreeView

    Quote Originally Posted by ttvo View Post
    I added

    Qt Code:
    1. connect(model, SIGNAL(itemChanged(QStandardItem * item)), SLOT(myItemChanged(QStandardItem *item)));
    2.  
    3. // where QStandardItemModel *model = new QStandardItemModel();
    To copy to clipboard, switch view to plain text mode 
    The correct code is:
    Qt Code:
    1. connect(model, SIGNAL(itemChanged(QStandardItem * )), SLOT(myItemChanged(QStandardItem *)));
    To copy to clipboard, switch view to plain text mode 
    TNG

Similar Threads

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