Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Unhandled exception (access violation) when attempting to expose C++ type to QML
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 3.2k Views 1 Watching
  • 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.
  • TLDRT Offline
    TLDRT Offline
    TLDR
    wrote on last edited by
    #1

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

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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();
      

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • TLDRT Offline
        TLDRT Offline
        TLDR
        wrote on last edited by
        #3

        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();
        

        }

        1 Reply Last reply
        0

        • Login

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