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. Fit the size to the parent for QQuickItem C++ Object

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

Scheduled Pinned Locked Moved QML and Qt Quick
c++qmlqt5.4qtquick
2 Posts 2 Posters 2.6k Views 2 Watching
  • 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.
  • T Offline
    T Offline
    Tank2005
    wrote on last edited by Tank2005
    #1

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

    p3c0P 1 Reply Last reply
    0
    • T Tank2005

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

      p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by p3c0
      #2

      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);
      

      157

      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