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. Basic question about signal slot
Forum Updated to NodeBB v4.3 + New Features

Basic question about signal slot

Scheduled Pinned Locked Moved General and Desktop
15 Posts 4 Posters 5.5k Views 1 Watching
  • 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.
  • D Offline
    D Offline
    dolevo
    wrote on last edited by
    #1

    Hi there,

    I will illustrate my question with an example:
    @
    class A: public QObject, public B
    {
    Q_OBJECT
    //Other stuff

    public slots:
    void SetTheThreshold(float aTarget);
    }

    //Somewhere in the constructor of class A
    A::A(void)
    {
    //Other stuff
    connect(this, SLOT(SetTheThreshold(float)), this, SIGNAL(sgSetTheThreshold(float)));
    }

    void A::SetTheThreshold(float aTarget);
    {
    //Do something with aTarget;
    }

    class B
    {
    //Other things

    signals:
    void sgSetTheThreshold(float aTarget);
    }
    @

    Above code gives linker error:
    @
    error: LNK2019: unresolved external symbol "protected: void __thiscall B::sgSetTheThreshold(float)" (?sgSetTheThreshold@A@@IAEXM@Z) referenced in function "public: void __thiscall A::SetTheThresholdReceived(float)" (?SetTheThreshold@A@@QAEXM@Z)
    @

    I didn't get the error. How can I solve this? SetTheThresholdReceived in the error is the function where I emit the signal.

    It seems to me that one of the "this"s is wrong.

    1 Reply Last reply
    0
    • raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      does your class B miss the Q_OBJECT macro?

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      0
      • D Offline
        D Offline
        dolevo
        wrote on last edited by
        #3

        [quote author="raven-worx" date="1375443731"]does your class B miss the Q_OBJECT macro?[/quote]

        Oh man. Yes it does. I have just added and recompiled.
        Now I get
        @
        error: C2385: ambiguous access of 'connect'
        could be the 'connect' in base 'QObject'
        or could be the 'connect' in base 'QObject'

        error: C3861: 'connect': identifier not found
        @

        1 Reply Last reply
        0
        • raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by
          #4

          let the highest class derive from QObject and rerun qmake.

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          0
          • D Offline
            D Offline
            dolevo
            wrote on last edited by
            #5

            Aha, I had to delete Q_OBJECT and related inherit from class A and it compiles now.

            1 Reply Last reply
            0
            • D Offline
              D Offline
              dolevo
              wrote on last edited by
              #6

              It runs ok but doesn't trigger my slot. I am debugging it now and I see that signal is being emitted. But no slot is called.

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

                Of course not: if you want slots, you need Q_OBJECT.

                Re-insert Q_OBJECT, re-run qmake (as you were told to do by raven-worx), and re-compile.

                1 Reply Last reply
                0
                • D Offline
                  D Offline
                  dolevo
                  wrote on last edited by
                  #8

                  [quote author="Andre" date="1375445688"]Of course not: if you want slots, you need Q_OBJECT.

                  Re-insert Q_OBJECT, re-run qmake (as you were told to do by raven-worx), and re-compile.[/quote]

                  Yes but then I get the error that I mentioned in
                  "Link to the post...":http://qt-project.org/forums/viewthread/30800/#136159

                  1 Reply Last reply
                  0
                  • D Offline
                    D Offline
                    dbzhang800
                    wrote on last edited by
                    #9

                    Hi, you should connect SIGNAL to SLOT, but you are connecting a SLOT to SIGNAL now.

                    1 Reply Last reply
                    0
                    • D Offline
                      D Offline
                      dolevo
                      wrote on last edited by
                      #10

                      [quote author="1+1=2" date="1375446251"]Hi, you should connect SIGNAL to SLOT, but you are connecting a SLOT to SIGNAL now.[/quote]

                      I have tried that as well. Now in my actual code it is running like:

                      @
                      connect(this,SIGNAL(sgSetTheThreshold(float)), this, SLOT(SetTheThreshold(float)));
                      @

                      I think it should work in both ways.

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

                        [quote author="dolevo" date="1375446069"]
                        [quote author="Andre" date="1375445688"]Of course not: if you want slots, you need Q_OBJECT.

                        Re-insert Q_OBJECT, re-run qmake (as you were told to do by raven-worx), and re-compile.[/quote]

                        Yes but then I get the error that I mentioned in
                        "Link to the post...":http://qt-project.org/forums/viewthread/30800/#136159[/quote]
                        Ah... your problem is that you are using multiple inheritance of QObject. You have the dreaded diamond-shaped inheritance diagram. Don't do that. It is not supported.

                        [quote author="dolevo" date="1375446385"]
                        [quote author="1+1=2" date="1375446251"]Hi, you should connect SIGNAL to SLOT, but you are connecting a SLOT to SIGNAL now.[/quote]

                        I have tried that as well. Now in my actual code it is running like:

                        @
                        connect(this,SIGNAL(sgSetTheThreshold(float)), this, SLOT(SetTheThreshold(float)));
                        @

                        I think it should work in both ways.[/quote]

                        No, it will not work both ways. You can connect a signal to a slot, or to
                        another signal, or with Qt 5 even to a normal member function or a functor or a lambda function. But you cannot connect a slot to anything.

                        1 Reply Last reply
                        0
                        • D Offline
                          D Offline
                          dolevo
                          wrote on last edited by
                          #12

                          I didn't get what I should not do?

                          What I want to do is to connect a signal in the sub class in a slot in the super class. Is this not supported?

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

                            No. The error you give us, suggests to me that you are using multiple inheritance, and that class B is already descendant from QObject. Your class currently inherits from QObject and B. If B also directly or indirectly inherits QObject, you have the diamond-shaped multiple inheritance, and QObject will not function properly if you do that. In your case, the solution is simple: only inherit from B.

                            1 Reply Last reply
                            0
                            • raven-worxR Offline
                              raven-worxR Offline
                              raven-worx
                              Moderators
                              wrote on last edited by
                              #14

                              [quote author="raven-worx" date="1375444124"]let the highest class derive from QObject and rerun qmake.[/quote]
                              i think i was unclear with that post... thats what i meant with "highest" class. (hierarchical highest class, the most basic class, and only this one) .. sry

                              --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                              If you have a question please use the forum so others can benefit from the solution in the future

                              1 Reply Last reply
                              0
                              • D Offline
                                D Offline
                                dbzhang800
                                wrote on last edited by
                                #15

                                [quote author="dolevo" date="1375446799"]I didn't get what I should not do?
                                [/quote]
                                As Andre said, you should not use multiple inheritance of QObject.

                                The Qt documentation says that:

                                If you are using multiple inheritance, moc assumes that the first inherited class is a subclass of QObject. Also, be sure that only the first inherited class is a QObject.

                                @
                                // correct
                                class SomeClass : public QObject, public OtherClass
                                {
                                ...
                                };
                                @

                                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