PDA

View Full Version : get mysql table into QComboBox



eleanor
10th October 2007, 12:05
Hi, I have a problem.

I have a mysql table of towns and I want those towns to be available in QComboBox.

Any idea how to do that?

Just for testing I did this:



kraj_combo = new QComboBox();
QStringList list;
list << tr("Kranj") << tr("Postojna") << tr("Ljubljana") << tr("Trzin") << tr("Maribor");
kraj_combo->insertItems(0,list);


Now I need to change this as I said. Please help me.

wysota
10th October 2007, 12:09
Wrap that table into QSqlTableModel and use QComboBox::setModel() and QComboBox::setModelColumn().

eleanor
10th October 2007, 12:55
Hi , it worked.

Now I need to display 2 things in QComboBox, the "town" and the "number of people in town" beside town

- this two information are in the same table, just in different column and I need to display it like this (in QComboBox)

London 12000000
New York 2100000

- this two are just an example.

wysota
10th October 2007, 13:00
Ok :)
For that you'll need to implement a proxy model that merges a two column model into one column model. But if you continue to add requirements, it might prove simpler to extract data from the table using whatever means (QSqlTableModel or QSqlQuery) and pass the composed data to the combobox using a string list like in your first post.

eleanor
10th October 2007, 13:02
Huh, well how can I extract data from the table into a String list and add it to the QComboBox. Example would really help.

BTW: thanks for all your help

wysota
10th October 2007, 13:04
See QSqlQuery docs. There are examples there.

eleanor
10th October 2007, 13:40
Hi. Now I did this:



QStringList kraji_list, kraji_list_numbers;
QString town, town_number;
QSqlQuery query("SELECT ime,postna_st FROM kraj");
while(query.next()) {
//value 0 corresponds to ime in SELECT statement
town = query.value(0).toString();
town_number = query.value(1).toString();
kraji_list.append(town);
kraji_list_numbers.append(town_number);
}
kraj_combo->insertItems(0,kraji_list);
kraj_combo->insertItems(0,kraji_list_numbers);


Which works, but it doesn't display the town and town's number in the same row...how to do that?

wysota
10th October 2007, 14:13
You have to combine items together.

QString town = "London";
QString resid = "1000000";
QString combined = town+" "+resid;
And then add only those combined items to the list.

eleanor
10th October 2007, 14:20
Thanks, it works.

Now I only need to know how slow will this work. Because in the future I'll have to add 100.000 towns in the mysql table and in QComboBox. So will this be instant, or will take a lot of time.

And how to add items in that QComboBox based on alpabetical order?

wysota
10th October 2007, 14:36
Now I only need to know how slow will this work. Because in the future I'll have to add 100.000 towns in the mysql table and in QComboBox. So will this be instant, or will take a lot of time.
Are you sure having a combobox with 100000 items is a good idea? How do you expect anyone to find anything there? Maybe it'd be better to provide a separate dialog with a list and search facilities to choose the town?


And how to add items in that QComboBox based on alpabetical order?
Sort the list before adding it to the combo box.

eleanor
10th October 2007, 14:48
while(query.next()) {
//value 0 corresponds to ime in SELECT statement
town = query.value(0).toString();
town_number = query.value(1).toString();
combined = town + " " + town_number;
comb.append(combined);
}
//sort the list of items in ascending order
//comb.sort();
QMap<QString,QString> comb;
kraj_combo->insertItems(0,comb);


- I want to sort items apphabetically but not case sensitively. I'm not sure how to do it?


Well, if you type in the QCobmoBox a name or a number (I added in the previous post) then it throws you on that name, you don't have to search through that manually. I'm just worried that it would be too slow, would it?

wysota
10th October 2007, 15:03
- I want to sort items apphabetically but not case sensitively. I'm not sure how to do it?
Town names usually start with capital letters, so what's the problem with case sensitivity? Anyway you can provide your own lessThan implementation that will do a case insensitive comparison.



Well, if you type in the QCobmoBox a name or a number (I added in the previous post) then it throws you on that name, you don't have to search through that manually.
Ok, but it looks pretty bad.


I'm just worried that it would be too slow, would it?

Adding and first display might be slow. Later it should work instantly.

eleanor
10th October 2007, 15:32
Now I created 2 QComboBOxes like this:



QStringList comb,comb_number;
QString town, town_number;//, combined;
QSqlQuery query("SELECT ime,postna_st FROM kraj");
while(query.next()) {
//value 0 corresponds to ime in SELECT statement
town = query.value(0).toString();
town_number = query.value(1).toString();

//combined = town + " " + town_number;
comb.append(town);
comb_number.append(town_number);
}
//sort the list of items in ascending order (case-sensitively)
comb.sort();
comb_number.sort();
//QMap<QString,QString> comb;
kraj_combo->insertItems(0,comb);
kraj_combo_number->insertItems(0,comb_number);


but, I what I want to do is this: when one of the items is selected (in either of the QComboBoxes) I want to adjust the second one to the same value (based on the ID number) in the table.

So for example there is an entry like this in table:
London 1000

when I choose 1000, I want the other QComboBox to set itself to London and the other way around. Any idea how to do that?

wysota
10th October 2007, 15:35
Take a look at signals and slots QComboBox offers.

eleanor
10th October 2007, 15:43
Yes, I did...it would be easy this way:



connect(kraj_combo,SIGNAL(currentIndexChanged(int) ),kraj_combo_number,SLOT(setCurrentIndex(int)));


but the problem with this code is that I sort the QComboBox and the indexes do not match. I want to match them based on ID in mysql table.

Any idea?

wysota
10th October 2007, 15:53
So provide a two way mapping between the id and the name. Then you can see which index in one combobox corresponds to the same item in the other and select the appropriate item.

eleanor
10th October 2007, 15:56
Two wa mapping.WHat's this?

Can you express this with an example please?

wysota
10th October 2007, 16:35
Two way mapping - mapping from name to id and from id to name. So that you know that if London has index 7 in one list, it has index 12 in the other list and that if it has index 12 in the second list, it has index 7 in the first one.