By: rich
EDIT: Well, it turned out to be pretty easy:
2005
26
Mar
26
Mar
I've started looking into porting KJSEmbed to Qt 4. To begin with, I've recreated most of the Q_CLASSINFO demo I posted recently using the Qt 4 equivalents. Listing the slots of an object is even easier than before, as is finding a marker interface:
const QMetaObject *moa = metaObject();
// Find the marker interface
int index = moa->indexOfClassInfo( "MyMetaInfo" );
QMetaClassInfo ci = moa->classInfo( index );
Q3CString cs = ci.value();
QLabel *l = new QLabel( cs, this, "hello label" );
l->adjustSize();
// List an object's slots
const QMetaObject *mo = l->metaObject();
// The loop lets us walk up the inheritance tree
do {
printf( "=================\n" );
for ( int i = 0 ; i < mo->memberCount(); i++ ) {
QMetaMember mm = mo->member(i);
if ( mm.memberType() != QMetaMember::Slot )
continue;
printf( "%s\n", mm.signature() );
}
mo = mo->superClass();
} while( mo );
It shouldn't be too hard to move on to calling slots from here.
EDIT: Well, it turned out to be pretty easy:
mo = l->metaObject();
bool ok = mo->invokeMember( l, "setText",
Q_ARG(QString, QString("I was called") ) );
printf( "Call result: %d\n", ok );
This sets the text of the label to 'I was called'.
- rich's blog
- Login or register to post comments
- 503 reads
Comments
Re: invokeMember()
Strange, I've got qt-x11-opensource-4.0.0-b1, and it doesn't have a QMetaObject::invokeMember() method - has it been recently added?
I was going to use QObject::qt_metacall() for the ruby bindings, but I haven't actually tried it yet.
I'm using a snapshot
The version I'm using is 4.0.0-b2-snapshot-20050318, I'm not sure if this is a new addition.
Re: I'm using a snapshot
I've fished around the trolltech site and can't find any way of downloading a snapshot - I can only find the beta 1 that I've already got. How do you get hold of a current snapshot?
Get snapshots from here
ftp://ftp.trolltech.com/qt/snapshots/
or ftp://ftp.silug.org/pub/qt/snapshots/
Re: Get snapshots from here
Great, thanks! I'm just building it now. I'll do some experiments - one thing I want to look at is the api for creating QMetaObjects on the fly.
Please write up what you find out
Please could you write up the results of this? I'm sure other people will need to do the same thing.