View Full Version : QComboBox with QSqlTableModel
Havoc][
14th June 2009, 23:51
Hello,
i have QSqlTableModel set to a Table with two columns (id, name). Now i want to have this Table "in my QComboBox". I found some ways to have the ID in the ComboBox with QSqlRelationDelegate - but i have it in one table (so i dont need to setup a relation between two tables).
Can some tell me, how i can equalize the combobox-index and the model-id?
I played with QDataWidgetMapper and with the combobox-function setRootModelIndex() but it doesnt worked yet.
Thanks a lot.
Havoc][
nish
15th June 2009, 06:44
make another model which will return one coloum only.... this would be less mess.
Havoc][
15th June 2009, 09:16
But i want to show the names, and not the IDs? What should i do with the second (new) model with only the ID-Column?
Thanks and Regards.
Havoc][
nish
15th June 2009, 09:31
dont show the id col... show the names....
model view prog has three parts..
data
model
view
so different model can present different view of same data...
Havoc][
15th June 2009, 09:49
okay, i tried to set the combobox-view to another model:
void zeige_kategorien_combobox(QComboBox *cbanzeige)
{
if ( datenbankverbindung )
{
QSqlTableModel *kategorie_combobox_model = new QSqlTableModel;
kategorie_combobox_model->setTable("kategorie");
kategorie_combobox_model->setEditStrategy(QSqlTableModel::OnManualSubmit);
kategorie_combobox_model->select();
QSqlTableModel *kategorie_combobox_model2 = new QSqlTableModel;
kategorie_combobox_model2->setTable("kategorie");
kategorie_combobox_model2->setEditStrategy(QSqlTableModel::OnManualSubmit);
kategorie_combobox_model2->select();
cbanzeige->setModel(kategorie_combobox_model);
kategorie_combobox_model2->removeColumn(0);
cbanzeige->view()->setModel(kategorie_combobox_model2);
}
}
But that didnt worked.
Is there no easy way to set one column of a model to the Indexes, and one Column of a model to the shown names?
Thanks,
Havoc][
nish
15th June 2009, 10:00
qt assistant provides the example as
QSqlTableModel *model = new QSqlTableModel;
model->setTable("employee");
model->setEditStrategy(QSqlTableModel::OnManualSubmit);
model->select();
model->removeColumn(0); // don't show the ID
model->setHeaderData(0, Qt::Horizontal, tr("Name"));
model->setHeaderData(1, Qt::Horizontal, tr("Salary"));
QTableView *view = new QTableView;
view->setModel(model);
view->show();
i dono why its not working for you..
Havoc][
15th June 2009, 10:04
Yes, *THATS* working - of course. But in this case i only have the names OR the IDs. And i would have to show the names, and give back the ID. So i tought i have to set the combobox indexes to my IDs.
nish
15th June 2009, 10:12
let me tell what i understood...
u have a table like
ID NAME
1 Name1
2 Name2
now u want a combobox to show
Name1 --> combobox index 0
Name2 --> combobox index 1
rite??
Havoc][
15th June 2009, 10:16
Not exactly :-).
I have a table:
ID NAME
101 name1
102 name2
103 name3
104 name4
And i want to show NAME in my ComboBox. And i someone set it to name2 i want to get "102" (the ID) :-).
nish
15th June 2009, 10:28
bit tricky..
i think you need to subclass QSqlTableModel and/or QCombobox... and define your own Qt::userrole on the ids
QCombobox::itemData() can fetch the id...
Havoc][
15th June 2009, 11:20
Okay - i think, to subclass and create my own display-type is very circuitous for this case.
I asked in #qt (freenode) and daff told me that i could use the combobox index to get the data from my model at the same index :-).
So i did:
model->index(ui->combobox->currentIndex(), 0).data(0).toString().toInt()
That works for me.
Thanks anyway!
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.