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. Conclusion of fixed lists in the editing table. Qt 6.2.1
Forum Update on Monday, May 27th 2025

Conclusion of fixed lists in the editing table. Qt 6.2.1

Scheduled Pinned Locked Moved Solved General and Desktop
qt6
20 Posts 6 Posters 1.3k Views
  • 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.
  • M MyNick 0

    I have a "status" field in the database.
    This is a fixed list of values that is stored in the program code in the form of an array:

    QMap<int, QString> map;
    map[1] = "Show";
    map[2] = "Hide";
    map[5] = "Moderation";
    

    In the base in the field, the values are chopped in the form of numbers 1, 2, 5, etc.

    And I have a QSQLTableModel model:

    model = new QSqlTableModel;
    
    model->setTable("TABLE");
    model->setEditStrategy(QSqlTableModel::OnFieldChange);
    model->setHeaderData(0, Qt::Horizontal, tr("id"));
    model->setHeaderData(1, Qt::Horizontal, tr("Status"));
    model->setHeaderData(2, Qt::Horizontal, tr("Name"));
    
    ui->tableView->setModel(model);
    model->select();
    

    How to make sure that when the data of these numbers from the database in the ui->statusComboBox field, they are replaced by map values
    "Show", "Hide", "Moderation"?

    M Offline
    M Offline
    MyNick 0
    wrote on last edited by
    #3

    @MyNick-0 I do not need to update map, but I need to display the values, "Show", "Hide", "Moderation" in the ui->ComboBox field.

    The map array indexs is equal to the values of the "status" database.

    1 Reply Last reply
    0
    • M MyNick 0

      I have a "status" field in the database.
      This is a fixed list of values that is stored in the program code in the form of an array:

      QMap<int, QString> map;
      map[1] = "Show";
      map[2] = "Hide";
      map[5] = "Moderation";
      

      In the base in the field, the values are chopped in the form of numbers 1, 2, 5, etc.

      And I have a QSQLTableModel model:

      model = new QSqlTableModel;
      
      model->setTable("TABLE");
      model->setEditStrategy(QSqlTableModel::OnFieldChange);
      model->setHeaderData(0, Qt::Horizontal, tr("id"));
      model->setHeaderData(1, Qt::Horizontal, tr("Status"));
      model->setHeaderData(2, Qt::Horizontal, tr("Name"));
      
      ui->tableView->setModel(model);
      model->select();
      

      How to make sure that when the data of these numbers from the database in the ui->statusComboBox field, they are replaced by map values
      "Show", "Hide", "Moderation"?

      Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #4

      @MyNick-0 said in Conclusion of fixed lists in the editing table. Qt 6.2.1:

      How to make sure that when the data of these numbers from the database in the ui->statusComboBox field, they are replaced by map values

      Please rephrase your question.
      I assume you've a single combobox so you can simply add those three values in there and set the key as user data.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      M 1 Reply Last reply
      0
      • Christian EhrlicherC Christian Ehrlicher

        @MyNick-0 said in Conclusion of fixed lists in the editing table. Qt 6.2.1:

        How to make sure that when the data of these numbers from the database in the ui->statusComboBox field, they are replaced by map values

        Please rephrase your question.
        I assume you've a single combobox so you can simply add those three values in there and set the key as user data.

        M Offline
        M Offline
        MyNick 0
        wrote on last edited by
        #5

        @Christian-Ehrlicher No, I have no Combobox field yet.
        I need to somehow create it in my "model" model.

        Christian EhrlicherC 1 Reply Last reply
        0
        • M MyNick 0

          @Christian-Ehrlicher No, I have no Combobox field yet.
          I need to somehow create it in my "model" model.

          Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #6

          @MyNick-0 said in Conclusion of fixed lists in the editing table. Qt 6.2.1:

          I need to somehow create it in my "model" model.

          A model does not display anything. When you want a combobox in your view then you should take a look at https://doc.qt.io/qt-6/qstyleditemdelegate.html

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          SGaistS M 2 Replies Last reply
          0
          • Christian EhrlicherC Christian Ehrlicher

            @MyNick-0 said in Conclusion of fixed lists in the editing table. Qt 6.2.1:

            I need to somehow create it in my "model" model.

            A model does not display anything. When you want a combobox in your view then you should take a look at https://doc.qt.io/qt-6/qstyleditemdelegate.html

            SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #7

            Hi,

            Are these values coming from a different table ?

            You should check QSqlRelationalTableModel.

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

            M 1 Reply Last reply
            0
            • SGaistS SGaist

              Hi,

              Are these values coming from a different table ?

              You should check QSqlRelationalTableModel.

              M Offline
              M Offline
              MyNick 0
              wrote on last edited by
              #8

              @SGaist Hi,

              No, they are originally chopped in the program in the program in ```
              QMap<int, QString> map;

              1 Reply Last reply
              0
              • Christian EhrlicherC Christian Ehrlicher

                @MyNick-0 said in Conclusion of fixed lists in the editing table. Qt 6.2.1:

                I need to somehow create it in my "model" model.

                A model does not display anything. When you want a combobox in your view then you should take a look at https://doc.qt.io/qt-6/qstyleditemdelegate.html

                M Offline
                M Offline
                MyNick 0
                wrote on last edited by
                #9

                @Christian-Ehrlicher I don’t understand how to do it, I need an example

                jsulmJ 1 Reply Last reply
                0
                • M MyNick 0

                  @Christian-Ehrlicher I don’t understand how to do it, I need an example

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #10

                  @MyNick-0 https://doc.qt.io/qt-6/qtwidgets-itemviews-spinboxdelegate-example.html
                  https://doc.qt.io/qt-6/qtwidgets-itemviews-stardelegate-example.html

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  M 1 Reply Last reply
                  0
                  • jsulmJ jsulm

                    @MyNick-0 https://doc.qt.io/qt-6/qtwidgets-itemviews-spinboxdelegate-example.html
                    https://doc.qt.io/qt-6/qtwidgets-itemviews-stardelegate-example.html

                    M Offline
                    M Offline
                    MyNick 0
                    wrote on last edited by
                    #11

                    @jsulm Hi,

                    Now my code looks like this:

                    statusdelegat.h

                    #ifndef STATUSDELEGAT_H
                    #define STATUSDELEGAT_H
                    
                    #include <QStyledItemDelegate>
                    #include <QObject>
                    #include <QComboBox>
                    
                    class statusDelegat : public QStyledItemDelegate
                    {
                        Q_OBJECT
                    public:
                        explicit statusDelegat(QObject *parent = nullptr);
                    
                        QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
                    
                        void setEditorData(QWidget *editor, const QModelIndex &index) const override;
                    
                    };
                    
                    #endif // STATUSDELEGAT_H
                    

                    statusdelegat.cpp

                    #include "statusdelegat.h"
                    
                    statusDelegat::statusDelegat(QObject *parent)
                        : QStyledItemDelegate{parent}
                    {
                    
                    }
                    
                    QWidget *statusDelegat::createEditor(QWidget *parent, const QStyleOptionViewItem &/* option */, const QModelIndex &/* index */) const
                    {
                        QComboBox *editor = new QComboBox(parent);
                    
                        return editor;
                    }
                    
                    void statusDelegat::setEditorData(QWidget *editor, const QModelIndex &index) const
                    {
                        //int value = index.model()->data(index, Qt::EditRole).toInt();
                    
                        QComboBox *statusComboBox = static_cast<QComboBox*>(editor);
                    
                        QMap<int, QString> map;
                        map[1] = "Show";
                        map[2] = "Hide";
                        map[5] = "Moderation";
                    
                        for (auto e : map.keys()) statusComboBox->addItem(map.value(e), map.value(e));
                    
                    }
                    

                    dbeditor.cpp

                    #include "dbeditor.h"
                    #include "ui_dbeditor.h"
                    #include "statusdelegat.h"
                    
                    model = new QSqlTableModel;
                    
                    model->setTable("TABLE");
                    model->setEditStrategy(QSqlTableModel::OnFieldChange);
                    model->setHeaderData(0, Qt::Horizontal, tr("id"));
                    model->setHeaderData(1, Qt::Horizontal, tr("Status"));
                    model->setHeaderData(2, Qt::Horizontal, tr("Name"));
                    
                    ui->tableView->setModel(model);
                    ui->tableView->setItemDelegateForColumn(1, new statusDelegat);
                    model->select();
                    

                    What else should I do to make it work?

                    JonBJ 1 Reply Last reply
                    0
                    • M MyNick 0

                      @jsulm Hi,

                      Now my code looks like this:

                      statusdelegat.h

                      #ifndef STATUSDELEGAT_H
                      #define STATUSDELEGAT_H
                      
                      #include <QStyledItemDelegate>
                      #include <QObject>
                      #include <QComboBox>
                      
                      class statusDelegat : public QStyledItemDelegate
                      {
                          Q_OBJECT
                      public:
                          explicit statusDelegat(QObject *parent = nullptr);
                      
                          QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
                      
                          void setEditorData(QWidget *editor, const QModelIndex &index) const override;
                      
                      };
                      
                      #endif // STATUSDELEGAT_H
                      

                      statusdelegat.cpp

                      #include "statusdelegat.h"
                      
                      statusDelegat::statusDelegat(QObject *parent)
                          : QStyledItemDelegate{parent}
                      {
                      
                      }
                      
                      QWidget *statusDelegat::createEditor(QWidget *parent, const QStyleOptionViewItem &/* option */, const QModelIndex &/* index */) const
                      {
                          QComboBox *editor = new QComboBox(parent);
                      
                          return editor;
                      }
                      
                      void statusDelegat::setEditorData(QWidget *editor, const QModelIndex &index) const
                      {
                          //int value = index.model()->data(index, Qt::EditRole).toInt();
                      
                          QComboBox *statusComboBox = static_cast<QComboBox*>(editor);
                      
                          QMap<int, QString> map;
                          map[1] = "Show";
                          map[2] = "Hide";
                          map[5] = "Moderation";
                      
                          for (auto e : map.keys()) statusComboBox->addItem(map.value(e), map.value(e));
                      
                      }
                      

                      dbeditor.cpp

                      #include "dbeditor.h"
                      #include "ui_dbeditor.h"
                      #include "statusdelegat.h"
                      
                      model = new QSqlTableModel;
                      
                      model->setTable("TABLE");
                      model->setEditStrategy(QSqlTableModel::OnFieldChange);
                      model->setHeaderData(0, Qt::Horizontal, tr("id"));
                      model->setHeaderData(1, Qt::Horizontal, tr("Status"));
                      model->setHeaderData(2, Qt::Horizontal, tr("Name"));
                      
                      ui->tableView->setModel(model);
                      ui->tableView->setItemDelegateForColumn(1, new statusDelegat);
                      model->select();
                      

                      What else should I do to make it work?

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by
                      #12

                      @MyNick-0 said in Conclusion of fixed lists in the editing table. Qt 6.2.1:

                      What else should I do to make it work?

                      Would be nice if you indicated what does not work?

                      for (auto e : map.keys()) statusComboBox->addItem(map.value(e), map.value(e));

                      You intend to make the text and the value both be the same string?

                      You implement setEditorData() but have no need for setModelData()?

                      I don't know the answers to these, they are questions for you.

                      M 1 Reply Last reply
                      2
                      • JonBJ JonB

                        @MyNick-0 said in Conclusion of fixed lists in the editing table. Qt 6.2.1:

                        What else should I do to make it work?

                        Would be nice if you indicated what does not work?

                        for (auto e : map.keys()) statusComboBox->addItem(map.value(e), map.value(e));

                        You intend to make the text and the value both be the same string?

                        You implement setEditorData() but have no need for setModelData()?

                        I don't know the answers to these, they are questions for you.

                        M Offline
                        M Offline
                        MyNick 0
                        wrote on last edited by
                        #13

                        @JonB Hi,

                        1. Nothing has changed, in the first column values of 1, 2, 5

                        2. No, there should be values of 1, 2, 5. I still do not know how to get them

                        3. Setmodeldata() probably needed, but I still do not know how to write this method.
                          After all, I already have ui->tableView->setModel(model);

                        JonBJ 1 Reply Last reply
                        0
                        • M MyNick 0

                          @JonB Hi,

                          1. Nothing has changed, in the first column values of 1, 2, 5

                          2. No, there should be values of 1, 2, 5. I still do not know how to get them

                          3. Setmodeldata() probably needed, but I still do not know how to write this method.
                            After all, I already have ui->tableView->setModel(model);

                          JonBJ Offline
                          JonBJ Offline
                          JonB
                          wrote on last edited by
                          #14

                          @MyNick-0 said in Conclusion of fixed lists in the editing table. Qt 6.2.1:

                          Nothing has changed, in the first column values of 1, 2, 5

                          With your code you should be seeing the strings. Do you mean in the model/database? You have done nothing to update the model.

                          To get a combobox still showing the string texts but having the values 1,2 5, you should need

                          for (auto e : map.keys()) statusComboBox->addItem(map.value(e), e);
                          

                          You should review what setEditorData() is called to do. The creation of the combo items belongs in the createEditor() method. setEditorData() needs to use index to read the current value in the model and select the corresponding item in the combobox.

                          Similarly nothing is going to store the user's selection back to the model till you implement setModelData(). Which needs to use the index passed to it to set the value into the model from the item chosen in the combo.

                          M 1 Reply Last reply
                          1
                          • JonBJ JonB

                            @MyNick-0 said in Conclusion of fixed lists in the editing table. Qt 6.2.1:

                            Nothing has changed, in the first column values of 1, 2, 5

                            With your code you should be seeing the strings. Do you mean in the model/database? You have done nothing to update the model.

                            To get a combobox still showing the string texts but having the values 1,2 5, you should need

                            for (auto e : map.keys()) statusComboBox->addItem(map.value(e), e);
                            

                            You should review what setEditorData() is called to do. The creation of the combo items belongs in the createEditor() method. setEditorData() needs to use index to read the current value in the model and select the corresponding item in the combobox.

                            Similarly nothing is going to store the user's selection back to the model till you implement setModelData(). Which needs to use the index passed to it to set the value into the model from the item chosen in the combo.

                            M Offline
                            M Offline
                            MyNick 0
                            wrote on last edited by
                            #15

                            @JonB
                            Excellent! I added SetModeldata():

                            void statusDelegat::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
                            {
                                QComboBox *statusComboBox = static_cast<QComboBox*>(editor);
                            
                                QString value = QString::number(statusComboBox->itemData(statusComboBox->currentIndex()).toInt());
                            
                                model->setData(index, value, Qt::EditRole);
                            }
                            

                            and appeared ComboBox and I can change the values.

                            I only have to replace the numbers with the corresponding text values from the map array, with the initial show of the "Status" field, but I do not understand what method I should do this?

                            JonBJ 1 Reply Last reply
                            0
                            • M MyNick 0

                              @JonB
                              Excellent! I added SetModeldata():

                              void statusDelegat::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
                              {
                                  QComboBox *statusComboBox = static_cast<QComboBox*>(editor);
                              
                                  QString value = QString::number(statusComboBox->itemData(statusComboBox->currentIndex()).toInt());
                              
                                  model->setData(index, value, Qt::EditRole);
                              }
                              

                              and appeared ComboBox and I can change the values.

                              I only have to replace the numbers with the corresponding text values from the map array, with the initial show of the "Status" field, but I do not understand what method I should do this?

                              JonBJ Offline
                              JonBJ Offline
                              JonB
                              wrote on last edited by
                              #16

                              @MyNick-0
                              If you use statusComboBox->addItem(map.value(e), e) then the numeric value, e, is stored as userdata with the item. Then QVariant currentData(int role = Qt::UserRole) const returns the selected item's value. You will want that during setModelData().

                              There are also methods to read any item's value or find an item with a specified value if you need them, e.g. during your setEditorData().

                              M 1 Reply Last reply
                              1
                              • JonBJ JonB

                                @MyNick-0
                                If you use statusComboBox->addItem(map.value(e), e) then the numeric value, e, is stored as userdata with the item. Then QVariant currentData(int role = Qt::UserRole) const returns the selected item's value. You will want that during setModelData().

                                There are also methods to read any item's value or find an item with a specified value if you need them, e.g. during your setEditorData().

                                M Offline
                                M Offline
                                MyNick 0
                                wrote on last edited by
                                #17

                                @JonB
                                I don't understand.
                                This is what the "Status" field looks when opening the table - it is with numbers:
                                status_field.png
                                I need to replace these numbers with the corresponding values from the array map.
                                How can i do this?

                                JonBJ 1 Reply Last reply
                                0
                                • M MyNick 0

                                  @JonB
                                  I don't understand.
                                  This is what the "Status" field looks when opening the table - it is with numbers:
                                  status_field.png
                                  I need to replace these numbers with the corresponding values from the array map.
                                  How can i do this?

                                  JonBJ Offline
                                  JonBJ Offline
                                  JonB
                                  wrote on last edited by
                                  #18

                                  @MyNick-0
                                  Up until now you have been asking about editing and the combobox which goes with that. Now you are asking about a different situation, when the table is just showing the data.

                                  A QStyledItemDelegate handles the normal display as well as the editing. You could use QString QStyledItemDelegate::displayText(const QVariant &value, const QLocale &locale) const to do your mapping from the numeric value to the string.

                                  Another possibility is to override the model's data() method to return the mapped string for DisplayRole but the numeric value for EditRole.

                                  Since you already have an editing delegate you might as well do it the first way.

                                  M 1 Reply Last reply
                                  0
                                  • A Offline
                                    A Offline
                                    aintcross7
                                    wrote on last edited by
                                    #19

                                    I need to somehow create it in my "model" model.

                                    https://panoramacharter.ltd/
                                    https://19216811.vin/

                                    1 Reply Last reply
                                    0
                                    • JonBJ JonB

                                      @MyNick-0
                                      Up until now you have been asking about editing and the combobox which goes with that. Now you are asking about a different situation, when the table is just showing the data.

                                      A QStyledItemDelegate handles the normal display as well as the editing. You could use QString QStyledItemDelegate::displayText(const QVariant &value, const QLocale &locale) const to do your mapping from the numeric value to the string.

                                      Another possibility is to override the model's data() method to return the mapped string for DisplayRole but the numeric value for EditRole.

                                      Since you already have an editing delegate you might as well do it the first way.

                                      M Offline
                                      M Offline
                                      MyNick 0
                                      wrote on last edited by
                                      #20

                                      @JonB
                                      Yes, I asked because this question is related to the first. Sorry if I confused you.

                                      I displayed text data using the displayText() method.

                                      Thank you all very much!

                                      1 Reply Last reply
                                      0
                                      • M MyNick 0 has marked this topic as solved on

                                      • Login

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