Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    How to Use Classes derived from QObject to send signals and have slots

    General and Desktop
    2
    6
    2809
    Loading More Posts
    • 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.
    • A
      Aethalides last edited by

      Hi

      I have been trying to have a class called Tool that derives from QObject and declares several slots.

      I my QMainWindow class (TheClass), I am trying to use that class.

      For instance TheClass has a private member:

      @private: Tool* TheTool;@

      Now when it comes to connect to signals from Tool, I do in the constructor of TheClass:

      @connect(TheTool,SIGNAL(theToolSignal),this,SLOT(myToolSlot()));@

      Which compiles fine, but when I run it, I get the message

      bq. QObject::connect: Cannot connect (nulll)::theToolSignal to TheClass::myToolSlot()

      Which seems to indicate that TheTool is not instantiated.

      Yet, when I make a call to a member function

      @TheTool->member()@

      I see from its qDebug() output that it is being called

      I have read the notes on the QObject class and that it has a Q_DISABLE_COPY macro which means that implied or explicit assignments won't work and that one cannot instantiate the QObject?

      My question for you is then:

      How can I modify my code so that I can have a class derived from Q_Object that uses Signals and Slots, and then use that class from another class?

      1 Reply Last reply Reply Quote 0
      • Zlatomir
        Zlatomir last edited by

        First make sure that the connect statement is after the line: TheTool = new Tool(); and also at the connect you are missing a pair of parenthesis: @SIGNAL(theToolSignal () )@

        https://forum.qt.io/category/41/romanian

        1 Reply Last reply Reply Quote 0
        • A
          Aethalides last edited by

          Hi

          Yes, I transcribed the signal wrong in this question.

          However, the problem I have is that when I do use new, the linker starts whinging at me:

          bq. in function Tool::Tool()': Tool.h:77: undefined reference to vtable for Tool::Tool'

          1 Reply Last reply Reply Quote 0
          • Zlatomir
            Zlatomir last edited by

            -Did you include the header file where the Tool class is declared?-

            LE: Make sure that Tool class has Q_OBJECT macro, if it doesn't add it and than run qmake and than build your project.

            https://forum.qt.io/category/41/romanian

            1 Reply Last reply Reply Quote 0
            • A
              Aethalides last edited by

              Hi

              I finally figured it out, your mentioning the Q_OBJECT macro again made me check and lo, I did not include the "tool.moc" file.

              Interestingly enough, it is not created by my cmake either even though qt4_automoc is fed both the tool as well as the MyClass

              I did a moc ../Tool.h > tool.moc in my build directory after appending the include "tool.moc" line in my tool.cpp file, then make and voila! it is compiling!

              Thank you, you're a star :)

              1 Reply Last reply Reply Quote 0
              • A
                Aethalides last edited by

                FYI; getting the cmake to build the moc for the tool as wel was as simple as adding a line

                @set(myCoolProject_H tool.h myclass.h)@

                In addition to having these lines:

                @QT4_WRAP_UI(myCoolProject_H ${myCoolProject_GUI} )

                qt4_automoc(${myCoolProject_SRCS})@

                1 Reply Last reply Reply Quote 0
                • First post
                  Last post