Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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



  • 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
    


  • @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.



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



  • @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.



  • Aha, I get it. thanks.


Log in to reply