Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How can I observe a custom qobject's slot?
QtWS25 Last Chance

How can I observe a custom qobject's slot?

Scheduled Pinned Locked Moved General and Desktop
9 Posts 4 Posters 5.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    javid
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • G Offline
      G Offline
      goetz
      wrote on last edited by
      #2

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

      http://www.catb.org/~esr/faqs/smart-questions.html

      1 Reply Last reply
      0
      • J Offline
        J Offline
        javid
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andre
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • G Offline
            G Offline
            giesbert
            wrote on last edited by
            #5

            -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

            Nokia Certified Qt Specialist.
            Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

            1 Reply Last reply
            0
            • J Offline
              J Offline
              javid
              wrote on last edited by
              #6

              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!

              1 Reply Last reply
              0
              • A Offline
                A Offline
                andre
                wrote on last edited by
                #7

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

                1 Reply Last reply
                0
                • G Offline
                  G Offline
                  giesbert
                  wrote on last edited by
                  #8

                  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.

                  Nokia Certified Qt Specialist.
                  Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    andre
                    wrote on last edited by
                    #9

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

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved