anchors.fill: parent doesn't work with C++ types?
-
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 shows0
for both.How do I get the anchor property to work properly for my custom C++ type?
-
@Edwin-Vane I think the problem is more with the order of execution of
onCompleted
handler as said here. By the time theonCompleted
is executed the size is not set. To test it try fetchingwidth
andheight
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 }
-
@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 outwidth()
andheight()
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. -
@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/quickiosDid you checked the parent's width and height? In QML , the children can be visible even the size of parent is (0,0).
-
@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 ? -
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.