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. How to set data for QAbstractTableModel?
Forum Updated to NodeBB v4.3 + New Features

How to set data for QAbstractTableModel?

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 4 Posters 2.2k Views 3 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
    eswaramrth03
    wrote on last edited by eswaramrth03
    #1

    Hi

    I am using qt to create model for my project. In that i create Qabstracttablemodel is used to store the different data. I attached the snippet of the code.
    table .h

    #ifndef TABLEMODEL_H
    #define TABLEMODEL_H
    
    #include <QAbstractTableModel>
    #include <QList>
    #include <QDebug>
    
    struct DrugDetail
    {
        QString m_DrugName;
        QString m_concentraion;
        QString m_dosingUnits;
        
        
    };
    
    class TableModel : public QAbstractTableModel
    {
        Q_OBJECT
        
    public:
        
        TableModel( QObject *parent = nullptr);
        enum roleName{
            R_DrugName=1,
            R_Concentration=2,
            R_dosingUnits=3
            
        };
        
        int rowCount(const QModelIndex &parent) const override;
        int columnCount(const QModelIndex &parent) const override;
        QVariant data(const QModelIndex &index, int role) const override;
        QHash<int, QByteArray>  roleNames() const override;
        bool setData(const QModelIndex &index, const QVariant &value, int role) override;
        bool insertRows(int position, int rows, const QModelIndex &index = QModelIndex()) override;
        
        
    private:
        QList<DrugDetail> drugDetails;
    };
    
    
    #endif // TABLEMODEL_H
     
    

    table.cpp

    
    
    #include "Header/tablemodel.h"
    
    
    
    TableModel::TableModel( QObject *parent)
        : QAbstractTableModel(parent)
        
    {
        
        
    //    drugDetails.append({"doub","150mg","ml/hr"});
    //    drugDetails.append({"bba","50mg","ml/hr/kg"});
        
    }
    
    int TableModel::rowCount(const QModelIndex &parent) const
    {
        
        return drugDetails.size();
    }
    
    int TableModel::columnCount(const QModelIndex &parent) const
    {
        
        return roleNames().size();
    }
    QHash<int, QByteArray>  TableModel::roleNames() const
    {
        QHash<int,QByteArray> roles;
        roles.insert(R_DrugName,"drugName");
        roles.insert(R_Concentration,"concentration");
        roles.insert(R_dosingUnits,"dosingUnits");
        return roles;
        
    }
    
    bool TableModel::setData(const QModelIndex &index, const QVariant &value, int role)
    {
        
        
        if (index.isValid() && role ==R_DrugName ) {
            int row = index.row();
            qDebug()<<"Role Entered";
            
            auto contact = drugDetails.value(row);
            
            if (index.column() == 0)
            {
                contact.m_DrugName = value.toString();
                
            }
            else if (index.column() == 1)
                contact. = value.toString();
            else
            {
                return false;
            }
            drugDetails.replace(row, contact);
            emit(dataChanged(index, index));
            
            return true;
        }
        
        
        return false;
    }
    
    
    
    QVariant TableModel::data(const QModelIndex &index, int role) const
    {
        QVariant variant;
        const int row=index.row();
        const int col=role;
        
        switch(col)
        {
        case R_DrugName:
            variant=drugDetails.at(row).m_DrugName;
            qDebug()<<"Inserted";
            break;
        case R_Concentration:
            variant=drugDetails.at(row).m_concentraion;
            break;
        case R_dosingUnits:
            variant=drugDetails.at(row).m_dosingUnits;
            break;
        default:
            break;
        }
        return variant;
    }
    

    I have doubt relate to set the data for model based on the roles and i tried but, it didn't display any data in the view. can i know the details of the working of the set data function to the model.

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      Role != Column. They are 2 VERY different things. Also the first 256 roles are reserved by Qt and some of them well defined: http://doc.qt.io/qt-5/qt.html#ItemDataRole-enum
      For what you want to do, you just need to consider Qt::EditRole and Qt::DisplayRole. All the other checks you do regarding roles should use columns instead

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      JKSHJ 1 Reply Last reply
      1
      • sierdzioS Offline
        sierdzioS Offline
        sierdzio
        Moderators
        wrote on last edited by
        #3

        @eswaramrth03 said in How to set data for QAbstractTableModel?:

        enum roleName{
        R_DrugName=1,
        R_Concentration=2,
        R_dosingUnits=3

        }
        

        Role names should start with Qt::UserRole + 1, not with 1, 2, 3.

        (Z(:^

        1 Reply Last reply
        2
        • E Offline
          E Offline
          eswaramrth03
          wrote on last edited by eswaramrth03
          #4

          Hi @VRonin

          Thanks for reply. I need to access the drugdetail member list to qml combobox. The data will be acessed based on the textRole property in qml. How can i set the data and access the data in combo box without text roles?

          VRoninV 1 Reply Last reply
          0
          • E eswaramrth03

            Hi @VRonin

            Thanks for reply. I need to access the drugdetail member list to qml combobox. The data will be acessed based on the textRole property in qml. How can i set the data and access the data in combo box without text roles?

            VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by
            #5

            @eswaramrth03 said in How to set data for QAbstractTableModel?:

            access the data in combo box

            The combobox will show the Qt::DisplayRole of the first column of the model

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            1 Reply Last reply
            2
            • VRoninV VRonin

              Role != Column. They are 2 VERY different things. Also the first 256 roles are reserved by Qt and some of them well defined: http://doc.qt.io/qt-5/qt.html#ItemDataRole-enum
              For what you want to do, you just need to consider Qt::EditRole and Qt::DisplayRole. All the other checks you do regarding roles should use columns instead

              JKSHJ Offline
              JKSHJ Offline
              JKSH
              Moderators
              wrote on last edited by JKSH
              #6

              @eswaramrth03 said in How to set data for QAbstractTableModel?:

              I need to access the drugdetail member list to qml combobox.

              A ComboBox uses a list model, not a table model. If you want to display drug name, concentration, and dosing units within the same combobox, you must concatenate them into a single string:

              QString drugDetail = m_DrugName + ' ' + m_concentration + ' ' + m_dosingUnits;

              The data will be acessed based on the textRole property in qml. How can i set the data and access the data in combo box without text roles?

              In QML, you must set the textRole.

              Before you rewrite your model, try a simple example first:

              1. Put the drug names into a QStringListModel
              2. Set the QStringListModel as your ComboBox's model.
              3. Set your ComboBox's textRole to "display".

              Note: "display" corresponds to Qt::DisplayRole: http://doc.qt.io/qt-5/qabstractitemmodel.html#roleNames

              @VRonin said in How to set data for QAbstractTableModel?:

              Role != Column.

              Unfortunately, Role == Column in TableView and TreeView of Qt Quick Controls.

              I really dislike this design. This introduces a huge inconsistency between C++ and QML; a model implemented for use with a C++ View usually can't be used with a QML View without wrapper code: http://doc.qt.io/qt-5/qtquickcontrols1-filesystembrowser-main-cpp.html

              The combobox will show the Qt::DisplayRole of the first column of the model

              This is true for a C++ QComboBox.

              This is not true for QML ComboBox unless we set textRole: "display".

              Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

              E VRoninV 2 Replies Last reply
              2
              • JKSHJ JKSH

                @eswaramrth03 said in How to set data for QAbstractTableModel?:

                I need to access the drugdetail member list to qml combobox.

                A ComboBox uses a list model, not a table model. If you want to display drug name, concentration, and dosing units within the same combobox, you must concatenate them into a single string:

                QString drugDetail = m_DrugName + ' ' + m_concentration + ' ' + m_dosingUnits;

                The data will be acessed based on the textRole property in qml. How can i set the data and access the data in combo box without text roles?

                In QML, you must set the textRole.

                Before you rewrite your model, try a simple example first:

                1. Put the drug names into a QStringListModel
                2. Set the QStringListModel as your ComboBox's model.
                3. Set your ComboBox's textRole to "display".

                Note: "display" corresponds to Qt::DisplayRole: http://doc.qt.io/qt-5/qabstractitemmodel.html#roleNames

                @VRonin said in How to set data for QAbstractTableModel?:

                Role != Column.

                Unfortunately, Role == Column in TableView and TreeView of Qt Quick Controls.

                I really dislike this design. This introduces a huge inconsistency between C++ and QML; a model implemented for use with a C++ View usually can't be used with a QML View without wrapper code: http://doc.qt.io/qt-5/qtquickcontrols1-filesystembrowser-main-cpp.html

                The combobox will show the Qt::DisplayRole of the first column of the model

                This is true for a C++ QComboBox.

                This is not true for QML ComboBox unless we set textRole: "display".

                E Offline
                E Offline
                eswaramrth03
                wrote on last edited by eswaramrth03
                #7

                Hi @JKSH

                Thanks for the reply. I need to display the drugname, concentration and dosing units in different combo box. If i select the one of the drugname, i need to filter the concentration data and update the filter data in concentration combo box. if i choose concentration in that combobox and filter the dosing unit based on concentraion data and update in the dosing unit combobox. It is possible to use single QAbstractlistmodel to achieve this concept? or 3 different Qabstractlistmodel is need to achieve this concept. I am also new to qt and learn the model right now only.

                1 Reply Last reply
                0
                • JKSHJ JKSH

                  @eswaramrth03 said in How to set data for QAbstractTableModel?:

                  I need to access the drugdetail member list to qml combobox.

                  A ComboBox uses a list model, not a table model. If you want to display drug name, concentration, and dosing units within the same combobox, you must concatenate them into a single string:

                  QString drugDetail = m_DrugName + ' ' + m_concentration + ' ' + m_dosingUnits;

                  The data will be acessed based on the textRole property in qml. How can i set the data and access the data in combo box without text roles?

                  In QML, you must set the textRole.

                  Before you rewrite your model, try a simple example first:

                  1. Put the drug names into a QStringListModel
                  2. Set the QStringListModel as your ComboBox's model.
                  3. Set your ComboBox's textRole to "display".

                  Note: "display" corresponds to Qt::DisplayRole: http://doc.qt.io/qt-5/qabstractitemmodel.html#roleNames

                  @VRonin said in How to set data for QAbstractTableModel?:

                  Role != Column.

                  Unfortunately, Role == Column in TableView and TreeView of Qt Quick Controls.

                  I really dislike this design. This introduces a huge inconsistency between C++ and QML; a model implemented for use with a C++ View usually can't be used with a QML View without wrapper code: http://doc.qt.io/qt-5/qtquickcontrols1-filesystembrowser-main-cpp.html

                  The combobox will show the Qt::DisplayRole of the first column of the model

                  This is true for a C++ QComboBox.

                  This is not true for QML ComboBox unless we set textRole: "display".

                  VRoninV Offline
                  VRoninV Offline
                  VRonin
                  wrote on last edited by
                  #8

                  @JKSH said in How to set data for QAbstractTableModel?:

                  Unfortunately, Role == Column in TableView and TreeView of Qt Quick Controls.

                  TableView not anymore in version 2. And they are working on changing TreeView as well.

                  If i select the one of the drugname, i need to filter the concentration data and update the filter data in concentration combo box.

                  You can use a proxy model like: http://doc.qt.io/qt-5/qtquickcontrols1-tableview-example.html

                  "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                  ~Napoleon Bonaparte

                  On a crusade to banish setIndexWidget() from the holy land of Qt

                  1 Reply Last reply
                  2

                  • Login

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