qmlRegisterType not working, cannot import C++ Object
-
wrote on 14 Jul 2016, 01:11 last edited by
I am getting this error when running the following code:
qrc:/Masterpatientframeii.qml:45 Expected type name
Here is the QML file importing the C++ object (hopefully)...
import QtQuick 2.7 import QtQuick.Controls 2.0 import QtQuick.Layouts 1.3 import io.qt.projects.mockup1 1.0 Frame { id: masterpatientframe property var patientdata Frame { id: patientinfo x: -24 y: -12 width: 300 height: 350 anchors.left: parent.left anchors.leftMargin: -12 anchors.bottom: parent.bottom anchors.bottomMargin: -32 Frame { id: patientselectionframe x: 277 y: -24 width: 259 anchors.bottom: parent.bottom anchors.bottomMargin: 12 anchors.topMargin: -12 anchors.top: parent.top anchors.left: parent.left anchors.leftMargin: 289 ListView { id: patientlistview anchors.fill:parent topMargin: 5 leftMargin: 5 bottomMargin: 5 rightMargin: 5 spacing: 15 model: sqlpatientdata{} //This is what Qt 5.7 refuses to load delegate: ItemDelegate { width: patientlistview.width - patientlistview.leftMargin - patientlistview.rightMargin height: 40 Row { id: row1 Rectangle { id: rectangle1 width: 40 height: 40 color: "steelblue" Label{ font.pixelSize: 10 text:name font.bold: true } } Rectangle { id: rectangle2 width: 50 height: 40 color: "lightgray" } // ETC... more rectangles }//end row }//end itemdelegate }//endlistView }//endframe
I am pretty sure I used qmlRegisterType correctly to connect my C++ object to this QML. Here is main.cpp
#include <QGuiApplication> #include <QQmlApplicationEngine> #include <QStandardPaths> #include <QSqlDatabase> #include <QSqlError> #include <QtSql> #include "sqlpatientdata.h" static void connectToDatabase() { QSqlDatabase database = QSqlDatabase::database(); if (!database.isValid()) { database = QSqlDatabase::addDatabase("QSQLITE"); if (!database.isValid()) qFatal("Cannot add database: %s", qPrintable(database.lastError().text())); } const QDir writeDir = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation); if (!writeDir.mkpath(".")) qFatal("Failed to create writable directory at %s", qPrintable(writeDir.absolutePath())); // Ensure that we have a writable location on all devices. const QString fileName = writeDir.absolutePath() + "/chat-database.sqlite3"; // When using the SQLite driver, open() will create the SQLite database if it doesn't exist. database.setDatabaseName(fileName); if (!database.open()) { qFatal("Cannot open database: %s", qPrintable(database.lastError().text())); QFile::remove(fileName); } } int main(int argc, char *argv[]) { QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); QGuiApplication app(argc, argv); qmlRegisterType<sqlpatientdata>("io.qt.projects.mockup1", 1,0, "sqlpatientdata"); connectToDatabase(); QQmlApplicationEngine engine; engine.load(QUrl(QLatin1String("qrc:/main.qml"))); return app.exec(); }
Any explanation of this error would be great. When I comment out model: sqlpatientdata the error goes away. Qt is struggling to import the C++ object over to QML :(
-
wrote on 14 Jul 2016, 03:33 last edited by
Hi!
@Julia-Johnson said:
I am pretty sure I used qmlRegisterType correctlyWell, almost. But for the QML magic to work all your QML type names have to start with an uppercase character. So the following should work:
qmlRegisterType<sqlpatientdata>("io.qt.projects.mockup1", 1,0, "Sqlpatientdata");
You can now use the type in your QML files (
Sqlpatientdata{}
). -
wrote on 14 Jul 2016, 19:44 last edited by
Thank-you so much! That fixed the issue! I can now visualize data from my QSqlTableModel and implement Q_INVOKABLE functions. Thanks!
1/3