Results 1 to 8 of 8

Thread: QComboBox and QTreeView

  1. #1
    Join Date
    May 2009
    Posts
    52
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default QComboBox and QTreeView

    I'm trying to use a QTreeView as a view for a QComboBox.

    I've done something like this:

    Qt Code:
    1. TreeView m_view = new TreeView(this);
    2.  
    3. QComboBox* m_combo = new QComboBox(this);
    4. m_combo->setModel(m_view->getModel());
    5. m_combo->setView(m_view);
    6.  
    7. QStandardItem* item1 = new QStandardItem("One");
    8. QStandardItem* item2 = new QStandardItem("Two");
    9. QStandardItem* item3 = new QStandardItem("Three);
    10. QStandardItem* item4 = new QStandardItem("Four");
    11.  
    12. m_view->getModel()->appendRow(item1);
    13. item1->appendRow(item2);
    14. m_view->getModel()->appendRow(item3);
    15. m_view->getModel()->appendRow(item4);
    To copy to clipboard, switch view to plain text mode 

    My questions are:

    1) Am I doing the right thing there?

    2) How can I select an item from the list?

    3) How can I know when an item has been selected?

    For 2) and 3) I tried several things but failed

    For example if the QTreeView is not set as a view for the combo box, I can use the signal clicked(), but this is not working with the view set on the combo box. If I use the combo's currentIndexChanged() signal, selecting item 2 will return index 0, because item 2 is a child of item 1 at index 0. I'm really confused about this model/view thing

  2. #2
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QComboBox and QTreeView

    first set a model to treeview
    treeview->setModel(new QStandardItemModel()).

    secondly i dont know wether tree view is a good choice (or even work) for comboboxes...

  3. #3
    Join Date
    May 2009
    Posts
    52
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QComboBox and QTreeView

    Thanks for the answer. I did set the model for tree view, my class TreeView is an extension of QTreeView

    What I'm trying to achieve is a combo box like the one in the Windows Explorer file dialog, like:

    Qt Code:
    1. Image - Desktop
    2. Image - My Documents
    3. Image - My Computer
    4. Image - C:
    5. Image - D:
    6. Image - Folder
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2008
    Location
    Bengaluru
    Posts
    144
    Thanks
    8
    Thanked 7 Times in 7 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: QComboBox and QTreeView

    If i have understood your question right, then why cannot you use QDirView to show like windows explorer.

  5. #5
    Join Date
    May 2009
    Posts
    52
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QComboBox and QTreeView

    If you mean QDirModel, that is because i want to include those Windows "special folders", like "Desktop" and also I want to put in the volume names, because only C, D, E, F, G, H, I, J, M, R etc look weird and not so clear

    It's really not important what is in the tree, it's just I don't really manage to get it work

  6. #6
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QComboBox and QTreeView

    If you mean QDirModel, that is because i want to include those Windows "special folders", like "Desktop" and also I want to put in the volume names, because only C, D, E, F, G, H, I, J, M, R etc look weird and not so clear
    I'm not sure but QFileSystemModel can be better in your case.
    It's really not important what is in the tree, it's just I don't really manage to get it work
    Here it is (screenshot attached):
    Qt Code:
    1. //prepare model
    2. QStandardItemModel *model = new QStandardItemModel(ui->comboBox);
    3. QStandardItem *item = new QStandardItem("item1");
    4. model->appendRow(item);
    5. item->appendRow(new QStandardItem("item2"));
    6. item = new QStandardItem("item3");
    7. model->appendRow(item);
    8. item->appendRow(new QStandardItem("item4"));
    9. item = item->child(0,0);
    10. item->appendRow(new QStandardItem("item5"));
    11.  
    12. // or
    13. QFileSystemModel *fsmodel = new QFileSystemModel(this);
    14. fsmodel->setRootPath("/");
    15.  
    16. // set view
    17. QTreeView *tv = new QTreeView(ui->comboBox);
    18. ui->comboBox->setView(tv);
    19.  
    20.  
    21. // set model
    22. // ui->comboBox->setModel(model);
    23. ui->comboBox->setModel(fsmodel);
    To copy to clipboard, switch view to plain text mode 
    The only problem is that expanding treeview closes the popup.
    For example if the QTreeView is not set as a view for the combo box, I can use the signal clicked(), but this is not working with the view set on the combo box. If I use the combo's currentIndexChanged() signal, selecting item 2 will return index 0, because item 2 is a child of item 1 at index 0. I'm really confused about this model/view thing
    You can use QComboBox::currentText() to get currently selected text or maybe use QItemSelectionModel::currentChanged() from:
    Qt Code:
    1. comboBox->view()->selectionModel()
    To copy to clipboard, switch view to plain text mode 
    (didn't try this).
    Attached Images Attached Images
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  7. #7
    Join Date
    May 2009
    Posts
    52
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QComboBox and QTreeView

    Could't make it work, now I'm doing something else with a QTreeWidget

    I have a question regarding the cursor appearance. How can I make it span only the text width (like in the picture above), not the whole line width?

  8. #8
    Join Date
    Feb 2011
    Posts
    7
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QComboBox and QTreeView

    I just spent two days trying to find a simple way to get a tree into the combobox popup, and here's the solution I finally came up with. I'm putting these few lines of code here so they may help others

    Qt Code:
    1. #include <QtGui>
    2.  
    3. int main(int argc, char *argv[]) {
    4. QApplication app(argc, argv);
    5. QComboBox combo;
    6.  
    7. combo.setView(new QTreeView(&combo));
    8. combo.setModel(new QStandardItemModel(&combo));
    9.  
    10. // when adding children use a QStandardItem pointer to the parent
    11. QStandardItem *item = new QStandardItem("Toplevel1");
    12. item->setSelectable(false); // make partents of subtrees not selectable
    13. ((QStandardItemModel*)combo.model())->appendRow(item);
    14. item->appendRow(new QStandardItem("Child1"));
    15. item->appendRow(new QStandardItem("Child2"));
    16.  
    17. // with no children the standard QComboBox method can be used
    18. combo.addItem("Toplevel2");
    19. combo.show();
    20.  
    21. return app.exec();
    22. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. QSortFilterProxyModel with QComboBox
    By h123 in forum Qt Programming
    Replies: 1
    Last Post: 10th June 2009, 19:16
  2. QcomboBox update QTreeView
    By josiah47 in forum Newbie
    Replies: 2
    Last Post: 5th March 2009, 23:32
  3. Editable QComboBox with QItemDelegate
    By Jmgr in forum Qt Programming
    Replies: 11
    Last Post: 10th December 2008, 09:21
  4. row height with QTreeView and delegates
    By Philipp in forum Qt Programming
    Replies: 7
    Last Post: 11th November 2008, 10:40
  5. How to use QTreeView in QComboBox?
    By Tamara in forum Qt Programming
    Replies: 2
    Last Post: 20th June 2007, 19:03

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.