[Qt-qml] C++ MyClass to detect child added/removed from QML

Girish Ramakrishnan girish at forwardbias.in
Thu Nov 25 05:34:17 CET 2010


Hi,

On Thu, Nov 25, 2010 at 2:55 AM, Charley Bay <charleyb123 at gmail.com> wrote:
> I confirmed that these *do not* work for me:
>
> ...with a "Text { width: 50; height: 20 }" on the QML side,
>
> QGraphicsItem::boundingRect() // ALWAYS RETURNS QRectF(0,0,-1,-1)
> QGraphicsItem::opaqueArea().boundingRect() // ALWAYS RETURNS QRectF(0,0,0,0)
>
> ...however, after casting the QGraphicsItem* to a QObject*, I can correctly
> extract my "width" and "height" properties.
>
> So, my current conclusion is that I have *no* useful properties in
> QGraphicsItem (and must cross-cast it to something I can use).

boundingRect() works as I would expect. Can you provide some sample code?

Rectangle {
    width: 300
    height: 300
    objectName: "rect"

    Text {
        objectName: "text"
        text: "foo"
    }
}


QDeclarativeItem *rect = qobject_cast<QDeclarativeItem *>(view->rootObject());
qDebug() << rect->boundingRect();
QDeclarativeItem *text = rect->findChild<QDeclarativeItem *>("text");
qDebug() << text->boundingRect();

Output:
QRectF(0,0 300x300)
QRectF(0,0 24x17)

The first one is the rect of the top level item (and it matches what I
set in qml) and the second one is the text item's computed
width/height. So, the values are as I would expect.

Girish



More information about the Qt-qml mailing list