Qt5 Qt Quick 2 - adding a member variable to a c++ class causes app to crash



  • I have VS 2010 and Qt5 installed on a Windows 7 VM.

    Question 1: If CMenu contains a variable the application crashes - remove the variable and the app runs.

    CMenu.h
    @
    #ifndef MENU_H
    #define MENU_H

    #include <QObject>

    class CMenu : public QObject
    {
    public:

    CMenu(QObject *parent = 0);
    //Q_PROPERTY(QString text READ m_text);
    
    QString m_text;
    

    };

    #endif // MENU_H
    @

    CMenu.cpp
    @
    #include "menu.h"

    CMenu::CMenu(QObject *parent) : QObject (parent)
    {
    m_text = "blah";
    }
    @

    main.cpp
    @
    #include <QtGui/QGuiApplication>
    #include "qtquick2applicationviewer.h"
    #include <QQmlContext>

    #include "menu.h"

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

    QtQuick2ApplicationViewer  viewer;
    
    CMenu menu(&viewer);
    
    viewer.rootContext()->setContextProperty("myMenu", &menu);
    
    viewer.setMainQmlFile&#40;QStringLiteral("qml/GuideNext/main.qml"&#41;&#41;;
    viewer.showExpanded();
    
    return app.exec&#40;&#41;;
    

    }
    @

    pro
    @

    Add more folders to ship with the application, here

    folder_01.source = qml/GuideNext
    folder_01.target = qml
    DEPLOYMENTFOLDERS = folder_01

    QT += quick qml core

    Additional import path used to resolve QML modules in Creator's code model

    QML_IMPORT_PATH =

    If your application uses the Qt Mobility libraries, uncomment the following

    lines and add the respective components to the MOBILITY variable.

    CONFIG += mobility

    MOBILITY +=

    The .cpp file which was generated for your project. Feel free to hack it.

    SOURCES += main.cpp
    menu.cpp

    Please do not modify the following two lines. Required for deployment.

    include(qtquick2applicationviewer/qtquick2applicationviewer.pri)
    qtcAddDeployment()

    HEADERS +=
    menu.h
    @

    Question 2: What is the QML I would need to write in this file to get the string from my CMenu class to display? Cannot seem to get it to work.
    @
    import QtQuick 2.0

    Rectangle {
    width: 360
    height: 360
    Text {
    //text: myMenu.text //qsTr("Hello World")
    anchors.centerIn: parent
    }
    MouseArea {
    anchors.fill: parent
    onClicked: {
    Qt.quit();
    }
    }
    }
    @


  • Moderators

    Hallo again :)

    @
    Q_PROPERTY(QString text READ m_text);
    @

    Parameter passed to READ needs to be a method. So you need to add a getter method and put it into the declaration above. Once you do that, the second problem should go away, too. Hopefully.



  • Evening :-)

    Yes I know, that bit is commented out which is why I cannot why understand adding a class variable is causing the app to crash. Honestly, I am testing this stuff line by line - without the variable it works fine, add the variable and run again and it crashes. It is not even referenced in the QML code so I cannot see how it can be causing the app to crash :-(


  • Moderators

    Q_PROPERTY is not a simple macro. Qt uses MOC to generate additional meta-object code for classes that inherit from QObject. There might be a crash somewhere there. A debugging session might help here. And/ or adding that getter.



  • Evening :-)

    Yes I know thanks, I should have corrected that in the post - that bit is commented out so is not doing anything, which is why I cannot why understand adding a class variable is causing the app to crash. Honestly, I am testing this stuff line by line - without the variable it works fine, add the variable and run again and it crashes. It is not even referenced in the QML code so I cannot see how it can be causing the app to crash :-(

    Is that the problem?? Does the class have to only contain things the QML cares about?

    @
    #ifndef MENU_H
    #define MENU_H

    #include <QObject>

    class CMenu : public QObject
    {
    public:

    CMenu(QObject *parent = 0);
    Q_PROPERTY(QString text READ getText())
    
    QString getText();
    

    private:
    //********un comment this line and the app crashes????
    //QString m_text;
    };

    #endif // MENU_H
    @


  • Moderators

    I've meant more like this:
    @
    class CMenu : public QObject
    {
    public:

    CMenu(QObject *parent = 0);
    Q_PROPERTY(QString text READ getText) // no brackets needed for getText here
    
    QString getText();
    

    private:
    QString m_text; // should work when uncommented
    };
    @

    But judging from our previous tries, I'm fully prepared for another surprise your PC will throw at us.



  • My typo's aside the issue is that this code crashes

    @
    #ifndef MENU_H
    #define MENU_H

    #include <QObject>

    class CMenu : public QObject
    {
    public:

    CMenu(QObject *parent = 0);
    

    private:
    QString m_text;
    };

    #endif // MENU_H
    @

    and this does not
    @
    #ifndef MENU_H
    #define MENU_H

    #include <QObject>

    class CMenu : public QObject
    {
    public:

    CMenu(QObject *parent = 0);
    

    private:
    };

    #endif // MENU_H
    @

    Q_PROPERTY is not involved.

    Not making any other changes, just that one line in the header == crash/not crash. I have started on a clean machine (VM Windows 7) with Qt5........... grrrrr


  • Moderators

    Another idea. It could be that the piece that is missing is include of <QString>. Qt uses forward declaration, so when you include QObject in line 4. it informs compiler that there is a class named QString, but it does not actually define what it is. Compiler assumes everything is ok, but when you then try to assign to it in .cpp file, it crashes.

    That is a bit far-fetched, though. The compiler should raise and error and not finish the compilation. In any case, try adding:
    @
    #include <QString>
    @

    to cmenu.h.



  • Yeah, I noticed that earlier and thought it was weird that it compiled with out QString include, I mean, where is the compile error right? Then the phone went and the yammering at the other end distracted me, and I did not put the include in - DOH!

    Anyway, include did not change anything but re installing Qt 4.8.4 seems to have done the trick. Whoooo hoooo!


  • Moderators

    Well, congrats on that. :) It's still a total mystery to me, but at least something works.


Log in to reply
 

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