Sendig a model from C++ class to QML



  • Hi,
    I have problem with loading a C++ model into the TableView in Qml.
    the model:

    //dataModel.h
    #ifndef DATAMODEL_H
    #define DATAMODEL_H
    
    #include <QSqlQueryModel>
    
    class dataModel : public QSqlQueryModel
    {
        Q_OBJECT
    
    public:
        explicit dataModel(QObject *parent = 0);
        void setQuery(const QString &query, const QSqlDatabase &db = QSqlDatabase());
        void setQuery(const QSqlQuery &query);
        QVariant data(const QModelIndex &index, int role) const;
        QHash<int, QByteArray> roleNames() const {	return m_roleNames;	}
    
    private:
        void generateRoleNames();
        QHash<int, QByteArray> m_roleNames;
    };
    
    
    #endif // DATAMODEL_H
    

    I have a Controller that create a object from this

    //QmlController.h
    #ifndef QMLCONTROLLER_H
    #define QMLCONTROLLER_H
    
    #include <QObject>
    #include <QString>
    #include <QQmlEngine>
    #include <QJSEngine>
    #include "database/database.h"
    #include "model/datamodel.h"
    
    class QmlController : public QObject
    {
        Q_OBJECT
        Database db;
        dataModel m_model;
    public:
        explicit QmlController(QObject *parent = 0);
        Q_INVOKABLE void insertData(QString date, QString title, QString inputMoney, QString outputMoney);
        Q_INVOKABLE void selectAll();
        Q_INVOKABLE dataModel &getModel();
    
    signals:
    
    public slots:
    
    private:
        QString buildQuery(QString &date, QString &title, QString &inputMoney, QString &outputMoney);
    };
    
    static QObject *example_qobject_singletontype_provider(QQmlEngine *engine, QJSEngine *scriptEngine)
    {
        Q_UNUSED(engine)
        Q_UNUSED(scriptEngine)
    
        QmlController *example = new QmlController();
        return example;
    }
    #endif // QMLCONTROLLER_H
    

    and the definition of the getModel() is this:

    //QmlController.cpp
    dataModel &QmlController::getModel()
    {
        return this->m_model;
    }
    

    main.cpp

    //main.cpp
    #include <QGuiApplication>
    #include <QQmlApplicationEngine>
    #include <QQmlContext>
    #include "qmlcontroller.h"
    
    int main(int argc, char *argv[])
    {
        QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    
        QGuiApplication app(argc, argv);
        qmlRegisterSingletonType<QmlController>("test.myself", 1, 0, "ControllerCpp", example_qobject_singletontype_provider);
        qmlRegisterType<dataModel>("test.myself", 1, 0, "ModelMaster");
        QQmlApplicationEngine engine;
        engine.load(QUrl(QLatin1String("qrc:/main.qml")));
    
        return app.exec();
    }
    
    

    and I call the getModel() here:

    //MyTable.qml
    import QtQuick 2.0
    import QtQuick.Controls 1.4
    import test.myself 1.0
    
    Item {
        TableView {
            id: tableViewId
            anchors.centerIn: parent
            width: 300
            height: 200
            model: ControllerCpp.getModel()
            TableViewColumn { ... }
        }
    
    }
    

    there is no error that I can understand where the problem is but I think I have problem with getModel()
    because all other methods work just find and add data to database but this method doesn't work I guess.


  • Moderators

    Hi alirezafour ,

    There is a working example with the same goal as yours in this thread



  • Thnaks @Eddy
    my problem solved by changing the m_model to a QPointer<dataModel> and change dataModel &QmlController::getModel() to dataModel *QmlController::getModel()


  • Moderators

    Good deal !


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.