Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 2

Qt-interest Archive, March 2001
determine Widgettype at runtime


Message 1 in thread

HI.


I want to determine at runtime the class of an object.
My first idea was to do it with parent () and typeid, but parent()
return QObject*.

assert( typeid( this->parent() ) == typeid( QWidget ) ) ;

Is there an easy QT-way to know, of which classtype my Object is.
If not, I must derive my own classes from QWidget an give them a methode
widgetClassID().

Thanks in advance.

Rainer


Message 2 in thread

Hello,

On Wed, Mar 07, 2001 at 11:05:03AM +0100, Rainer Grimm wrote:
> Is there an easy QT-way to know, of which classtype my Object is.
> If not, I must derive my own classes from QWidget an give them a methode
> widgetClassID().

Yes, you can use QObject::className() for this.

Greetings,
Wilco


Message 3 in thread

Am Mittwoch,  7. März 2001 11:05 schrieb Rainer Grimm:
> HI.
>
>
> I want to determine at runtime the class of an object.
> My first idea was to do it with parent () and typeid, but parent()
> return QObject*.
>
> assert( typeid( this->parent() ) == typeid( QWidget ) ) ;
>
> Is there an easy QT-way to know, of which classtype my Object is.
> If not, I must derive my own classes from QWidget an give them a methode
> widgetClassID().
QObject::className() tells you the actual class name of the widget (as 
QWidget inherits QObject) and QObject::inherits(const char *classname) 
returns true if the object is or inherits the class given by name. 
Just make sure you have a Q_OBJECT macro in the class declaration as this 
works via the meta class.
-Malte


Message 4 in thread

Rainer Grimm wrote:
> 
> HI.
> 
> I want to determine at runtime the class of an object.
> My first idea was to do it with parent () and typeid, but parent()
> return QObject*.
> 
> assert( typeid( this->parent() ) == typeid( QWidget ) ) ;
> 
> Is there an easy QT-way to know, of which classtype my Object is.
> If not, I must derive my own classes from QWidget an give them a methode
> widgetClassID().
> 
> Thanks in advance.
> 
> Rainer
> 
> --
> List archive and information: http://qt-interest.trolltech.com


All right. Thanks everybody for your help.

Rainer


Message 5 in thread

> HI.
> 
> 
> I want to determine at runtime the class of an object.
> My first idea was to do it with parent () and typeid, but parent()
> return QObject*.
> 
> assert( typeid( this->parent() ) == typeid( QWidget ) ) ;
> 
> Is there an easy QT-way to know, of which classtype my Object is.
> If not, I must derive my own classes from QWidget an give 
> them a methode
> widgetClassID().

You might want to look into:
QObject::isA
QObject::inherits 
and the stuff to be found in the QMetaObject

Regards..
-- 
 [ signature omitted ] 

Message 6 in thread

> > I want to determine at runtime the class of an object.
> > My first idea was to do it with parent () and typeid, but parent()
> > return QObject*.
> >
> > assert( typeid( this->parent() ) == typeid( QWidget ) ) ;
> >
> > Is there an easy QT-way to know, of which classtype my Object is.
> > If not, I must derive my own classes from QWidget an give
> > them a methode
> > widgetClassID().
>
> You might want to look into:
> QObject::isA
> QObject::inherits
> and the stuff to be found in the QMetaObject

There was also recently a disscution on this list of the use of dynamic_cast
(a C++ and therefore maybe a Qt way). Instead of bringing the debate up
again you might want to check some archives.

--
 [ signature omitted ] 

Message 7 in thread


<<< text/html: EXCLUDED >>>


Message 8 in thread

Hi,

every QObject-derived class has a method called className() that returns exactly the class name (like "QWidget").
It also works with derived classes as the code is implemented in the moc-generated file.

cheers,
 teddy

> HI.
> 
> 
> I want to determine at runtime the class of an object.
> My first idea was to do it with parent () and typeid, but parent()
> return QObject*.
> 
> assert( typeid( this->parent() ) == typeid( QWidget ) ) ;
> 
> Is there an easy QT-way to know, of which classtype my Object is.
> If not, I must derive my own classes from QWidget an give them a methode
> widgetClassID().
> 
> Thanks in advance.
> 
> Rainer
> 
> --
> List archive and information: http://qt-interest.trolltech.com