PDA

View Full Version : how to do chained selection



xfurrier
11th January 2009, 21:16
I use QMultiHash for categories (keys) and subcategories (values). In a wizard page I want to have two QComboBox-es - the first for selecting a category (say - arts, education, engineering, leisure and sports, ...) and the second one for a subcategory (say, for engineering one could select civil, electrical, mechanical, ...).

Of course, the values of the second combo box are determined by user's selection in the first one. How can I do that? It would be great if somebody could help with a simple example code.

I suppose I need to use signals and slots, but after many hours of trying I still haven't managed to work it out. I'm a complete QT newbie and relatively new to C++.

jpn
12th January 2009, 10:26
// "QMultiHash for categories (keys) and subcategories (values)"
QMultiHash<QString, QString> categoryHash;

// establish the connection
connect(ui.categoryCombo, SIGNAL(currentIndexChanged(QString)), this, SLOT(setCategory(QString)));

// declared as a slot, of course
void MyWindow::setCategory(const QString& category)
{
ui.subcategoryCombo->clear();
QStringList subcategories = categoryHash.values(category);
ui.subcategoryCombo->addItems(subcategories);
}

xfurrier
14th January 2009, 16:53
Thanks for that, you are a star. How things look simple/obvious once you see the solution. I really don't understand how could've I spent so much time trying to solve my problem.