Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

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

    QML and Qt Quick
    2
    3
    1409
    Loading More Posts
    • 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.
    • I
      iviv1 last edited by

      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?

      1 Reply Last reply Reply Quote 0
      • M
        melghawi last edited by

        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.

        1 Reply Last reply Reply Quote 0
        • I
          iviv1 last edited by

          Thank you, @moeg687 ! This helped a lot.

          1 Reply Last reply Reply Quote 0
          • First post
            Last post