QDataWidgetMapper && QSqlRelationalTableModel && QComboBox



  • Hi all,
    I need to use a QDataWidgetMapper to show data from an Sql table:
    @
    tableModel = new QSqlTableModel()
    tableModel->setTable(TABELLA);
    ...
    ...
    dataWidgetMapper = new QDataWidgetMapper(this);
    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 = new QSqlRelationalTableModel();
    tableModel->setTable(TABELLA);
    ...
    tableModel->setRelation(3, QSqlRelation("vs_menu", "nome", "nome"));
    ...
    ...
    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"));
    dataWidgetMapper->setItemDelegate(new QSqlRelationalDelegate(this));
    @

    but the combo isn't populated...

    What do I wrong...?



  • 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.
    [quote author="Luca" date="1309294719"]Hi all,
    I need to use a QDataWidgetMapper to show data from an Sql table:
    @
    tableModel = new QSqlTableModel()
    tableModel->setTable(TABELLA);

    dataWidgetMapper = new QDataWidgetMapper(this);
    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 = new QSqlRelationalTableModel();
    tableModel->setTable(TABELLA);
    tableModel->setRelation(3, QSqlRelation("vs_menu", "nome", "nome"));
    ...
    ...
    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"));
    dataWidgetMapper->setItemDelegate(new QSqlRelationalDelegate(this));
    @

    but the combo isn't populated...

    What do I wrong...?[/quote]



  • 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.



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



  • [quote author="sidewinder" date="1309332836"]What about QComboBox's setModel() and setModelColumn() functions? Maybe you can use QComboBox without QDataWidgetMapper and connect signals from QComboBox to navigate in QDataWidgetMapper.[/quote]

    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.



  • 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:
    @
    tableModel->submitAll();
    @
    How can I manage the right element in the combo to be saved in the DB?



  • After addMapping to QComboBox,you should do this:

    @ QSqlTableModel *relationalModel = tableModel->relationModel(i);
    ui->menuComboBox->setModel(relationalModel);
    ui->menuComboBox->setModelColumn(relationalModel->fieldIndex("menu"); @



  • [quote author="pate" date="1311819665"]After addMapping to QComboBox,you should do this:

    @ QSqlTableModel *relationalModel = tableModel->relationModel(i);
    ui->menuComboBox->setModel(relationalModel);
    ui->menuComboBox->setModelColumn(relationalModel->fieldIndex("menu");
    @
    [/quote]

    What is "i" in :
    @
    QSqlTableModel *relationalModel = tableModel->relationModel(i);
    @
    ?



  • Ok, now I understand what you mean.

    I tried this, now the combo get populated but if I navigate the table with:
    @
    dataWidgetMapper->toNext();
    @

    the combo doesn't change showed item...

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



  • [quote author="Luca" date="1311879787"][quote author="pate" date="1311819665"]After addMapping to QComboBox,you should do this:

    @ QSqlTableModel *relationalModel = tableModel->relationModel(i);
    ui->menuComboBox->setModel(relationalModel);
    ui->menuComboBox->setModelColumn(relationalModel->fieldIndex("menu");
    @
    [/quote]

    What is "i" in :
    @
    QSqlTableModel *relationalModel = tableModel->relationModel(i);
    @
    ?[/quote]

    Sorry!It's the index of the field "menu".
    Here it should be
    @
    QSqlTableModel *relationalModel = tableModel->relationModel(tableModel->fieldIndex("menu"));@


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.