View Full Version : QComboBox and QTreeView
Carlsberg
8th July 2009, 11:32
I'm trying to use a QTreeView as a view for a QComboBox.
I've done something like this:
TreeView m_view = new TreeView(this);
QComboBox* m_combo = new QComboBox(this);
m_combo->setModel(m_view->getModel());
m_combo->setView(m_view);
QStandardItem* item1 = new QStandardItem("One");
QStandardItem* item2 = new QStandardItem("Two");
QStandardItem* item3 = new QStandardItem("Three);
QStandardItem* item4 = new QStandardItem("Four");
m_view->getModel()->appendRow(item1);
item1->appendRow(item2);
m_view->getModel()->appendRow(item3);
m_view->getModel()->appendRow(item4);
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 :D
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
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...:)
Carlsberg
8th July 2009, 13:04
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:
Image - Desktop
Image - My Documents
Image - My Computer
Image - C:
Image - D:
Image - Folder
nikhilqt
8th July 2009, 19:53
If i have understood your question right, then why cannot you use QDirView to show like windows explorer.
Carlsberg
8th July 2009, 21:05
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 :D
faldzip
9th July 2009, 08:42
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):
//prepare model
QStandardItemModel *model = new QStandardItemModel(ui->comboBox);
QStandardItem *item = new QStandardItem("item1");
model->appendRow(item);
item->appendRow(new QStandardItem("item2"));
item = new QStandardItem("item3");
model->appendRow(item);
item->appendRow(new QStandardItem("item4"));
item = item->child(0,0);
item->appendRow(new QStandardItem("item5"));
// or
QFileSystemModel *fsmodel = new QFileSystemModel(this);
fsmodel->setRootPath("/");
// set view
QTreeView *tv = new QTreeView(ui->comboBox);
ui->comboBox->setView(tv);
// set model
// ui->comboBox->setModel(model);
ui->comboBox->setModel(fsmodel);
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:
comboBox->view()->selectionModel()
(didn't try this).
Carlsberg
9th July 2009, 19:04
Could't make it work, now I'm doing something else with a QTreeWidget :D
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?
Batox
14th February 2013, 16:58
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 :)
#include <QtGui>
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
QComboBox combo;
combo.setView(new QTreeView(&combo));
combo.setModel(new QStandardItemModel(&combo));
// when adding children use a QStandardItem pointer to the parent
QStandardItem *item = new QStandardItem("Toplevel1");
item->setSelectable(false); // make partents of subtrees not selectable
((QStandardItemModel*)combo.model())->appendRow(item);
item->appendRow(new QStandardItem("Child1"));
item->appendRow(new QStandardItem("Child2"));
// with no children the standard QComboBox method can be used
combo.addItem("Toplevel2");
combo.show();
return app.exec();
}
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.