[Qt-interest] signals and slots....revisited...

Sean Harmer sean.harmer at maps-technology.com
Mon Mar 14 16:10:13 CET 2011


Hi,

On Monday 14 March 2011 13:52:54 sarvesh saran wrote:
> Hi Bo,
> 
> thank you for your answer. There was one question that is still
> unanswered...
> 
> 5. QT stores the names slots and signals of an object in its  meta object?
> 
> > in what format.....<name,function pointer> ?
> 
> *Something like it, yes. But you don't need to know. Just use the SLOT and
> SIGNAL macros to access them.*
> *
> *
> I would like to know...at least at a higher abstraction. The SLOT and SIGNAL
> macros would generate a string representation of the signal and the slot
> containing the name, arguments etc (kind like a mangled C++ method name?).
> These names are then looked up (where?) and the appropriate slots are
> called ( i guess through the meta object?..qt_metacall).
> 
> I want this info for a variety of reasons...i was under the belief that
> signals and slots involve function pointers until some one very recently
> contradicted this...so i would like some clarification as to how the
> signals/slots are mapped,looked up and called?

The information is stored in a character array in the generated 
moc_myclass.cpp file. For a simple class declaration like this:

class MyClass : public QObject
{
    Q_OBJECT
public:
    explicit MyClass( QObject* parent = 0 );

    int data() const;

signals:
    void dataChanged( int newData );

public slots:
    void setData( int data );
    void increment();

private:
    int m_data;
};

moc generates the following:

static const char qt_meta_stringdata_MyClass[] = {
    "MyClass\0\0newData\0dataChanged(int)\0"
    "data\0setData(int)\0increment()\0"
}

This contains the information describing the signals and slots.

Now the other part of the puzzle is how this information is looked up to do 
something useful. The way this is done is with a corresponding uint array. In 
this case moc generates:

static const uint qt_meta_data_MyClass[] = {

 // content:
       5,       // revision
       0,       // classname
       0,    0, // classinfo
       3,   14, // methods
       0,    0, // properties
       0,    0, // enums/sets
       0,    0, // constructors
       0,       // flags
       1,       // signalCount

 // signals: signature, parameters, type, tag, flags
      17,    9,    8,    8, 0x05,

 // slots: signature, parameters, type, tag, flags
      39,   34,    8,    8, 0x0a,
      52,    8,    8,    8, 0x0a,

       0        // eod
};

The numbers are (for the most part) simply offsets into the stringdata array.

This page gives a little more information on this structure:

http://dev.libqxt.org/libqxt/wiki/Meta_Object_Format

You really do not need to know all of this to actually use it though since the 
moc does it all for you. What is you use case? Are you trying to extend moc or 
something?

Sean

ps Also be aware that several macros actually expand to nothing as far as the 
C++ compiler is concerned but the moc uses them as indications to generate 
some code.




More information about the Qt-interest-old mailing list