Loaded QQuickItem, how to set properties correct?



  • How can i set the build in properties right?
    I create a QQuickItem via QQmlComponent:

    @LoadQuickItem::LoadQuickItem(QQmlEngine* engine, QQuickItem* parentView, QObject *parent) : QObject(parent) {

    this->engine_ = engine;
    this->parentView_ = parentView;
    this->component_ = new QQmlComponent(engine, QUrl("qrc:///Item.qml"));
    this->obj_ = new QObject();
    this->continueLoading();
    }

    LoadQuickItem::~LoadQuickItem() {
    delete this->component_;
    delete this->obj_;
    delete this->item_;
    }

    void LoadQuickItem::continueLoading() {
    if (this->component_->isError()) {
    qWarning() << this->component_->errors();
    } else {
    qDebug() << "Create QQuickItem";

      this->obj_ = this->component_->create();
      QQmlEngine::setObjectOwnership(this->obj_, QQmlEngine::CppOwnership);
      this->item_ = qobject_cast<QQuickItem*>(this->obj_);
    
      this->item_->setParentItem(this->parentView_);
      this
    

    }
    }@

    Instance:
    @
    QQuickWindow wnd;
    QQmlEngine engine;
    LoadQuickItem* item1 = new LoadQuickItem(&engine, wnd.contentItem());
    qDebug() << (item1->getItem()->setProperty("id", "item1") ? "Succes" : "Failed: item1->getItem()->setProperty("id", "item1")");
    @

    Item.qml

    @import QtQuick 2.2
    import QtQuick.Controls 1.2
    import QtQuick.Controls.Styles 1.2

    Button {
    text:"Maximize"
    tooltip:"Press me"
    style: CustomStyle {}
    }@

    I want to set the property "id", but always get a false.
    The property ought to be known?

    Background, I would like load files via a QML and use them as templates. A QML file always contains only one control, eg a button.

    Any idear?


  • Moderators

    id is not a property, it is not visible to public Qt API. You can not change it in C++. All remaining properties are available and you can change them as you wish.


  • Moderators

    From the Doc:

    bq. Once an object instance is created, the value of its id attribute cannot be changed. While it may look like an ordinary property, the id attribute is not an ordinary property attribute, and special semantics apply to it;

    So you can't change it.



  • Thanks.

    how can I implement a dependency between two QQuickitems?
    Like:

    @
    LoadQuickItem* item1 = new LoadQuickItem(&engine, wnd.contentItem());
    LoadQuickItem* item2 = new LoadQuickItem(&engine, wnd.contentItem());
    item2->getItem()->setProperty("y", "item1.height")
    @


  • Moderators

    You can use property() to get the height of other Item.


  • Moderators

    [quote author="p3c0" date="1409819479"]You can use property() to get the height of other Item.[/quote]

    I think the OP wants to create a property binding from C++.



  • [quote author="p3c0" date="1409819479"]You can use property() to get the height of other Item.[/quote]

    Yes that works. But only in static way.

    I mean this construct, in case of resizing the window.
    @Button {
    id: button1
    width: parent.width / 100 * 20
    height: parent.height / 100 * 20
    }
    Button {
    id: button2
    width: parent.width / 100 * 20
    height: parent.height / 100 * 20
    y: button1.height
    }
    @


  • Moderators

    -Here is an example on property bindings in C++. "Link":http://qt-project.org/doc/qt-5/qtqml-tutorials-extending-chapter3-bindings-example.html.-

    Nope, sorry, that is not good enough.


  • Moderators

    Connect the heightChanged signal to a slot, then get the height of item1 and then setProperty to item2.


Log in to reply
 

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