How can I observe a custom qobject's slot?



  • I download the code from somewhere which can make me to catch the qobject's signals and slot :

    "Q4puGenericSignalSpy.h":http://140.122.184.221/Q4puGenericSignalSpy.h
    "Q4puGenericSignalSpy.cpp":http://140.122.184.221/Q4puGenericSignalSpy.cpp

    I write some code to observe some object
    @
    Q4puGenericSignalSpy* spy = new Q4puGenericSignalSpy(NULL);
    spy->spyOn(obj);
    QObject::connect(spy, SIGNAL(caughtSignal(const QString&)), this, SLOT(catchedSignal(const QString&)));
    connect(spy, SIGNAL(caughtSlot(const QString&)), this, SLOT(catchedSlot(const QString&)));
    @

    It means that you just make to spy to observe some object X, and when X emits signals or trigger slots, the spy will be notified.

    It works fine when it observes QMainWindow or any other GUI widgets.

    But if I observe a customed QObject like this:
    @
    class MyObj : public QObject
    {
    Q_OBJECT
    public:
    MyObj();
    public slots:
    void slot_fun1();
    }
    @

    The Q4puGenericSignalSpy only can catch the signal.
    Weird, QMainWindow is derived from QObject, too. Why do my customed QObject can't catch the slot?

    Does anybody meet this problem before?
    thanks again.



  • Because a slot is not a signal and you do only declare a slot, but no signal in your custom class.

    A signal sends information from one Object to many (or no, if none is connected) other objects

    A slot receives information from other Objects (again, 0 to n)

    A signal spy can only receive data. And as the name already states, it's a "signal spy", not a "slot spy".



  • Thank you, Volker
    Yes, but I do really get the notification of slot by the Q4puGenericSignalSpy.

    Here's the example :
    "http://qt4.digitalfanatics.org/articles/signalspy.html":http://qt4.digitalfanatics.org/articles/signalspy.html

    In this example, you can find that the signal spy also can intercept the slot.
    I've traced the source code of the Q4puGenericSignalSpy.
    I think it's no problem to intercept the slot of qobject.

    It can intercept the slot of QMainWindow, QDialog, etc.
    However, it can't intercept the slot of my custom QObject.
    Why? What's the difference between them?

    thanks again.
    Javid



  • The signal spy class you use, uses private Qt API. The code is old, so perhaps the internals changed in the meantime? But you are right, it is advertised to be able to incercept slot invokations as well. You'll have to dig into the internals of Qt to see what is going on.



  • -You can't intercept slots!-

    Slots are just functions that are called, not more. They have the possibility to be called via the meta data API, but they stay functions. This signals spy uses internal Meta data stuff to catch the slot invocations via Meta data API. If the slot is called directly, it's a pure function call and can't be intercepted.

    EDIT: wiped out an incorrect sentence, Gerolf



  • Oh! Gerolf is right!! I call the slot directly so the slot becomes a pure function call!

    After I modified some code (use the signal-slot mechanism), the spy can intercept the slot now!

    Thanks again!



  • [quote author="Gerolf" date="1301899401"]You can't intercept slots!

    slots are just functions that are called, not more. They have the possibility to be called via the meta data API, but they stay functions. This signals spy uses internal Meta data stuff to catch the slot invocations via Meta data API. If the slot is called directly, it's a pure function call and can't be intercepted.[/quote]

    Perhaps you should read the code that javid links to. It uses internal Qt hooks to get callbacks when a slot is invoked through the signal/slot mechanism. I did not know this, but it seems Qt has internal (private) API for this kind of thing.



  • Hi Andre, I'm sorry, I've only rewritten part of my post (look at the second half) and forgot to rewrite the first half :-)
    I looked at the code and also saw it. the important part of my post was:

    bq. This signals spy uses internal Meta data stuff to catch the slot invocations via Meta data API. If the slot is called directly, it’s a pure function call and can’t be intercepted.



  • Yes, of course. It would only catch invocations through the signal/slot system. Sorry, I missed that part in your post.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.