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. Sendig a model from C++ class to QML
Qt 6.11 is out! See what's new in the release blog

Sendig a model from C++ class to QML

Scheduled Pinned Locked Moved Solved QML and Qt Quick
4 Posts 2 Posters 1.9k Views 1 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.
  • A Offline
    A Offline
    alirezafour
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • EddyE Offline
      EddyE Offline
      Eddy
      wrote on last edited by
      #2

      Hi alirezafour ,

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

      Qt Certified Specialist
      www.edalsolutions.be

      1 Reply Last reply
      0
      • A Offline
        A Offline
        alirezafour
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • EddyE Offline
          EddyE Offline
          Eddy
          wrote on last edited by
          #4

          Good deal !

          Qt Certified Specialist
          www.edalsolutions.be

          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