Multiple QComboboxes, best practice
-
Alright, I'm pretty much lost when it comes to models. I hate to ask for this, but does someone have a simple example on how to do this?
I've googled and found various examples but they really doesn't make any sense to me. I'm probably just to stupid for this. :P
-
From a user experience perspective, I'd like to raise another issue. Are you sure that using two QComboBoxes to select a main & sub category is really the optimal solution? Personally, I don't like it much to be honest. You don't get a great overview over what is available, and you need quite a number of mouse clicks to select anything.
Would it not make sense to use a single tree view instead, in which you allow filtering? That would allow your user to view all available categories and subcategories in one go and allow you to extend your hierarchy in places where that is needed. You could even, with some tricks, use that tree view as the dropdown-part of a QComboBox in case you lack the space for a full tree view in your form.
The filtering feature would need a bit of work to get right. A standard QSFPM doesn't cut it, as you would have to still return main categories that have sub categories that match, even if they themselves do not match the filter (and vise versa). However, there are already topics discussing this here on the forum, complete with example code on how you could do this.
-
Andre: You're right. However, I want to keep the form minimalistic.
Say, a push button which pops up a menu when triggered and then changes text to represent what category you have chosen. Would that be considered user friendly/practical or just messy?
You'd have somewhat better overview than with two comboboxes though.
Tree view as the dropdown part of a combobox seems a little bit too advanced for me. :)
Thanks for your input!
-
I hope the following code will help. I've included comments to help you understand. I'm new to Qt myself so if my example is wrong, others will definitely point it out. Good luck!
@
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); // The DB being used
db.setDatabaseName("Path\test1.db"); //Where the DB lies
db.open(); //to open the databaseQSqlTableModel m = new QSqlTableModel(); //Model which maps the DB to the comboBox
m->setTable("table"); //The table in the DB which has to be mapped
m->select(); //The model needs to be selected in order to use itQComboBox *comboBox = new QCombobox();
comboBox->setModel(m); //This is where the mapping happens
comboBox->show();
@ -
The code shown by holygirl is not very useful for the case at hand. The discussion was about a set of two comboboxes with a catagory and a sub category. There was no mention of the data being stored in a data base though. The code also fails to handle the case where the thing to be displayed in the combo box is not in the first column of the table, as would often be the case (the ID is usually first).
-
[quote author="Andre" date="1359455436"]The code also fails to handle the case where the thing to be displayed in the combo box is not in the first column of the table, as would often be the case (the ID is usually first). [/quote]
Suppose we had to display the 3rd column in the comboBox, could we not do something like?
@
model->removeColumns(0,2);
@ -
qxoz!
Omg!! I didn't know I could do something like
@
comboBox->setModelColumn(int);
@I've been deleting columns all this while to get to the correct column. Thank you SO SO much for teaching me this. I'm clearly not doing a good job learning by myself. So, thanks!
And sorry to the OP for deviating from the main topic.
-
Okay, I think I'm starting to understand models a little bit now - though I haven't made a custom one yet. This is what I've come up with now:
In my main window constructor
@ QStandardItem *categoryOne = new QStandardItem("One");
categoryOne->appendRow(new QStandardItem("Some"));
categoryOne->appendRow(new QStandardItem("stuff"));
categoryOne->appendRow(new QStandardItem("here"));QStandardItem *categoryTwo = new QStandardItem("Two"); categoryTwo->appendRow(new QStandardItem("More")); categoryTwo->appendRow(new QStandardItem("stuff")); QStandardItemModel *model = new QStandardItemModel(); model->appendRow(categoryOne); model->appendRow(categoryTwo); ui->comboCategory->setModel(model); ui->comboSubcategory->setModel(model);@
In the currentIndexChanged slot for the main category combobox
@ //QStandardItemModel model = static_cast<QStandardItemModel>(ui->comboCategory->model());
QAbstractItemModel *model = ui->comboCategory->model(); ui->comboSubcategory->setRootModelIndex(model->index(index, 0)); ui->comboSubcategory->setCurrentIndex(0);@
It seems to work fine, but is this the "correct" way of doing it?