PDA

View Full Version : How to get database id value from QComboBox text?



ohmyqt
29th September 2013, 12:47
Hello all,

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



ID Text
----------------
1 Apple
4 Banana
6 Mango
8 Rambutan
19 Durian


I got a QComboBox to populate with the right text using QSqlRelationalTableModel and QSqlTableModel:



QSqlRelationalTableModel *rtmodel = new QSqlRelationalTableModel();

// etc etc... populate database, set relations as per example on help files etc...

QSqlTableModel * relModel = rtmodel->relationModel(typeIndex);
ui->myComboBox->setModel(relModel);
ui->myComboBox->setModelColumn(relModel->fieldIndex("Text"));


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.

toufic.dbouk
29th September 2013, 13:54
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.

ohmyqt
29th September 2013, 14:05
Hi. Thanks for the response. Unfortunately, I'm getting this error...



error: no matching function for call to 'QSqlTableModel::index(int)'
qDebug() << relModel->data(relModel->index(0));
^


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?

toufic.dbouk
29th September 2013, 14:45
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.

anda_skoa
30th September 2013, 09:04
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,
_

toufic.dbouk
30th September 2013, 14:44
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

avpro
25th November 2013, 18:16
hi,
I have the same problem.
so, which is the correct solution?
thanks.

toufic.dbouk
25th November 2013, 18:23
Obviously the last post by anda_skoa.