PDA

View Full Version : QComboBox QSqlQueryModel



aekilic
16th December 2008, 17:16
Dear All

We setup our combo like this.


QSqlQueryModel *modKod = new QSqlQueryModel();
modKod->setQuery("SELECT kod, id FROM stok_kod UNION SELECT '', null ORDER BY kod");
comboStokKod->setModel(modKod);
comboStokKod->setModelColumn(0);
comboStokKod->setEditable(true);

But after that we get a value from another query that id = 5, after that I would like to change the combo to value id = 5?. Anyway I could do it?

jpn
16th December 2008, 20:21
Try using QAbstractItemModel::match() to find the appropriate QModelIndex.

aekilic
16th December 2008, 21:02
We normaly do it like



combo->setCurrentIndex(combo->findData(sqlquery.value(8)));


But in this case it should find it from a model?

jpn
16th December 2008, 21:08
Well you're interested in searching the model column that combo box is presenting, but another model column, right?

aekilic
16th December 2008, 21:23
yes

what we have in the combo is

id and the name,

we show the name and hide the id, but also save the id.

But when we call the widget we create our combo and fill inside. Afther that we run a query and get the id from the query. We would like to find the same id in the query id.

jpn
16th December 2008, 21:28
what we have in the combo is

id and the name
Both? I thought "kod" was the name.


we show the name and hide the id, but also save the id.
What do you mean with saving the id? Isn't it already available in the second column?



Afther that we run a query and get the id from the query. We would like to find the same id in the query id.
Yeah, that's what I suggested the match() for...

aekilic
16th December 2008, 21:34
Let me explain with examples



QSqlQueryModel *modKod = new QSqlQueryModel();
modKod->setQuery("SELECT kod, id FROM stok_kod ");
comboStokKod->setModel(modKod);
comboStokKod->setModelColumn(0);
comboStokKod->setEditable(true);


We create our combo like this.

kod1, 1
kod2, 2
kod3, 3


and after that we get another value from another query for the id of the query. So if we have 2 for the id, we try to set the combo text kod2.

jpn
16th December 2008, 21:53
int id = 2;
QModelIndex index = modKod->index(0, 1); // column 1
QModelIndexList result = modKod->match(index, Qt::EditRole, id, 1, Qt::MatchExactly);
int row = result.value(0).row(); // might be -1
comboStokKod->setCurrentRow(row);

aekilic
17th December 2008, 13:01
Thank you very much guys,

I have solved it like this

Thank you jpn...


QModelIndexList result = comboStokKod->model()->match(comboStokKod->model()->index(0, 1), Qt::EditRole, query.value(3), 1, Qt::MatchExactly);
comboStokKod->setCurrentIndex(result.value(0).row());