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. [Solved] Mapping a column from a table to a QComboBox
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved General and Desktop
10 Posts 3 Posters 7.9k 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.
  • E Offline
    E Offline
    Edico
    wrote on last edited by
    #1

    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!

    1 Reply Last reply
    0
    • P Offline
      P Offline
      p91paul
      wrote on last edited by
      #2

      @combo->setModel(yourModel);
      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
      @yourModel=modelToMap->relationModel(<foreing key column>);@

      1 Reply Last reply
      0
      • E Offline
        E Offline
        Edico
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • P Offline
          P Offline
          p91paul
          wrote on last edited by
          #4

          you can do an "insertRow(0)":http://doc.qt.nokia.com/4.7/qabstractitemmodel.html#insertRow on yourModel. you have to pay attention not to submit changes to the underlying db.

          @yourModel->setEditStrategy(QSqlTableModel::OnManualSubmit);@

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

          1 Reply Last reply
          0
          • G Offline
            G Offline
            giesbert
            wrote on last edited by
            #5

            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.

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

            1 Reply Last reply
            0
            • P Offline
              P Offline
              p91paul
              wrote on last edited by
              #6

              [quote author="p91paul" date="1311080997"]you have to pay attention not to submit changes to the underlying db[/quote]

              I have said this =D

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

              1 Reply Last reply
              0
              • E Offline
                E Offline
                Edico
                wrote on last edited by
                #7

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

                @
                model = new QSqlRelationalTableModel(this);
                model->setTable("Produse");
                model->setEditStrategy(QSqlTableModel::OnManualSubmit);
                model->select();
                proxyModel = new QSortFilterProxyModel;
                proxyModel->setSourceModel(model);
                proxyModel->setFilterKeyColumn(model->fieldIndex("Denumire"));
                denumireCBox->setModel(proxyModel);
                denumireCBox->setModelColumn(proxyModel->filterKeyColumn());
                @

                1 Reply Last reply
                0
                • P Offline
                  P Offline
                  p91paul
                  wrote on last edited by
                  #8

                  you have to add proxyModel->insertRow();

                  1 Reply Last reply
                  0
                  • G Offline
                    G Offline
                    giesbert
                    wrote on last edited by
                    #9

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

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

                    1 Reply Last reply
                    0
                    • E Offline
                      E Offline
                      Edico
                      wrote on last edited by
                      #10

                      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!

                      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