Unhandled exception (access violation) when attempting to expose C++ type to QML



  • Hi, I'm attempting to follow the tutorial here: http://doc.qt.io/qt-5/qtqml-cppintegration-exposecppattributes.html

    The tutorial creates a C++ "Message" object and uses it in a QML project. Everything builds and links just fine (and MOC is run, as would be needed), but attempting to run it leads to a Unhandled exception which the stack trace says happens within app.exec(). Anyone have any pointers?

    Unhandled exception at 0x00007FFC9106CD34 (Qt5Guid.dll) in testapp.exe: 0xC0000005: Access violation reading location 0xFFFFFFFFFFFFFFFF.
    
    Exception thrown at 0x00007FFC9106CD34 (Qt5Guid.dll) in testapp.exe: 0xC0000005: Access violation reading location 0xFFFFFFFFFFFFFFFF.
    
    Unhandled exception at 0x00007FFC9106CD34 (Qt5Guid.dll) in testapp.exe: 0xC0000005: Access violation reading location 0xFFFFFFFFFFFFFFFF.
    
    Exception thrown at 0x00007FFC9106CD34 (Qt5Guid.dll) in testapp.exe: 0xC0000005: Access violation reading location 0xFFFFFFFFFFFFFFFF.
    
    Unhandled exception at 0x00007FFC9106CD34 (Qt5Guid.dll) in testapp.exe: 0xC0000005: Access violation reading location 0xFFFFFFFFFFFFFFFF.
    
    Exception thrown at 0x00007FFC9106CD34 (Qt5Guid.dll) in testapp.exe: 0xC0000005: Access violation reading location 0xFFFFFFFFFFFFFFFF.
    
    Unhandled exception at 0x00007FFC9106CD34 (Qt5Guid.dll) in testapp.exe: 0xC0000005: Access violation reading location 0xFFFFFFFFFFFFFFFF.
    
    Exception thrown at 0x00007FFC9106CD34 (Qt5Guid.dll) in testapp.exe: 0xC0000005: Access violation reading location 0xFFFFFFFFFFFFFFFF.
    

    I'm using Visual Studio 14 (2015) and Qt 5.6. Full source code below:

    main.cpp:

    #include "message.h"
    
    #include <QtWidgets/QApplication>
    #include <QtQml/QQmlContext>
    #include <QtQml/QQmlEngine>
    #include <QtQml/QQmlComponent>
    
    int main(int argc, char *argv[])
    {
        QCoreApplication app(argc, argv);
    
        QQmlEngine engine;
        Message msg;
        engine.rootContext()->setContextProperty("msg", &msg);
        QQmlComponent component(&engine, QUrl::fromLocalFile("MyItem.qml"));
        component.create();
    
        return app.exec();
    }
    

    message.h:

    #include <QtCore/QObject>
    
    class Message : public QObject
    {
        Q_OBJECT
    	Q_PROPERTY(QString author READ author WRITE setAuthor NOTIFY authorChanged)
    public:
        void setAuthor(const QString &a) {
    	    if (a != m_author) {
    		    m_author = a;
    		    emit authorChanged();
    	    }
        }
        QString author() const {
    	    return m_author;
        }
    signals:
        void authorChanged();
    private:
        QString m_author;
    };
    

    MyItem.qml:

    // MyItem.qml
    import QtQuick 2.0
    
    Text {
        width: 100; height: 100
        text: msg.author    // invokes Message::author() to get this value
    
        Component.onCompleted: {
            msg.author = "Jonah"  // invokes Message::setAuthor()
        }
    }
    

    (all of which are copied wholesale from the link at the top, with the exception of #include lines which aren't present in the tutorial).


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    That's a bug in the documentation (fix underway)
    Change the body of main for:

    QGuiApplication app(argc, argv);
    
    QQuickView view;
    Message msg;
    view.engine()->rootContext()->setContextProperty("msg", &msg);
    view.setSource(QUrl::fromLocalFile("MyItem.qml"));
    view.show();
    
    return app.exec();
    


  • Awesome, thank you. This works just as you said it would:

    #include "message.h"

    #include <QtGui/QGuiApplication>
    #include <QtQuick/QQuickView>
    #include <QtQml/QQmlContext>
    #include <QtQml/QQmlEngine>
    #include <QtQml/QQmlComponent>

    int main(int argc, char *argv[])
    {
    QGuiApplication app(argc, argv);

    QQuickView view;
    Message msg;
    view.engine()->rootContext()->setContextProperty("msg", &msg);
    view.setSource(QUrl::fromLocalFile("MyItem.qml"));
    view.show();
    
    return app.exec();
    

    }


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.