PDA

View Full Version : Two way data map?



chimby
16th January 2009, 22:37
I am fairly new to QT so I have what might be an easy question.

I have a QComboBox that needs to be populated by a table in my database. I would like to be able to programmatically select the combo box by the table ID from the database. Of course the combobox index does not match the table ID's. To solve that problem I created QMap<TableID, ComboBoxIndex>. Now I can programmatically select the correct value in the combo box by using the map. However, I don't know how to do a "reverse" map to get the TableID by looking at the ComboBoxIndex. Arrrr. Is this possible? Am I not approaching this correctly?

thanks.

jpn
16th January 2009, 22:42
You could use an SQL model directly in the QComboBox or even use a QDataWidgetMapper.

chimby
16th January 2009, 23:30
At one point I had an SQL model directly attached, but I couldn't get at the table ID of the model. The displayed text in the combo box came from column 1 and the table ID was in column 0. Is there a way to get at the table ID in column 0 that I can't figure out?.... I just found a way, but it is ugly. Here it is...is there a better way?



int row = ui.comboBox->currentIndex();
QModelIndex idx = ui.comboBox->model()->index(row, 0);

int id = ui.comboBox->model()->data(idx).toInt();
qDebug() << id;

jpn
16th January 2009, 23:34
At one point I had an SQL model directly attached, but I couldn't get at the table ID of the model. The displayed text in the combo box came from column 1 and the table ID was in column 0. Is there a way to get at the table ID in column 0 that I can't figure out?
See QComboBox::setModelColumn().