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. Using custom QSqlTableModel from QT Quick QML file
Forum Updated to NodeBB v4.3 + New Features

Using custom QSqlTableModel from QT Quick QML file

Scheduled Pinned Locked Moved QML and Qt Quick
4 Posts 3 Posters 1.6k 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.
  • E Offline
    E Offline
    enorrmann
    wrote on last edited by
    #1

    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
    0
    • M Offline
      M Offline
      melghawi
      wrote on last edited by
      #2

      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
      0
      • E Offline
        E Offline
        enorrmann
        wrote on last edited by
        #3

        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
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved