Important: Please read the Qt Code of Conduct -

qmlRegisterType not working, cannot import C++ Object

  • 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.left: parent.left
                anchors.leftMargin: 289
                ListView {
                    id: patientlistview
                    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"
                                    font.pixelSize: 10
                                    font.bold: true
                            Rectangle {
                                id: rectangle2
                                width: 50
                                height: 40
                                color: "lightgray"
                            } // ETC... more rectangles
                             }//end row
                        }//end itemdelegate

    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.
        if (! {
            qFatal("Cannot open database: %s", qPrintable(database.lastError().text()));
    int main(int argc, char *argv[])
        QGuiApplication app(argc, argv);
        qmlRegisterType<sqlpatientdata>("io.qt.projects.mockup1", 1,0, "sqlpatientdata");
        QQmlApplicationEngine engine;
        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 :(

  • Hi!

    @Julia-Johnson said:
    I am pretty sure I used qmlRegisterType correctly

    Well, 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{}).

  • Thank-you so much! That fixed the issue! I can now visualize data from my QSqlTableModel and implement Q_INVOKABLE functions. Thanks!

Log in to reply