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. QuickView rootObjects()
Qt 6.11 is out! See what's new in the release blog

QuickView rootObjects()

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
9 Posts 4 Posters 1.5k 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.
  • C Offline
    C Offline
    Cyclotech
    wrote on last edited by
    #1

    I am trying to run a code like this but the object pointer is always NULL.
    I have different qml files but the object pointers are always NULL.
    Do you have any idea?
    Thank you

    // Using QQuickView
    QQuickView view;
    view.setSource(QUrl::fromLocalFile("MyItem.qml"));
    view.show();
    QObject *object = view.rootObject();

    jsulmJ JoeCFDJ 2 Replies Last reply
    0
    • C Cyclotech

      I am trying to run a code like this but the object pointer is always NULL.
      I have different qml files but the object pointers are always NULL.
      Do you have any idea?
      Thank you

      // Using QQuickView
      QQuickView view;
      view.setSource(QUrl::fromLocalFile("MyItem.qml"));
      view.show();
      QObject *object = view.rootObject();

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Cyclotech said in QuickView rootObjects():

      view.rootObject

      You're calling it right after calling show() which is asynchronous. My guess is that at that time view is not yet ready. You should try to callrootObject later, for example if https://doc.qt.io/qt-6/qquickview.html#statusChanged changes to QQuickView::Ready.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • C Cyclotech

        I am trying to run a code like this but the object pointer is always NULL.
        I have different qml files but the object pointers are always NULL.
        Do you have any idea?
        Thank you

        // Using QQuickView
        QQuickView view;
        view.setSource(QUrl::fromLocalFile("MyItem.qml"));
        view.show();
        QObject *object = view.rootObject();

        JoeCFDJ Offline
        JoeCFDJ Offline
        JoeCFD
        wrote on last edited by JoeCFD
        #3

        @Cyclotech said in QuickView rootObjects():

        // Using QQuickView
        QQuickView view;
        view.setSource(QUrl::fromLocalFile("MyItem.qml"));
        view.show();
        QObject *object = view.rootObject();

        jsulm is right. But it is better that you code it using pointer.

        // Using QQuickView
        auto view = new QQuickView;
        view->setSource(QUrl::fromLocalFile("MyItem.qml"));
        view->setResizeMode( QQuickWidget::SizeRootObjectToView );
        QObject *object = view->rootObject();
        view->setVisible( true );
        
        
        GrecKoG 1 Reply Last reply
        0
        • JoeCFDJ JoeCFD

          @Cyclotech said in QuickView rootObjects():

          // Using QQuickView
          QQuickView view;
          view.setSource(QUrl::fromLocalFile("MyItem.qml"));
          view.show();
          QObject *object = view.rootObject();

          jsulm is right. But it is better that you code it using pointer.

          // Using QQuickView
          auto view = new QQuickView;
          view->setSource(QUrl::fromLocalFile("MyItem.qml"));
          view->setResizeMode( QQuickWidget::SizeRootObjectToView );
          QObject *object = view->rootObject();
          view->setVisible( true );
          
          
          GrecKoG Offline
          GrecKoG Offline
          GrecKo
          Qt Champions 2018
          wrote on last edited by GrecKo
          #4

          @JoeCFD Not really. Why do you say it's better?

          @Cyclotech Why do you want to access your rootObject, it is generally considered a bad practice and should be avoided.
          Expose properties and slots in C++ instead of accessing your QML objects from C++.

          jsulmJ JoeCFDJ 3 Replies Last reply
          0
          • GrecKoG GrecKo

            @JoeCFD Not really. Why do you say it's better?

            @Cyclotech Why do you want to access your rootObject, it is generally considered a bad practice and should be avoided.
            Expose properties and slots in C++ instead of accessing your QML objects from C++.

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @GrecKo said in QuickView rootObjects():

            Not really. Why do you say it's better?

            Because current code is using stack allocated object (view) which will be destroyed as soon as it gets out of scope. I did not comment on that to see whether OP will discover that by himself :-)

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            GrecKoG 1 Reply Last reply
            0
            • jsulmJ jsulm

              @GrecKo said in QuickView rootObjects():

              Not really. Why do you say it's better?

              Because current code is using stack allocated object (view) which will be destroyed as soon as it gets out of scope. I did not comment on that to see whether OP will discover that by himself :-)

              GrecKoG Offline
              GrecKoG Offline
              GrecKo
              Qt Champions 2018
              wrote on last edited by
              #6

              If it's done in the main it's not a problem, I should have said so.
              Your code is potentially leaking.

              jsulmJ 1 Reply Last reply
              0
              • GrecKoG GrecKo

                If it's done in the main it's not a problem, I should have said so.
                Your code is potentially leaking.

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @GrecKo I did not post any code.
                Of course heap allocated memory needs to be released at some point, but these are basics.

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • GrecKoG GrecKo

                  @JoeCFD Not really. Why do you say it's better?

                  @Cyclotech Why do you want to access your rootObject, it is generally considered a bad practice and should be avoided.
                  Expose properties and slots in C++ instead of accessing your QML objects from C++.

                  JoeCFDJ Offline
                  JoeCFDJ Offline
                  JoeCFD
                  wrote on last edited by JoeCFD
                  #8

                  @GrecKo For example in my app, I need it to find a child item inside a qml and set it as sink in rtsp streaming. Is there any other way around?

                  Ok, I guess I can use qwidgets to wrap to this item. Good to know it, thanks. I realized QQuickView is very sensitive when it is put into a layout of qwidgets. The experience is that make qml as simple as possible for QQuickView or QQuickWidget.

                  1 Reply Last reply
                  0
                  • GrecKoG GrecKo

                    @JoeCFD Not really. Why do you say it's better?

                    @Cyclotech Why do you want to access your rootObject, it is generally considered a bad practice and should be avoided.
                    Expose properties and slots in C++ instead of accessing your QML objects from C++.

                    JoeCFDJ Offline
                    JoeCFDJ Offline
                    JoeCFD
                    wrote on last edited by JoeCFD
                    #9

                    @GrecKo It is also a general practice to create Qt widgets and items with heap memory. It is better to get used to it. Heap memory clean-up when needed is some basic skill of a C++ coder.

                    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