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()
{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();
}
@
-
Hi there,
Take a look at "this":http://qt-project.org/doc/qt-4.7/qdeclarativemodels.html#id-01dbf022-49bc-4d56-8403-7b73526a3da5
-
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