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. anchors.fill: parent doesn't work with C++ types?
QtWS25 Last Chance

anchors.fill: parent doesn't work with C++ types?

Scheduled Pinned Locked Moved QML and Qt Quick
c++
6 Posts 3 Posters 3.3k 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.
  • E Offline
    E Offline
    Edwin Vane
    wrote on last edited by
    #1

    I have a very simple sublass of QQuickItem following the online code samples. My custom type is like a mouse area in that it doesn't have any visible content. When I instantiate the type in QML and specifiy anchors.fill: parent the width and height are not set (Component.onCompleted debug output shows 0 for both.

    How do I get the anchor property to work properly for my custom C++ type?

    p3c0P 1 Reply Last reply
    0
    • E Edwin Vane

      I have a very simple sublass of QQuickItem following the online code samples. My custom type is like a mouse area in that it doesn't have any visible content. When I instantiate the type in QML and specifiy anchors.fill: parent the width and height are not set (Component.onCompleted debug output shows 0 for both.

      How do I get the anchor property to work properly for my custom C++ type?

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

      @Edwin-Vane I think the problem is more with the order of execution of onCompleted handler as said here. By the time the onCompleted is executed the size is not set. To test it try fetching width and height on some button click.

      MyItem {
          id: item
          anchors.fill: parent
      }
      
      Button {
          text: "Click"
          onClicked: console.log(item.width,item.height) //Should print the new size
      }
      

      157

      E 1 Reply Last reply
      0
      • p3c0P p3c0

        @Edwin-Vane I think the problem is more with the order of execution of onCompleted handler as said here. By the time the onCompleted is executed the size is not set. To test it try fetching width and height on some button click.

        MyItem {
            id: item
            anchors.fill: parent
        }
        
        Button {
            text: "Click"
            onClicked: console.log(item.width,item.height) //Should print the new size
        }
        
        E Offline
        E Offline
        Edwin Vane
        wrote on last edited by
        #3

        @p3c0 I don't think this is related to onCompleted. I tried exactly as you suggested and still get 0 for both width and height. I even print out width() and height() from the C++ side and they too are zero. It just seems the anchor information is never set. There's clearly something a QtQuick subclass needs to do to get the anchor information but I can't find the info anywhere.

        benlauB p3c0P 2 Replies Last reply
        0
        • E Edwin Vane

          @p3c0 I don't think this is related to onCompleted. I tried exactly as you suggested and still get 0 for both width and height. I even print out width() and height() from the C++ side and they too are zero. It just seems the anchor information is never set. There's clearly something a QtQuick subclass needs to do to get the anchor information but I can't find the info anywhere.

          benlauB Offline
          benlauB Offline
          benlau
          Qt Champions 2016
          wrote on last edited by
          #4

          @Edwin-Vane You don't need to do anything special to make the anchor work. I have a working QQuickItem subclass and set size via "anchors.fill:parent".

          quickios/ActionSheetDemo.qml at master · benlau/quickios
          quickios/qiactionsheet.cpp at master · benlau/quickios

          Did you checked the parent's width and height? In QML , the children can be visible even the size of parent is (0,0).

          1 Reply Last reply
          0
          • E Edwin Vane

            @p3c0 I don't think this is related to onCompleted. I tried exactly as you suggested and still get 0 for both width and height. I even print out width() and height() from the C++ side and they too are zero. It just seems the anchor information is never set. There's clearly something a QtQuick subclass needs to do to get the anchor information but I can't find the info anywhere.

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

            @Edwin-Vane AFAIK there's nothing more required. TO demonstrate it consider the following simple example:
            MyItem - subclass of QQuickItem

            #include <QObject>
            #include <QQuickItem>
            
            class MyItem : public QQuickItem
            {
                Q_OBJECT
            public:
                MyItem() {}
            };
            

            Register it

            #include "myitem.h"
            qmlRegisterType<MyItem>("MyItem", 1, 0, "MyItem");
            
            QQmlApplicationEngine engine;
            engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
            

            Using it in main.qml

            import MyItem 1.0
            
            Window {
                visible: true
                width: 300
                height: 300
            
                MyItem {
                    id: item
                    anchors.fill: parent
                    Component.onCompleted: console.log("onCompleted: ",item.width,item.height)
                }
            
                Button {
                    text: "Click"
                    onClicked: console.log(item.width,item.height)
                }
            }
            

            If you run the above code you will find that Component.onCompleted doesn't print what is expected but on button click the item's width and height are obtained correctly.
            Can you post your code ?

            157

            1 Reply Last reply
            0
            • E Offline
              E Offline
              Edwin Vane
              wrote on last edited by
              #6

              Thanks for the feedback. I must be doing something silly somewhere. I'll try and distill my problem down to a postable code sample if I don't solve the problem in the process.

              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