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. qmlRegisterType not working, cannot import C++ Object
Forum Update on Monday, May 27th 2025

qmlRegisterType not working, cannot import C++ Object

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
3 Posts 2 Posters 2.5k Views
  • 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.
  • J Offline
    J Offline
    Julia Johnson
    wrote on 14 Jul 2016, 01:11 last edited by
    #1

    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 :(

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on 14 Jul 2016, 03:33 last edited by
      #2

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

      1 Reply Last reply
      4
      • J Offline
        J Offline
        Julia Johnson
        wrote on 14 Jul 2016, 19:44 last edited by
        #3

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

        1 Reply Last reply
        0

        1/3

        14 Jul 2016, 01:11

        • Login

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