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 to Use Classes derived from QObject to send signals and have slots
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved General and Desktop
6 Posts 2 Posters 3.0k 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.
  • A Offline
    A Offline
    Aethalides
    wrote on 10 Feb 2013, 19:20 last edited by
    #1

    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
    0
    • Z Offline
      Z Offline
      Zlatomir
      wrote on 10 Feb 2013, 19:58 last edited by
      #2

      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
      0
      • A Offline
        A Offline
        Aethalides
        wrote on 10 Feb 2013, 20:34 last edited by
        #3

        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
        0
        • Z Offline
          Z Offline
          Zlatomir
          wrote on 10 Feb 2013, 20:44 last edited by
          #4

          -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
          0
          • A Offline
            A Offline
            Aethalides
            wrote on 10 Feb 2013, 21:44 last edited by
            #5

            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
            0
            • A Offline
              A Offline
              Aethalides
              wrote on 10 Feb 2013, 21:49 last edited by
              #6

              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
              0

              2/6

              10 Feb 2013, 19:58

              topic:navigator.unread, 4
              • Login

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