Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Call for Presentations - Qt World Summit

    Using custom QSqlTableModel from QT Quick QML file

    QML and Qt Quick
    3
    4
    1337
    Loading More Posts
    • 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.
    • E
      enorrmann last edited by

      I'm getting frustrated here. I implemented a custom QSqlTableModel and I can't make it work from inside a QML file. But it works from c++ code ! Here is what I got:

      @#include <QSqlQuery>
      #include <QSqlTableModel>
      #include "tablemodel.h"

      MyTableModel::MyTableModel() : QSqlTableModel()
      {

      setTable("Event");
      setEditStrategy(QSqlTableModel::OnFieldChange);
      select();
      

      }
      @

      and this is my QML

      @import QtQuick 2.2
      import QtQuick.Controls 1.1
      import QtQuick.Window 2.1
      import org.qtproject.examples.calendar.tablemodel 1.0
      Window {
      visible:true
      width: 640
      height: 400
      minimumWidth: 400
      minimumHeight: 300
      color: "#f4f4f4"

      title: "Table example"
      
       MyTableModel{
           id : myModel
       }
      
      TableView{
          model:myModel
          anchors.fill: parent
      }
      

      }
      @

      and this is the main file, the commented code works, but i want to show the table from inside QML

      @#include <QtQml>
      #include <QtGui>
      #include <QTableView>

      #include "qtquickcontrolsapplication.h"
      #include "sqleventmodel.h"
      #include "tablemodel.h"
      #include "mytableview.h"

      int main(int argc, char *argv[])
      {

      QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
      db.setDatabaseName("test");
      if (!db.open()) {
          qFatal("Cannot open database");
      }
      

      QtQuickControlsApplication app(argc, argv);
      qmlRegisterType<MyTableModel>("org.qtproject.examples.calendar.tablemodel", 1, 0, "MyTableModel");

      QQmlApplicationEngine engine(QUrl("qrc:/qml/main.qml"));
      

      // QApplication app(argc, argv);
      // QSqlTableModel *model = new MyTableModel();

      // QTableView *view = new QTableView();
      // view->setModel(model);
      // view->show();

          return app.exec();
      

      }

      @

      1 Reply Last reply Reply Quote 0
      • M
        melghawi last edited by

        Hi there,

        Take a look at "this":http://qt-project.org/doc/qt-4.7/qdeclarativemodels.html#id-01dbf022-49bc-4d56-8403-7b73526a3da5

        1 Reply Last reply Reply Quote 0
        • E
          enorrmann last edited by

          Thanks moeg687, already read that. I exported the model and the model is visible from QML, but it is not rendering

          1 Reply Last reply Reply Quote 0
          • SGaist
            SGaist Lifetime Qt Champion last edited by

            Hi and welcome to devnet,

            It requires a little more work than that. Have a look at this "wiki entry":http://qt-project.org/wiki/How_to_use_a_QSqlQueryModel_in_QML

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply Reply Quote 0
            • First post
              Last post