[SOLVED] { Qt5.0.2/QtQuick2.0/C++ } What is the right way to make a class outside main.cpp to use QtQuick2ApplicationViewer?



  • I have described the question in detail in stack overflow:

    http://stackoverflow.com/questions/16066428/qt5-0-2-qtquick2-0-c-what-is-the-right-way-to-make-a-class-outside-main-cp

    The problem:

    I want to make a C++ application that uses QML for dialog UI.

    I am trying to put my UI code outside main.cpp (in a class called "Dialog"), so that I can later separate it to run in a thread.

    I build & run: No errors in compilation, no errors in application output.

    However, nothing shows up on the screen. But if written in main.cpp, this chunk of code shows the QML dialog correctly:

    @QtQuick2ApplicationViewer viewer;
    viewer.setMainQmlFile(QStringLiteral("qml/Kiosk/main.qml"));

    viewer.showExpanded();@

    ==============================================================

    I wonder why this is happening, could you please advise - what am I doing wrong?



  • Looking at the code you posted on stack overflow I notice that you are creating an instance of QtQuick2ApplicationViewer on the stack.

    @
    void Dialog::show()
    {
    QtQuick2ApplicationViewer viewer;
    viewer.setMainQmlFile(QStringLiteral("qml/Kiosk/main.qml"));

    viewer.showExpanded();
    

    }
    @

    This will go out of scope and be destroyed once your function returns.

    Try this:

    @
    void Dialog::show()
    {
    QtQuick2ApplicationViewer *viewer = new QtQuick2ApplicationViewer;
    viewer->setMainQmlFile(QStringLiteral("qml/Kiosk/main.qml"));

    viewer->showExpanded();
    

    }
    @

    You will need to retain the viewer pointer and explicitly call delete in order to avoid memory leaks.



  • Thank you, @moeg687 ! This helped a lot.


Log in to reply
 

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