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. QQuickItem from C++ to Item in QML

QQuickItem from C++ to Item in QML

Scheduled Pinned Locked Moved QML and Qt Quick
9 Posts 2 Posters 4.7k 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.
  • M Offline
    M Offline
    MaxL
    wrote on last edited by
    #1

    Hi everyone,

    I was trying to pass on a QQuickItem ( created from a .qml file ) to my root view but i cannot find a way to do it.
    I tried using a signal as well as a calling a js function inside that view but nothing works since I cannot cast QQuickItem into a QVariant.

    Does anyone have an idea on the matter ?

    Thanks :)

    1 Reply Last reply
    0
    • JKSHJ Offline
      JKSHJ Offline
      JKSH
      Moderators
      wrote on last edited by
      #2

      Hi,

      What is your goal? Since you already have the QML file, you can instantiate your Item directly in QML without using C++, right?

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      1 Reply Last reply
      0
      • M Offline
        M Offline
        MaxL
        wrote on last edited by
        #3

        Hi,

        That is true, but I have objects in C++ that each hold a view from this qml file and set some properties so I can't instantiate it from QML, it needs to be done in C++

        1 Reply Last reply
        0
        • M Offline
          M Offline
          MaxL
          wrote on last edited by
          #4

          Here is a little update on what I have.

          MyView.qml :
          @Item
          {
          property color topColor
          property color bottomColor

           Rectangle
           {
              id: topRect
              anchors.top: parent.top
              width: parent.width
              height: parent.height * 0.75
              color: topColor
          }
          Rectangle
          {
              id: bottomRect
              anchors.top: topRect.bottom
              width: parent.width
              height: parent.height * 0.25
              color: bottomColor
          }
          

          }@

          MyObject:
          @class MyObject
          {
          public:
          QQuickItem* view
          }@

          When creating a MyObject it instantiate its view property based on MyView.qml. It also sets some specific properties such as colors.
          When I want to dislay that object a signal is emitted ( emit( showMyObject( myObject --> view) )

          In my root qm that holds a stackview l have a signal that is showMyObject( Item view ) and in the slot i do the following:

          @onShowMyObject:
          {
          view.width = stackView.width
          view.height = stackView.height
          stackView.push( view )
          }@

          It does push the view but it appears that no height neither width is set so topRect and bottomRect have width and height still at 0. If I set a width and height to the root Item of MyView.qml then it not changed in the slot. It keeps the same height and width as they are set by default in the .qml

          edit: I should add that if I read width and height property of "view" after pushing it, they are correctly set.

          1 Reply Last reply
          0
          • JKSHJ Offline
            JKSHJ Offline
            JKSH
            Moderators
            wrote on last edited by
            #5

            You can pass the properties of your old QQuickItem into your root view, and use those properties to set up a new Item (and then delete your old item)

            However, you cannot pass a QQuickItem back into QML directly because "QObjects cannot be copied":http://qt-project.org/doc/qt-5/object.html#qt-objects-identity-vs-value.

            Can you please share some of your existing code?

            Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

            1 Reply Last reply
            0
            • M Offline
              M Offline
              MaxL
              wrote on last edited by
              #6

              Code is in the post just before yours ;)

              1 Reply Last reply
              0
              • JKSHJ Offline
                JKSHJ Offline
                JKSH
                Moderators
                wrote on last edited by
                #7

                [quote author="MaxL" date="1393158311"]Code is in the post just before yours ;)[/quote]Yep, saw it. I was too slow with typing ;)

                Anyway, what about instantiating Items in QML, then getting your C++ code to update their properties?

                Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  MaxL
                  wrote on last edited by
                  #8

                  That is what I'm trying to do.

                  I now have this in my root qml

                  @Component
                  {
                  id: viewToShow
                  MyView
                  {
                  width: stackView.width
                  height: stackView.height
                  }
                  }

                  onShowMyObject
                  {
                  stackView.push( { item: viewToShow, properties: { topColor: view.topColor, bottomColor: view.bottomColor } } )
                  }@

                  This does work, but the problem is that there is no binding between properties so when I update colors in C++ there are not in QML..

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    MaxL
                    wrote on last edited by
                    #9

                    I finally got it to work using Bindings.

                    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