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

QSqlQueryModel badly work in QML



  • Hi!
    This my class. I can't use updateModel(int batchId) in QML.
    In main.cpp I reite this

        MainWindowModel *mainWindowModel=new MainWindowModel();
        engine.rootContext()->setContextProperty("mainWindowModel",mainWindowModel);
    

    How I can use updateModel(int batchId) in QML?

    .h

    #ifndef MAINWINDOWMODEL_H
    #define MAINWINDOWMODEL_H
    
    #include <QObject>
    #include <QSqlQueryModel>
    
    class MainWindowModel : public QSqlQueryModel
    {
            Q_OBJECT
    public:
        explicit MainWindowModel(QObject *parent = nullptr);
    
        enum Roles
        {
            idRole= Qt::UserRole+1,
            serialNumber,
            cryptoSign,
            printedAt,
            doneAt,
            verifiedAt
        };
    
        Q_INVOKABLE QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const;
        Q_INVOKABLE QVariantMap get(int idx) const;
    
    signals:
    
    protected:
        QHash<int,QByteArray> roleNames()const;
    
    public slots:
        void updateModel();
        void updateModel(int batchId);
        int getId(int row);
    };
    
    #endif // MAINWINDOWMODEL_H
    
    

    .cpp

    #include "mainwindowmodel.h"
    
    MainWindowModel::MainWindowModel(QObject *parent) : QSqlQueryModel (parent)
    {
        this->updateModel();
    }
    
    QVariant MainWindowModel::data(const QModelIndex &index, int role) const
    {
        int columnId = role - Qt::UserRole - 1;
        // Создаём индекс с помощью новоиспечённого ID колонки
        QModelIndex modelIndex = this->index(index.row(), columnId);
    
        /* И с помощью уже метода data() базового класса
         * вытаскиваем данные для таблицы из модели
         * */
        return QSqlQueryModel::data(modelIndex, Qt::DisplayRole);
    }
    
    QVariantMap MainWindowModel::get(int idx) const
    {
        QVariantMap map;
        foreach(int k, roleNames().keys()) {
            map[roleNames().value(k)] = data(index(idx, 0), k);
        }
        return map;
    }
    
    QHash<int, QByteArray> MainWindowModel::roleNames() const
    {
        QHash<int, QByteArray> roles;
        roles[idRole]="id";
        roles[serialNumber]="serial";
        roles[cryptoSign]="crypto_sign";
        roles[printedAt]="printed_at";
        roles[doneAt]="done_at";
        roles[verifiedAt]="verified_at";
        return roles;
    }
    
    void MainWindowModel::updateModel()
    {
        this->setQuery("select id, serial, crypto_sign, printed_at, done_at, verified_at from Item");
    }
    
    void MainWindowModel::updateModel(int batchId)
    {
        this->setQuery("select id, serial, crypto_sign, printed_at, done_at, verified_at from Item WHERE batch_id = " + QString::number(batchId));
    }
    
    int MainWindowModel::getId(int row)
    {
        return this->data(this->index(row, 0), idRole).toInt();
    }
    
    


  • it is work

    #ifndef MAINWINDOWMODEL_H
    #define MAINWINDOWMODEL_H
    
    #include <QObject>
    #include <QSqlQueryModel>
    
    class MainWindowModel : public QSqlQueryModel
    {
            Q_OBJECT
    public:
        explicit MainWindowModel(QObject *parent = nullptr);
    
        enum Roles
        {
            idRole= Qt::UserRole+1,
            serialNumber,
            cryptoSign,
            printedAt,
            doneAt,
            verifiedAt
        };
    
        Q_INVOKABLE QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const;
        Q_INVOKABLE QVariantMap get(int idx) const;
    
    signals:
    
    protected:
        QHash<int,QByteArray> roleNames()const;
    
    public slots:
        //void updateModel();
        void updateModel(int batchId);
        int getId(int row);
    };
    
    #endif // MAINWINDOWMODEL_H
    

Log in to reply