Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. the model in Qt Quick can not show the data in listview?
Forum Updated to NodeBB v4.3 + New Features

the model in Qt Quick can not show the data in listview?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
5 Posts 2 Posters 465 Views 2 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.
  • MhM93M Offline
    MhM93M Offline
    MhM93
    wrote on last edited by
    #1

    hi.
    this is my model class, this class just show PID. not show name,family and other data. it just shows error refrence.Why?

    #ifndef MODEL_MNG_H
    #define MODEL_MNG_H
    
    #include <QObject>
    #include <QSqlQueryModel>
    #include <QSqlRecord>
    #include <QSqlField>
    #include "mydatabase.h"
    
    class model_mng : public QSqlQueryModel
    {
        Q_OBJECT
    public:
        enum Roles{
            PID=Qt::UserRole+1,
            Name2,
            family,
            Admin,
            Active,
            CanOpenthedoor,
            card,
            finger,
            pass
        };
        explicit model_mng(QObject *parent = nullptr);
    
    public slots:
        QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
        void updateModel(int id);
        int getId(int row);
    protected:
         QHash<int,QByteArray> roleNames() const;
    
    
    };
    
    #endif // MODEL_MNG_H
    

    and:

    #include "model_mng.h"
    model_mng::model_mng(QObject *parent) : QSqlQueryModel(parent)
    {
    
    }
    
    QVariant model_mng::data(const QModelIndex &index, int role) const
    {
        int columnId = role - Qt::UserRole - 1;
        QModelIndex modelIndex = this->index(index.row(), columnId);
    
        return QSqlQueryModel::data(modelIndex, Qt::DisplayRole);
    
    }
    
    QHash<int, QByteArray> model_mng::roleNames() const
    {
        QHash<int,QByteArray> roles;
        roles[PID] = "PID";
        roles[Name2] = "PNAME";
        roles[family] = "PFAMILY";
        roles[Admin] = "ACCESSLEVEL";
        roles[Active] = "ISACTIVE";
        roles[CanOpenthedoor]="CANOPENTHEDOOR";
        roles[card] = "card";
        roles[finger] = "finger";
        roles[pass] = "pass";
        //roles[Qt::DisplayRole] = "displayRole";
        return roles;
    }
    
    void model_mng::updateModel(int id)
    {
        qDebug()<<"in updatemodel:"<<id;
        QString str;
        str.append("SELECT DISTINCT ui.[PID],ui.[PNAME],ui.[PFAMILY],ui.[ISACTIVE],UI.[CANOPENTHEDOOR],"
                   "case when(CardID='') then 0 else 1 end as card,"
                   "case when(select count(*) FROM FINGERS WHERE FINGERS.PID=UI.PID)>0 then 1 else 0 end as finger,"
                   "case when([Password]='') then 0 else 1 end as pass "
                   "FROM PERSONEL ui LEFT JOIN FINGERS F");
    //    str.append("select p.PID,p.PNAME from PERSONEL p");
        if(id>-1)
        {
            str.append(" where ui.[PID]=");/*and ui.[UI_ID]=*/
            str.append(QString::number(id));
        }
        str.append(";");
    
        this->setQuery(str,QSqlDatabase::database("MainDB"));
        qDebug()<<query().lastError();
    //    qDebug()<<PID<<Name<<Family<<Admin<<Active<<card<<pass<<finger<<CanOpenthedoor;
        qDebug()<<this->rowCount();
    
    }
    
    int model_mng::getId(int row)
    {
        return this->data(this->index(row, 0), PID).toInt();
    
    }
    

    the qml page :

     ListView {
                width: 100; height: 400
    
                model: myModel
                delegate: Rectangle {
                    height: 25
                    width: parent.width
                    Text { text: PID  }
                    MyLabel { txt: family  }
                }
            }
    

    it shows this error:

    qrc:/Users_Manage.qml:101: ReferenceError: family is not defined
    QSqlError("", "", "")
    14<= roe count
    

    H.Ghassami

    B 1 Reply Last reply
    0
    • MhM93M MhM93

      hi.
      this is my model class, this class just show PID. not show name,family and other data. it just shows error refrence.Why?

      #ifndef MODEL_MNG_H
      #define MODEL_MNG_H
      
      #include <QObject>
      #include <QSqlQueryModel>
      #include <QSqlRecord>
      #include <QSqlField>
      #include "mydatabase.h"
      
      class model_mng : public QSqlQueryModel
      {
          Q_OBJECT
      public:
          enum Roles{
              PID=Qt::UserRole+1,
              Name2,
              family,
              Admin,
              Active,
              CanOpenthedoor,
              card,
              finger,
              pass
          };
          explicit model_mng(QObject *parent = nullptr);
      
      public slots:
          QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
          void updateModel(int id);
          int getId(int row);
      protected:
           QHash<int,QByteArray> roleNames() const;
      
      
      };
      
      #endif // MODEL_MNG_H
      

      and:

      #include "model_mng.h"
      model_mng::model_mng(QObject *parent) : QSqlQueryModel(parent)
      {
      
      }
      
      QVariant model_mng::data(const QModelIndex &index, int role) const
      {
          int columnId = role - Qt::UserRole - 1;
          QModelIndex modelIndex = this->index(index.row(), columnId);
      
          return QSqlQueryModel::data(modelIndex, Qt::DisplayRole);
      
      }
      
      QHash<int, QByteArray> model_mng::roleNames() const
      {
          QHash<int,QByteArray> roles;
          roles[PID] = "PID";
          roles[Name2] = "PNAME";
          roles[family] = "PFAMILY";
          roles[Admin] = "ACCESSLEVEL";
          roles[Active] = "ISACTIVE";
          roles[CanOpenthedoor]="CANOPENTHEDOOR";
          roles[card] = "card";
          roles[finger] = "finger";
          roles[pass] = "pass";
          //roles[Qt::DisplayRole] = "displayRole";
          return roles;
      }
      
      void model_mng::updateModel(int id)
      {
          qDebug()<<"in updatemodel:"<<id;
          QString str;
          str.append("SELECT DISTINCT ui.[PID],ui.[PNAME],ui.[PFAMILY],ui.[ISACTIVE],UI.[CANOPENTHEDOOR],"
                     "case when(CardID='') then 0 else 1 end as card,"
                     "case when(select count(*) FROM FINGERS WHERE FINGERS.PID=UI.PID)>0 then 1 else 0 end as finger,"
                     "case when([Password]='') then 0 else 1 end as pass "
                     "FROM PERSONEL ui LEFT JOIN FINGERS F");
      //    str.append("select p.PID,p.PNAME from PERSONEL p");
          if(id>-1)
          {
              str.append(" where ui.[PID]=");/*and ui.[UI_ID]=*/
              str.append(QString::number(id));
          }
          str.append(";");
      
          this->setQuery(str,QSqlDatabase::database("MainDB"));
          qDebug()<<query().lastError();
      //    qDebug()<<PID<<Name<<Family<<Admin<<Active<<card<<pass<<finger<<CanOpenthedoor;
          qDebug()<<this->rowCount();
      
      }
      
      int model_mng::getId(int row)
      {
          return this->data(this->index(row, 0), PID).toInt();
      
      }
      

      the qml page :

       ListView {
                  width: 100; height: 400
      
                  model: myModel
                  delegate: Rectangle {
                      height: 25
                      width: parent.width
                      Text { text: PID  }
                      MyLabel { txt: family  }
                  }
              }
      

      it shows this error:

      qrc:/Users_Manage.qml:101: ReferenceError: family is not defined
      QSqlError("", "", "")
      14<= roe count
      
      B Offline
      B Offline
      Bob64
      wrote on last edited by
      #2

      @MhM93 You need to use the role name string in QML, so it would be PFAMILY rather than family. I think you will also need to do a bit more work in laying out the items inside the Rectangle.

      1 Reply Last reply
      1
      • MhM93M Offline
        MhM93M Offline
        MhM93
        wrote on last edited by
        #3

        really thanks. so silly problem. excuse me. Sorry but what do you mean " laying out the items inside the Rectangle"?

        H.Ghassami

        B 1 Reply Last reply
        0
        • MhM93M MhM93

          really thanks. so silly problem. excuse me. Sorry but what do you mean " laying out the items inside the Rectangle"?

          B Offline
          B Offline
          Bob64
          wrote on last edited by
          #4

          @MhM93 what I mean is that by default the Text and Label children of the Rectangle will both be at (x,y) =(0,0) so will overlap. I assume you want them next to each other. QML provides various tools to help in placing objects in positions relative to each other. You could place them in a Row or use anchors, or just set x explicitly.

          1 Reply Last reply
          1
          • MhM93M Offline
            MhM93M Offline
            MhM93
            wrote on last edited by
            #5

            Aha, I get it. thanks.

            H.Ghassami

            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