How does Qt implement signals and slots?

后端 未结 3 727
暗喜
暗喜 2020-12-14 09:49

Can someone explain to me the basic idea of Qt signals&slots mechanism IMPLEMENTATION? I want to know what all those Q_OBJECT macros do \"in plain C++\". This question i

相关标签:
3条回答
  • 2020-12-14 09:56

    Those macros do absolutely nothing "in plain C++", - they expand to empty strings (I think).

    QT uses a meta-object compiler, that generates C++ code for Q_OBJECT-enabled classes (implementing the signals/slots you define, among other things).

    You can read more about it in the official documentation.

    0 讨论(0)
  • 2020-12-14 09:59

    Concerning the signals and slots, the Q_OBJECT macro adds a virtual function qt_metacall() declaration into the class’s declaration which is to be defined later by the the moc. (It also adds some declarations for conversion but that’s not too important here.)

    The moc then reads the header file and when it sees the macro, it generates another .cpp file named moc_headerfilename.cpp with the definitions to the virtual functions and – you might have asked yourself why you can get away with mentioning the signals: in your header file without a proper definition – of the signals.

    So, when a signal is called, the definition from the mocfile is executed and QMetaObject::activate() is called with the signal’s name and the signal’s arguments. The activate() function then figures out which connections have been established and fetches the names for the appropriate slots.

    Then it calls qt_metacall with the slot names and the arguments given to the signal and the metacall function delegates this with the help of a large switchcase statement to the real slots.

    As there is no real runtime information possible in C++ concerning the actual names for the signals and slots, as has already been noticed, these will be encoded by the SIGNAL and SLOT macros to simple const char*s (with either "1" or "2" added to the name to distinguish signals from slots).

    As is defined in qobjectdefs.h:

    #define SLOT(a)     "1"#a
    #define SIGNAL(a)   "2"#a
    

    The other thing the Q_OBJECT macro does is defining the tr() functions inside your object which can be used to translate your application.

    Edit As you asked what the qt_metacast is doing. It checks whether an object belongs to certain class and if it does returns the pointer to it. If it doesn’t, it returns 0.

    Widget* w = new Widget();
    Q_ASSERT(w->qt_metacast("Widget") != 0);
    Q_ASSERT(w->qt_metacast("QWidget") != 0);
    Q_ASSERT(w->qt_metacast("QObject") != 0);
    Q_ASSERT(w->qt_metacast("UnrelatedClass") == 0);
    

    This is needed to provide some runtime reflection which is not possible otherwise. The function is called in QObject::inherits(const char *) for example and simply checks for inheritance.

    0 讨论(0)
  • 2020-12-14 10:02

    The basic idea is that you can connect your objects allowing them to execute a method (slot) when a signal is done.

    connect(pistol,SIGNAL(sigShoot()),runner,SLOT(slotRun()))
    

    Doing the connection above, when the pistol emits the signal, the runner will execute its slot.

    To do this, you have to declare your signals and slots in your respective classes.

    Is the basic idea.

    Good luck!

    0 讨论(0)
提交回复
热议问题