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. [SOLVED] Why can't i emit a signal from a method that's not declared in a .h file?
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] Why can't i emit a signal from a method that's not declared in a .h file?

Scheduled Pinned Locked Moved General and Desktop
5 Posts 4 Posters 2.2k 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
    DodgerThud
    wrote on last edited by
    #1

    Hi,
    I've got the following problem. I'm trying to emit a custom signal from a function in my .cpp file, basically it looks like this:

    something.h
    @
    class something : public QObject
    {
    Q_OBJECT

    signals:
    void someKindOfSignal();
    }
    @

    something.cpp
    @
    void someMethod()
    {
    emit someKindOfSignal()
    }

    something::something(QObject *parent):QObject(parent)
    {
    }
    @

    Now, why can't i emit a signal from the someMethod() function? From what i understand, i can emit a signal from any method that has been declared inside my .h file, everything else just gives my compiler errors (identifier not found), but why?
    I also can write 'emit someSignal()' from the constructor something::something(etc), but not in any method that has been declared outside of the .h file.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,
      someMethod() doesn't belong to something so it doesn't know anything about someKindOfSignal.

      The signals are tied to your class, so emitting them outside of it would be the same as trying to call any other class function

      Hope it helps

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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

        Hi,
        as you can see, someKindOfSignal() is just a member function of your class something.

        While emit is an empty macro, which does nothing.
        @
        #define emit
        @

        1 Reply Last reply
        0
        • sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by
          #4

          Usually when you need to emit a signal from outside the class it means that your design is flawed.

          Signals are something with which the class is informing the world outside that something has happened inside it. Other classes/ methods should not interfere here.

          (Z(:^

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

            i see, i guess i got confused then. i thought methods that are in the .cpp are also involved with the class in the .h

            I'll rework my code and see if it works then

            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