June 28, 2011

Luca Luca
Ant Farmer
589 posts

QDataWidgetMapper && QSqlRelationalTableModel && QComboBox

 

Hi all,
I need to use a QDataWidgetMapper to show data from an Sql table:

  1. tableModel = new QSqlTableModel()
  2. tableModel->setTable(TABELLA);
  3. ...
  4. ...
  5. dataWidgetMapper = new QDataWidgetMapper(this);
  6. dataWidgetMapper->setModel(tableModel);
  7. dataWidgetMapper->addMapping(ui->idEdit, tableModel->fieldIndex("id"));
  8. dataWidgetMapper->addMapping(ui->descrizioneEdit, tableModel->fieldIndex("descrizione"));
  9. dataWidgetMapper->addMapping(ui->nomeEdit, tableModel->fieldIndex("nome"));
  10. dataWidgetMapper->addMapping(ui->menuEdit, tableModel->fieldIndex("menu"));
  11. ...
  12. ...

This part works fine. Now I’d like to show the “available” menu in a QComboBox (and not in QLineEdit) because there is a vs_menu sql table that contain all the “menu name”.

I tried this:

  1. tableModel = new QSqlRelationalTableModel();
  2. tableModel->setTable(TABELLA);
  3. ...
  4. tableModel->setRelation(3, QSqlRelation("vs_menu", "nome", "nome"));
  5. ...
  6. ...
  7. dataWidgetMapper->addMapping(ui->idEdit, tableModel->fieldIndex("id"));
  8. dataWidgetMapper->addMapping(ui->descrizioneEdit, tableModel->fieldIndex("descrizione"));
  9. dataWidgetMapper->addMapping(ui->nomeEdit, tableModel->fieldIndex("nome"));
  10. dataWidgetMapper->addMapping(ui->menuComboBox, tableModel->fieldIndex("menu"));
  11. dataWidgetMapper->setItemDelegate(new QSqlRelationalDelegate(this));

but the combo isn’t populated…

What do I wrong…?

9 replies

June 29, 2011

maxoreli maxoreli
Lab Rat
46 posts

you must know that “tableModel->setTable(TABELLA); “this method does not select data from the table, but fetches its field information.For selecting data from your table, apply “tableModel->select()”.
Try that and wait for seeing results.

Luca wrote:
Hi all,
I need to use a QDataWidgetMapper to show data from an Sql table:
  1. tableModel = new QSqlTableModel()
  2. tableModel->setTable(TABELLA);
  3.  
  4. dataWidgetMapper = new QDataWidgetMapper(this);
  5. dataWidgetMapper->setModel(tableModel);
  6. dataWidgetMapper->addMapping(ui->idEdit, tableModel->fieldIndex("id"));
  7. dataWidgetMapper->addMapping(ui->descrizioneEdit, tableModel->fieldIndex("descrizione"));
  8. dataWidgetMapper->addMapping(ui->nomeEdit, tableModel->fieldIndex("nome"));
  9. dataWidgetMapper->addMapping(ui->menuEdit, tableModel->fieldIndex("menu"));
  10. ...
  11. ...

This part works fine. Now I’d like to show the “available” menu in a QComboBox (and not in QLineEdit) because there is a vs_menu sql table that contain all the “menu name”.

I tried this:

  1. tableModel = new QSqlRelationalTableModel();
  2. tableModel->setTable(TABELLA);
  3. tableModel->setRelation(3, QSqlRelation("vs_menu", "nome", "nome"));
  4. ...
  5. ...
  6. dataWidgetMapper->addMapping(ui->idEdit, tableModel->fieldIndex("id"));
  7. dataWidgetMapper->addMapping(ui->descrizioneEdit, tableModel->fieldIndex("descrizione"));
  8. dataWidgetMapper->addMapping(ui->nomeEdit, tableModel->fieldIndex("nome"));
  9. dataWidgetMapper->addMapping(ui->menuComboBox, tableModel->fieldIndex("menu"));
  10. dataWidgetMapper->setItemDelegate(new QSqlRelationalDelegate(this));

but the combo isn’t populated…

What do I wrong…?

June 29, 2011

Luca Luca
Ant Farmer
589 posts

Thanks maxoreli,

I didn’t write all code but I already used tableModel->select() . And I can see data in my widget except for QComboBox.

June 29, 2011

sidewinder sidewinder
Ant Farmer
65 posts

What about QComboBox’s setModel() and setModelColumn() functions? Maybe you can use QComboBox without QDataWidgetMapper and connect signals from QComboBox to navigate in QDataWidgetMapper.

 Signature 

“Never memorize what you can look up in books.”
Albert Einstein

June 29, 2011

Luca Luca
Ant Farmer
589 posts
sidewinder wrote:
What about QComboBox’s setModel() and setModelColumn() functions? Maybe you can use QComboBox without QDataWidgetMapper and connect signals from QComboBox to navigate in QDataWidgetMapper.

There are a lot of possible solution but I’d like to know if it’s possible to use a QDataWidgetMapper in conjunction with QSqlRelationalTableModel to populate a QComboBox and to select the right item in the QComboBox while navigating the table.

June 29, 2011

Luca Luca
Ant Farmer
589 posts

So it isn’t possible?

For now I solved populating the combo in the constructor and positioning the right element by hand while navigating.

Now what when I save with:

  1. tableModel->submitAll();

How can I manage the right element in the combo to be saved in the DB?

July 28, 2011

pate pate
Lab Rat
2 posts

After addMapping to QComboBox,you should do this:

  1.  QSqlTableModel *relationalModel = tableModel->relationModel(i);
  2.    ui->menuComboBox->setModel(relationalModel);
  3.    ui->menuComboBox->setModelColumn(relationalModel->fieldIndex("menu");

July 28, 2011

Luca Luca
Ant Farmer
589 posts

pate wrote:
After addMapping to QComboBox,you should do this:

  1.  QSqlTableModel *relationalModel = tableModel->relationModel(i);
  2.    ui->menuComboBox->setModel(relationalModel);
  3.    ui->menuComboBox->setModelColumn(relationalModel->fieldIndex("menu");

What is “i” in :

  1. QSqlTableModel *relationalModel = tableModel->relationModel(i);

?

July 28, 2011

Luca Luca
Ant Farmer
589 posts

Ok, now I understand what you mean.

I tried this, now the combo get populated but if I navigate the table with:

  1. dataWidgetMapper->toNext();

the combo doesn’t change showed item…

Is it possible to do that or I must do it by hand?

July 29, 2011

pate pate
Lab Rat
2 posts

Luca wrote:
pate wrote:
After addMapping to QComboBox,you should do this:

  1.  QSqlTableModel *relationalModel = tableModel->relationModel(i);
  2.    ui->menuComboBox->setModel(relationalModel);
  3.    ui->menuComboBox->setModelColumn(relationalModel->fieldIndex("menu");

What is “i” in :

  1. QSqlTableModel *relationalModel = tableModel->relationModel(i);

?

Sorry!It’s the index of the field “menu”.
Here it should be

  1. QSqlTableModel *relationalModel = tableModel->relationModel(tableModel->fieldIndex("menu"));

 
  ‹‹ [Solved] Strange error      Using Signals and Slots with Touch Events ››

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