Important: Please read the Qt Code of Conduct -

Using custom QSqlTableModel from QT Quick QML file

  • 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()



    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 {
    width: 640
    height: 400
    minimumWidth: 400
    minimumHeight: 300
    color: "#f4f4f4"

    title: "Table example"
         id : 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");
    if (! {
        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();



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

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    It requires a little more work than that. Have a look at this "wiki entry":

Log in to reply