July 13, 2011

Edico Edico
Lab Rat
31 posts

[Solved] Mapping a column from a table to a QComboBox

 

Hi!
I have a table in a database and I want a column’s content be shown in a QComboBox.
I’ve seen examples using a QDataWidgetMapper for this but there is another table.
I’ve thought creating another table and create a relation with a foreign key and then to follow the strategy in sql examples for mapping
the column to QComboBox.
Is there a way for mapping the content of a table’s column to a QComboBox without creating another table?

Thanks!

9 replies

July 13, 2011

p91paul p91paul
Lab Rat
33 posts

  1. combo->setModel(yourModel);
  2. combo->setModelColumn(columnToDisplay);

if you want to map in this combo box the column of an external table, your model must be a QSqlRelationalTableModel, you have to set relations properly (see the docs) then you can get the external table with

  1. yourModel=modelToMap->relationModel(<foreing key column>);

July 19, 2011

Edico Edico
Lab Rat
31 posts

How can I make the first item to be empty and the rest of items to start after the empty one?

July 19, 2011

p91paul p91paul
Lab Rat
33 posts

you can do an insertRow [doc.qt.nokia.com] on yourModel. you have to pay attention not to submit changes to the underlying db.

  1. yourModel->setEditStrategy(QSqlTableModel::OnManualSubmit);

if you set this, the db won’t be modified unless you call yourModel->submitAll();

July 19, 2011

Gerolf Gerolf
Robot Herder
3252 posts

But with a submit, the row will be written to the DB :-(
The other option would be to use a proxy model, which adds this empty row.

 Signature 

Nokia Certified Qt Specialist.
Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

July 19, 2011

p91paul p91paul
Lab Rat
33 posts
p91paul wrote:
you have to pay attention not to submit changes to the underlying db

I have said this =D

obviously your idea is better, gerolf, cause original model is not touched.

July 20, 2011

Edico Edico
Lab Rat
31 posts

I didn’t made it. I did like that and doesn’t work:

  1.  
  2.     model = new QSqlRelationalTableModel(this);
  3.     model->setTable("Produse");
  4.     model->setEditStrategy(QSqlTableModel::OnManualSubmit);
  5.     model->select();
  6.     proxyModel = new QSortFilterProxyModel;
  7.     proxyModel->setSourceModel(model);
  8.     proxyModel->setFilterKeyColumn(model->fieldIndex("Denumire"));
  9.     denumireCBox->setModel(proxyModel);
  10.     denumireCBox->setModelColumn(proxyModel->filterKeyColumn());

July 20, 2011

p91paul p91paul
Lab Rat
33 posts

you have to add proxyModel->insertRow();

July 20, 2011

Gerolf Gerolf
Robot Herder
3252 posts

What would you like and did not work? Creating an own proxy model?

 Signature 

Nokia Certified Qt Specialist.
Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

July 20, 2011

Edico Edico
Lab Rat
31 posts

It works fine now. First I’ve understood that a proxy model automatically ads an extra row.
Now I added proxyModel->insertRow(0); and it works like I want.

Thank you very much!

 
  ‹‹ [solved]Mapping a QComboBox displaying multiple columns      QUiLoader’s method "load" used in custom widget results in "Segmentation fault" in Linguist ››

You must log in to post a reply. Not a member yet? Register here!