Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. [SOLVED] { Qt5.0.2/QtQuick2.0/C++ } What is the right way to make a class outside main.cpp to use QtQuick2ApplicationViewer?
QtWS25 Last Chance

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

Scheduled Pinned Locked Moved QML and Qt Quick
3 Posts 2 Posters 1.6k Views
  • 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 Offline
    I Offline
    iviv1
    wrote on last edited by
    #1

    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
    0
    • M Offline
      M Offline
      melghawi
      wrote on last edited by
      #2

      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
      0
      • I Offline
        I Offline
        iviv1
        wrote on last edited by
        #3

        Thank you, @moeg687 ! This helped a lot.

        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