If we have object name. Is their any way of getting the class to which it belongs dynamically?
-
IF you mean the QObject inherited parent, then YES, the address of the parent ... well, is the parent :)
Also object instances CANNOT have names that begin with a number. You mean the caption of the button or some custom property or what?
-
[quote author="ddriver" date="1330085230"]IF you mean the QObject inherited parent, then YES, the address of the parent ... well, is the parent :)
Also object instances CANNOT have names that begin with a number.[/quote]
I mean that suppose i have object name can i find the object type before using
@this->findChild<typecast*>("object name");@ -
..ops sorry I misunderstood your inquiry
-
[quote author="ddriver" date="1330085956"]Acording to the DOC you can:
@ QPushButton *button = parentWidget->findChild<QPushButton *>("button1");@
[/quote]
ya i have seen that but is their any way to know dynamically to which class type we should typecast(like here we have QPushbutton but it is specific i want something generic).
Because i have multiple object of different type. -
[quote author="pratik041" date="1330085607"]
I mean that suppose i have object name can i find the object type before using.[/quote]
No, because template functions are resolved at compile time, not runtime. You will have to use qobject_cast().@
QObject object = findChild<QObject>("object name");QWidget widget = qobject_cast<QWidget>(object);
if(widget != 0)
{
// object is QWidget or subclass
...
}
QLayoutItem layoutItem = qobject_cast<QLayoutItem>(object);
if(layoutItem != 0)
{
// object is QLayoutItem or subclass
...
}
@ -
You can use "QMetaObject::className()":http://developer.qt.nokia.com/doc/qt-4.8/qmetaobject.html#className
Also, take a look at "QMetaObject":http://developer.qt.nokia.com/doc/qt-4.8/qmetaobject.html itself, and "QMetaClassInfo":http://developer.qt.nokia.com/doc/qt-4.8/qmetaclassinfo.html docs.
-
So the solution is dynamic cast or qobject_cast() to every possible type until a valid pointer is returned? That doesn't sound too elegant.
-
Yes. But you have to have different code paths for different types anyways, so I see no problem in that.
There is no "name cast". This just doesn't work.
@
SomeObject* someObject = new SomeObject;
...
// what type does anyPossibleObject have at compile time?
...* anyPossibleObject = name_cast<"anyPossibleTypeName">(someObject);// what interface does anyPossibleObject have at compile time?
anyPossibleObject->...();
@ -
Yes, this is the generic way to go, I was just curious if Qt provides some improvement as it does in many other areas.
I suppose it is possible to extend stock Qt classes with additional member variable to identify a set of pre-defined objects during runtime instead of casting one by one. Then a simple switch should be way faster than a chain of dynamic casts.
-
You can use QObject::inherits(), QObject::className() and qobject_cast(). The Qt meta object system provides you the complete class hierarchy from the most specialized class name up to the toplevel QObject.
-
[quote author="ddriver" date="1330097642"]Yes, this is the generic way to go, I was just curious if Qt provides some improvement as it does in many other areas.
I suppose it is possible to extend stock Qt classes with additional member variable to identify a set of pre-defined objects during runtime instead of casting one by one. Then a simple switch should be way faster than a chain of dynamic casts.[/quote]
Well, you won't win anything ;-)You always can identify QObject classes during runtime using QObject::metaObject()->className(), but in order to use it you will have to cast to it.
Each class has its unique interface (set of methods, members and properties) and in order to access this interface (invoke a method) you will have to use either a pointer of the class' type or the parent class' type, because C++ enforces strict typing. And this type has to be known at compile time.
So in order to distinct n different classes you will have in any case to cast n times.
If multiple object share the same interface they share the same base class, so they can be called through a base class pointer, to which you have to cast too.
Qt doesn't improve anything here because there is just nothing to improve. That's how C++ and any other non-interpreted language works ;-)