QQuickItem from C++ to Item in QML

  • 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 :)

  • Moderators


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

  • 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++

  • Here is a little update on what I have.

    MyView.qml :
    property color topColor
    property color bottomColor

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


    @class MyObject
    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:

    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.

  • Moderators

    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?

  • Code is in the post just before yours ;)

  • Moderators

    [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?

  • That is what I'm trying to do.

    I now have this in my root qml

    id: viewToShow
    width: stackView.width
    height: stackView.height

    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..

  • I finally got it to work using Bindings.

Log in to reply

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