get mysql table into QComboBox
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:
Code:
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.
Re: get mysql table into QComboBox
Re: get mysql table into QComboBox
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.
Re: get mysql table into QComboBox
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.
Re: get mysql table into QComboBox
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
Re: get mysql table into QComboBox
See QSqlQuery docs. There are examples there.
Re: get mysql table into QComboBox
Hi. Now I did this:
Code:
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?
Re: get mysql table into QComboBox
You have to combine items together.
And then add only those combined items to the list.
Re: get mysql table into QComboBox
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?
Re: get mysql table into QComboBox
Quote:
Originally Posted by
eleanor
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?
Quote:
And how to add items in that QComboBox based on alpabetical order?
Sort the list before adding it to the combo box.
Re: get mysql table into QComboBox
Code:
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?
Re: get mysql table into QComboBox
Quote:
Originally Posted by
eleanor
- 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.
Quote:
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.
Quote:
I'm just worried that it would be too slow, would it?
Adding and first display might be slow. Later it should work instantly.
Re: get mysql table into QComboBox
Now I created 2 QComboBOxes like this:
Code:
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?
Re: get mysql table into QComboBox
Take a look at signals and slots QComboBox offers.
Re: get mysql table into QComboBox
Yes, I did...it would be easy this way:
Code:
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?
Re: get mysql table into QComboBox
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.
Re: get mysql table into QComboBox
Two wa mapping.WHat's this?
Can you express this with an example please?
Re: get mysql table into QComboBox
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.