Results 1 to 8 of 8

Thread: How to get database id value from QComboBox text?

  1. #1
    Join Date
    Sep 2011
    Posts
    6
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default How to get database id value from QComboBox text?

    Hello all,

    I have a 2 tables. One of them have the following data:

    Qt Code:
    1. ID Text
    2. ----------------
    3. 1 Apple
    4. 4 Banana
    5. 6 Mango
    6. 8 Rambutan
    7. 19 Durian
    To copy to clipboard, switch view to plain text mode 

    I got a QComboBox to populate with the right text using QSqlRelationalTableModel and QSqlTableModel:
    Qt Code:
    1.  
    2. // etc etc... populate database, set relations as per example on help files etc...
    3.  
    4. QSqlTableModel * relModel = rtmodel->relationModel(typeIndex);
    5. ui->myComboBox->setModel(relModel);
    6. ui->myComboBox->setModelColumn(relModel->fieldIndex("Text"));
    To copy to clipboard, switch view to plain text mode 

    The QComboBox shows the text fine, meaning I can choose the fruit I want. How do I get the selected text's ID once I've selected the text? Say, I picked Durian, how do I get myComboBox to return the value '19'?

    Thanks.

  2. #2
    Join Date
    Dec 2012
    Posts
    197
    Thanks
    25
    Thanked 41 Times in 33 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to get database id value from QComboBox text?

    Hello there,
    i don't exactly understand what you are trying to do, but take a look at the public member data of QSqlTableModel and try to get the index of row 0,
    I am sure you can find several other ways to do what you want.
    I hope this helps.
    Last edited by toufic.dbouk; 29th September 2013 at 14:46.

  3. #3
    Join Date
    Sep 2011
    Posts
    6
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to get database id value from QComboBox text?

    Hi. Thanks for the response. Unfortunately, I'm getting this error...

    Qt Code:
    1. error: no matching function for call to 'QSqlTableModel::index(int)'
    2. qDebug() << relModel->data(relModel->index(0));
    3. ^
    To copy to clipboard, switch view to plain text mode 

    What I'm trying to do is get the corresponding "ID" for the text values shown in the QComboBox. So, if I picked 'Banana' from the dropdown list, what function do I use to get the "ID" for 'Banana' which is 4, as shown in the table?

    I can find functions for currentText {Returns: Banana}, currentIndex {Returns: 1}, but nothing for currentModelCorrespondingData {Hoping it returns: 4} or something like that?
    Last edited by ohmyqt; 29th September 2013 at 14:11.

  4. #4
    Join Date
    Dec 2012
    Posts
    197
    Thanks
    25
    Thanked 41 Times in 33 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to get database id value from QComboBox text?

    yea my bad , i meant something else , it wouldn't have helped anyway.
    umm why don't you search in the database for the ID according to the text ( Banana ) after getting it?
    when CurrentText returns Banana, execute an Sql query to get the ID.
    Time consuming and not elegant , but if the table is small and you cant find a better way , give it a try.
    I am not aware of any way that can get that directly from the QcomboBox. if you found a solution let me know please.

  5. The following user says thank you to toufic.dbouk for this useful post:

    ohmyqt (29th September 2013)

  6. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to get database id value from QComboBox text?

    You want to access data in the model, so you need to get a QModelIndex.
    The index() method needs two arguments (table model -> you need row and column to address a cell).

    The combo box displays values of one column of the table, so it can give you the row that it is currently displaying:
    currentIndex -> row

    You already used QSqlTableModel::fieldIndex to get the column for "Text". Doing that for "Id" gives you the column:
    fieldIndex -> column

    Once you have the QModelIndex, you can use its data() method to retrieve the ID value.

    Cheers,
    _

  7. The following 2 users say thank you to anda_skoa for this useful post:

    ohmyqt (3rd October 2013), toufic.dbouk (30th September 2013)

  8. #6
    Join Date
    Dec 2012
    Posts
    197
    Thanks
    25
    Thanked 41 Times in 33 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to get database id value from QComboBox text?

    Thats exactly what i was trying to explain but i didn't know how because i never tried it.
    Thanks for the clarification. Im sure it will work perfectly

  9. #7
    Join Date
    May 2013
    Location
    Schweiz
    Posts
    21
    Thanked 1 Time in 1 Post
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Windows

    Default Re: How to get database id value from QComboBox text?

    hi,
    I have the same problem.
    so, which is the correct solution?
    thanks.

  10. #8
    Join Date
    Dec 2012
    Posts
    197
    Thanks
    25
    Thanked 41 Times in 33 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to get database id value from QComboBox text?

    Obviously the last post by anda_skoa.

Similar Threads

  1. QComboBox text misaligned
    By jiveaxe in forum Newbie
    Replies: 2
    Last Post: 29th March 2013, 11:14
  2. Get text from QCombobox.
    By dragon in forum Newbie
    Replies: 13
    Last Post: 31st January 2012, 20:50
  3. How to use rich text in a QComboBox?
    By aarunt1 in forum Qt Programming
    Replies: 3
    Last Post: 19th March 2010, 20:20
  4. QComboBox with fixed text?
    By squidge in forum Qt Programming
    Replies: 0
    Last Post: 14th November 2009, 21:39
  5. Bind a QComboBox to a Database
    By onefootswill in forum Newbie
    Replies: 4
    Last Post: 16th August 2008, 13:26

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
  •  
Qt is a trademark of The Qt Company.