Fit the size to the parent for QQuickItem C++ Object



  • My environment is Qt 5.4 desktop MSVC2013 and QtQuick 2.4 on Windows 8.1 64bit.

    I want to fill the custom qml object extended by QQuickItem on the parent and I wrote the code like this.

    //QQuickItem *item;
    item->setParentItem(parentItem);
    (*case 1)
    item->setProperty("anchors.fill", "parent");
    (*case 2)
    item->setProperty("width", "parent.width");
    item->setProperty("height", "parent.height");
    

    But, those didn't work. If these ways cannot be used, is there any way alternative to implementing it only by C++?


  • Moderators

    Hi @Tank2005,
    AFAIK setting anchors wont work from C++ side. You have to either do it on QML side or you can calculate the x and y positions programmatically on C++ side and set them. The latter can be done following way:

    child->setProperty("x",parentItem->width()/2-child->width()/2); //calculate and set
    child->setProperty("y",parentItem->height()/2-child->height()/2);
    
    

    Edit: Since you want to fill you can use parentItem's width and height

    item->setProperty("width", parentItem.width);
    item->setProperty("height", parentItem.height);
    

Log in to reply
 

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