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. Update combobox data from model
Forum Updated to NodeBB v4.3 + New Features

Update combobox data from model

Scheduled Pinned Locked Moved Solved General and Desktop
qcomboboxqsqltablemodel
8 Posts 3 Posters 4.1k 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.
  • rudagR Offline
    rudagR Offline
    rudag
    wrote on last edited by
    #1

    I have a categories QComboBox which is linked to a QSqlTableModel. Also I have a second window to manage categories (add/remove). How to update/refresh/reload the combobox whenever a category is added/removed?

    My code:

    int catIndex = model2->fieldIndex("category");
    model2->setRelation(catIndex, QSqlRelation("categories", "id", "name"));
    QSqlTableModel *catModel = model2->relationModel(catIndex);
    ui->comboBox_2->setModel(catModel);
    ui->comboBox_2->setModelColumn(catModel->fieldIndex("name"));
    mapper->addMapping(ui->comboBox_2, catIndex);
    
    m.sueM 1 Reply Last reply
    0
    • rudagR rudag

      I have a categories QComboBox which is linked to a QSqlTableModel. Also I have a second window to manage categories (add/remove). How to update/refresh/reload the combobox whenever a category is added/removed?

      My code:

      int catIndex = model2->fieldIndex("category");
      model2->setRelation(catIndex, QSqlRelation("categories", "id", "name"));
      QSqlTableModel *catModel = model2->relationModel(catIndex);
      ui->comboBox_2->setModel(catModel);
      ui->comboBox_2->setModelColumn(catModel->fieldIndex("name"));
      mapper->addMapping(ui->comboBox_2, catIndex);
      
      m.sueM Offline
      m.sueM Offline
      m.sue
      wrote on last edited by
      #2

      Hi @rudag

      after adding/removing in your second window send a signal to a slot of the object where you call "your code" and in the slot call "your code" again.

      -Michael.

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        How are you adding / removing these categories ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        1
        • rudagR Offline
          rudagR Offline
          rudag
          wrote on last edited by
          #4

          @m-sue
          I can call the main window from the categories window. Problem is I don't know the code to reload the QComboBox.

          @SGaist
          It's another window which has a QTableView with a QSqlTableModel linked to the categories table.

          m.sueM 1 Reply Last reply
          0
          • rudagR rudag

            @m-sue
            I can call the main window from the categories window. Problem is I don't know the code to reload the QComboBox.

            @SGaist
            It's another window which has a QTableView with a QSqlTableModel linked to the categories table.

            m.sueM Offline
            m.sueM Offline
            m.sue
            wrote on last edited by m.sue
            #5

            Hi @rudag

            I do not understand. You wrote the code in your first post. Just call it again. Then the changed model should refill the combobox.

            If you want the model to update your combobox, though, you will probably have to derive your own model class and override the functions for update and remove of data sets.

            -Michael.

            1 Reply Last reply
            0
            • rudagR Offline
              rudagR Offline
              rudag
              wrote on last edited by
              #6

              @m-sue
              I'm getting the following error when I try to call that piece of code more than once:
              QComboBox::setModel: cannot set a 0 model

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                Then why not share the model between the widgets ?

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                2
                • rudagR Offline
                  rudagR Offline
                  rudag
                  wrote on last edited by
                  #8

                  I did this:

                  ui->comboBox->clear();
                  ((QSqlTableModel*)ui->comboBox->model())->select();
                  

                  And it's working!

                  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