[Qt-qml] C++ MyClass to detect child added/removed from QML
Charley Bay
charleyb123 at gmail.com
Fri Nov 26 15:52:28 CET 2010
Girish spaketh:
> boundingRect() works as I would expect. Can you provide some sample code?
> <snip, example extracting good coordinates from
> QDeclarativeItem::boundingRect()>
>
Was your C++ code inside an application-derived
QDeclarativeItem::itemChange()?
I'm Qt4.7.0, Win7-64bit, MSVC++2008.
I tried it again to make sure there was no difference between my
"dynamic_cast<>()" and your "qobject_cast<>()", and they behave the same.
(I suppose I should be doing qobject_cast<> anyway).
My code subset:
-------------------------
//FILE: MyQml.qml
import Qt 4.7
import MyPluginLayout 1.0
Rectangle {
MyPluginLayout {
id: myPluginLayout
}
Text {
text: "hello"
parent: myPluginLayout
width: 40
height: 30
}
}
-------------------------
//FILE: MyPluginLayout.cpp
class MyPluginLayout : public SdqDeclarativeItem
{
...Q_OBJECT, Q_PROPERTY, ...
public:
virtual QVariant itemChange(
QGraphicsItem::GraphicsItemChange change,
const QVariant& value)
{
if(change == QGraphcisItem::ItemChildAddedChange)
{
QGraphicsItem* graphics_item = value.value<QGraphicsItem*>();
ASSERT(graphics_item);
//DEBUGGER SHOWS VTABLE FOR "graphics_item" TO BE A QDeclarativeText
QRectF bounding_rect;
bounding_rect = graphics_item->boundingRect(); // (0,0,-1,-1)
bounding_rect =
qobject_cast<QGraphicsObject*>(graphics_item)->boundingRect(); //
(0,0,-1,-1)
bounding_rect =
qobject_cast<QDeclarativeItem*>(graphics_item)->boundingRect(); //
(0,0,-1,-1)
double value;
value =
qobject_cast<QObject*>(graphics_item)->property("width").toReal(); // 40
value =
qobject_cast<QObject*>(graphics_item)->property("height").toReal(); // 30
value =
dynamic_cast<QObject*>(graphics_item)->property("width").toReal(); // 40
value =
dynamic_cast<QObject*>(graphics_item)->property("height").toReal(); // 30
//...do layout stuff to place item added...
}
};
-----------------------
On the bright side, all of the boundingRect() calls agree. They just don't
have the width/height property values. So, I'm using "QObject::property()",
which seems to always work.
--charley
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt.nokia.com/pipermail/qt-qml/attachments/20101126/b30e1dc0/attachment.html
More information about the Qt-qml
mailing list