[Development] Q_Q(), static_cast or reinterpret_cast?
Alberto Mardegan
mardy at users.sourceforge.net
Fri Jun 29 11:14:41 CEST 2012
Hi all,
I just run into an issue in Qt 4.8 QDeclarativeItem (but please read
on, the topic regards qt5 too): the geometryChanged() virtual method is
not invoked when the size of the item is changed by modifying the
"width" or "height" properties (for instance, from QML).
The QDeclarativeItem's geometryChanged() method, however, is called (I
can tell it because it's the only place where the widthChanged() signal
is emitted, and I verified that the signal is indeed emitted).
Here is the code of setWidth():
===========
void QDeclarativeItemPrivate::setWidth(qreal w)
{
Q_Q(QDeclarativeItem);
if (qIsNaN(w))
return;
widthValid = true;
if (mWidth == w)
return;
qreal oldWidth = mWidth;
q->prepareGeometryChange();
mWidth = w;
q->geometryChanged(QRectF(q->x(), q->y(), width(), height()),
QRectF(q->x(), q->y(), oldWidth, height()));
}
===========
So, it seems that q->geometryChanged() invokes just
QDeclarativeItem::geometryChanged(), ignoring the fact that it's a
virtual method. That is because the q_func() invoked with the Q_Q()
macro uses a static_cast(), which doesn't care about derived classes,
opposed to Q_D() which uses a reinterpret_cast().
Is there some good reason not to use reinterpret_cast() in q_func()?
If so, I will file a bug just against QDeclarativeItem, but I suspect
that the pattern of calling virtual methods through the "q" pointer
might be easily found in other classes too.
Note that q_func() uses static_cast() in Qt5 too.
Ciao,
Alberto
More information about the Development
mailing list