Skip navigation.
KDE Developer's Journals

Preparing for Qt4

rich's picture

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'.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
richard dale's picture

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.

rich's picture

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.

richard dale's picture

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?

rich's picture

Get snapshots from here

ftp://ftp.trolltech.com/qt/snapshots/
or ftp://ftp.silug.org/pub/qt/snapshots/

richard dale's picture

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.

rich's picture

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.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.