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



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


  • Lifetime Qt Champion

    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



  • 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
    @


  • Moderators

    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.



  • 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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.