Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QDataWidgetMapper && QSqlRelationalTableModel && QComboBox
Forum Updated to NodeBB v4.3 + New Features

QDataWidgetMapper && QSqlRelationalTableModel && QComboBox

Scheduled Pinned Locked Moved General and Desktop
10 Posts 4 Posters 7.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    luca
    wrote on last edited by
    #1

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

    1 Reply Last reply
    0
    • M Offline
      M Offline
      maxoreli
      wrote on last edited by
      #2

      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]

      1 Reply Last reply
      0
      • L Offline
        L Offline
        luca
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • S Offline
          S Offline
          sidewinder
          wrote on last edited by
          #4

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

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

          1 Reply Last reply
          0
          • L Offline
            L Offline
            luca
            wrote on last edited by
            #5

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

            1 Reply Last reply
            0
            • L Offline
              L Offline
              luca
              wrote on last edited by
              #6

              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?

              1 Reply Last reply
              0
              • P Offline
                P Offline
                pate
                wrote on last edited by
                #7

                After addMapping to QComboBox,you should do this:

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

                1 Reply Last reply
                0
                • L Offline
                  L Offline
                  luca
                  wrote on last edited by
                  #8

                  [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);
                  @
                  ?

                  1 Reply Last reply
                  0
                  • L Offline
                    L Offline
                    luca
                    wrote on last edited by
                    #9

                    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?

                    1 Reply Last reply
                    0
                    • P Offline
                      P Offline
                      pate
                      wrote on last edited by
                      #10

                      [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"));@

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved