Details
-
Bug
-
Resolution: Unresolved
-
P3: Somewhat important
-
None
-
4.8.x, 5.5.0
-
None
Description
qobject_cast documentation appears to be wrong.
Appears to be wrong in all version of 4.8 & 5
Example: http://doc.qt.io/qt-4.8/qobject.html#qobject_cast
Doc for "T qobject_cast(QObject * object)" says "The class T must inherit (directly or indirectly) QObject and be declared with the Q_OBJECT macro."
However, after my investigation, this doesn't appear to be the case. Instead I believe that object (the argument passed to qobject_cast) must be of a type that inherits (directly or indirectly) from QObject. However type T can be of any type (the cast will only succeed if it is a base type of object).
This is an important distinction for those implementing certain plugins, specifically using a collection concept as is done in the example below:
As an example of a type T that doesn't inherit from QObject see QDesignerCustomWidgetCollectionInterface (C:\Qt\4.8.4\tools\designer\src\lib\uilib\customwidget.h).
As an example of an object argument that inherits from it see PhononCollection (C:\Qt\4.8.4\tools\designer\src\plugins\phononwidgets\phononcollection.cpp).
As an example of using qobject_cast to convert/cast to a type that does not inherit (directly or indirectly) from QObject see the helper function insertPlugins(...) from FormBuilder (C:\Qt\4.8.4\tools\designer\src\lib\uilib\formbuilder.cpp), among others.
Finally, it might be helpful to have a small, complete, example of this concept. I had to dig these out myself as there is no complete example of how to implement a plugin using QDesignerCustomWidgetCollectionInterface. An example would be more of an enhancement suggestion, but the qobject_cast doc is more of a bug.