QDataWidgetMapper && QSqlRelationalTableModel && QComboBox
Hi all,
I need to use a QDataWidgetMapper to show data from an Sql table:
- tableModel->setTable(TABELLA);
- ...
- ...
- dataWidgetMapper->setModel(tableModel);
- dataWidgetMapper->addMapping(ui->idEdit, tableModel->fieldIndex("id"));
- dataWidgetMapper->addMapping(ui->descrizioneEdit, tableModel->fieldIndex("descrizione"));
- dataWidgetMapper->addMapping(ui->nomeEdit, tableModel->fieldIndex("nome"));
- dataWidgetMapper->addMapping(ui->menuEdit, tableModel->fieldIndex("menu"));
- ...
- ...
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:
- tableModel->setTable(TABELLA);
- ...
- ...
- ...
- dataWidgetMapper->addMapping(ui->idEdit, tableModel->fieldIndex("id"));
- dataWidgetMapper->addMapping(ui->descrizioneEdit, tableModel->fieldIndex("descrizione"));
- dataWidgetMapper->addMapping(ui->nomeEdit, tableModel->fieldIndex("nome"));
- dataWidgetMapper->addMapping(ui->menuComboBox, tableModel->fieldIndex("menu"));
but the combo isn’t populated…
What do I wrong…?
9 replies
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.
Hi all,
I need to use a QDataWidgetMapper to show data from an Sql table:
tableModel->setTable(TABELLA); dataWidgetMapper->setModel(tableModel); dataWidgetMapper->addMapping(ui->idEdit, tableModel->fieldIndex("id")); dataWidgetMapper->addMapping(ui->descrizioneEdit, tableModel->fieldIndex("descrizione")); dataWidgetMapper->addMapping(ui->nomeEdit, tableModel->fieldIndex("nome")); dataWidgetMapper->addMapping(ui->menuEdit, tableModel->fieldIndex("menu")); ... ...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:
tableModel->setTable(TABELLA); ... ... dataWidgetMapper->addMapping(ui->idEdit, tableModel->fieldIndex("id")); dataWidgetMapper->addMapping(ui->descrizioneEdit, tableModel->fieldIndex("descrizione")); dataWidgetMapper->addMapping(ui->nomeEdit, tableModel->fieldIndex("nome")); dataWidgetMapper->addMapping(ui->menuComboBox, tableModel->fieldIndex("menu"));but the combo isn’t populated…
What do I wrong…?
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.
You must log in to post a reply. Not a member yet? Register here!


