Results 1 to 18 of 18

Thread: get mysql table into QComboBox

  1. #1
    Join Date
    Nov 2006
    Posts
    96

    Default 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:

    Qt Code:
    1. kraj_combo = new QComboBox();
    2. list << tr("Kranj") << tr("Postojna") << tr("Ljubljana") << tr("Trzin") << tr("Maribor");
    3. kraj_combo->insertItems(0,list);
    To copy to clipboard, switch view to plain text mode 

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

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,365
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: get mysql table into QComboBox


  3. #3
    Join Date
    Nov 2006
    Posts
    96

    Default 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.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,365
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default 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.

  5. #5
    Join Date
    Nov 2006
    Posts
    96

    Default 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

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,365
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: get mysql table into QComboBox

    See QSqlQuery docs. There are examples there.

  7. #7
    Join Date
    Nov 2006
    Posts
    96

    Default Re: get mysql table into QComboBox

    Hi. Now I did this:

    Qt Code:
    1. QStringList kraji_list, kraji_list_numbers;
    2. QString town, town_number;
    3. QSqlQuery query("SELECT ime,postna_st FROM kraj");
    4. while(query.next()) {
    5. //value 0 corresponds to ime in SELECT statement
    6. town = query.value(0).toString();
    7. town_number = query.value(1).toString();
    8. kraji_list.append(town);
    9. kraji_list_numbers.append(town_number);
    10. }
    11. kraj_combo->insertItems(0,kraji_list);
    12. kraj_combo->insertItems(0,kraji_list_numbers);
    To copy to clipboard, switch view to plain text mode 

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

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,365
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: get mysql table into QComboBox

    You have to combine items together.
    Qt Code:
    1. QString town = "London";
    2. QString resid = "1000000";
    3. QString combined = town+" "+resid;
    To copy to clipboard, switch view to plain text mode 
    And then add only those combined items to the list.

  9. #9
    Join Date
    Nov 2006
    Posts
    96

    Default 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?

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,365
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: get mysql table into QComboBox

    Quote Originally Posted by eleanor View Post
    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.

  11. #11
    Join Date
    Nov 2006
    Posts
    96

    Default Re: get mysql table into QComboBox

    Qt Code:
    1. while(query.next()) {
    2. //value 0 corresponds to ime in SELECT statement
    3. town = query.value(0).toString();
    4. town_number = query.value(1).toString();
    5. combined = town + " " + town_number;
    6. comb.append(combined);
    7. }
    8. //sort the list of items in ascending order
    9. //comb.sort();
    10. QMap<QString,QString> comb;
    11. kraj_combo->insertItems(0,comb);
    To copy to clipboard, switch view to plain text mode 

    - 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?

  12. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,365
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: get mysql table into QComboBox

    Quote Originally Posted by eleanor View Post
    - 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.

  13. #13
    Join Date
    Nov 2006
    Posts
    96

    Default Re: get mysql table into QComboBox

    Now I created 2 QComboBOxes like this:

    Qt Code:
    1. QStringList comb,comb_number;
    2. QString town, town_number;//, combined;
    3. QSqlQuery query("SELECT ime,postna_st FROM kraj");
    4. while(query.next()) {
    5. //value 0 corresponds to ime in SELECT statement
    6. town = query.value(0).toString();
    7. town_number = query.value(1).toString();
    8.  
    9. //combined = town + " " + town_number;
    10. comb.append(town);
    11. comb_number.append(town_number);
    12. }
    13. //sort the list of items in ascending order (case-sensitively)
    14. comb.sort();
    15. comb_number.sort();
    16. //QMap<QString,QString> comb;
    17. kraj_combo->insertItems(0,comb);
    18. kraj_combo_number->insertItems(0,comb_number);
    To copy to clipboard, switch view to plain text mode 

    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?

  14. #14
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,365
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: get mysql table into QComboBox

    Take a look at signals and slots QComboBox offers.

  15. #15
    Join Date
    Nov 2006
    Posts
    96

    Default Re: get mysql table into QComboBox

    Yes, I did...it would be easy this way:

    Qt Code:
    1. connect(kraj_combo,SIGNAL(currentIndexChanged(int)),kraj_combo_number,SLOT(setCurrentIndex(int)));
    To copy to clipboard, switch view to plain text mode 

    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?

  16. #16
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,365
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default 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.

  17. #17
    Join Date
    Nov 2006
    Posts
    96

    Default Re: get mysql table into QComboBox

    Two wa mapping.WHat's this?

    Can you express this with an example please?

  18. #18
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,365
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default 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.

Similar Threads

  1. MYSQL 5 Table qt model as small Mysql admin
    By patrik08 in forum Qt-based Software
    Replies: 0
    Last Post: 1st May 2007, 10:43
  2. QDataWidgetMapper <=> QComboBox best practice
    By saknopper in forum Qt Programming
    Replies: 1
    Last Post: 18th January 2007, 11:50
  3. Qt 4.1.4 & Mysql 5 on Linux x64
    By bothapn in forum Installation and Deployment
    Replies: 7
    Last Post: 4th August 2006, 14:23
  4. displaying any table on a qdatatable
    By Philip_Anselmo in forum Newbie
    Replies: 4
    Last Post: 9th May 2006, 23:12
  5. creating table plugin
    By mgurbuz in forum Qt Programming
    Replies: 3
    Last Post: 28th April 2006, 14:50

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.