Why am I getting a 'not a signal declaration' error in this header file?



  • I'm reading about "Signals & Slots" from the docs, and I wanted to try a test class. In a "header.h", I made a subclass of QPushButton called PushButtonWIdget, and made one slot, one signal, and one function to call the signal. However, on building it, I get an error in my signalFunction definition saying "Not a signal declaration". Why did I get this error? I'm pretty sure I can define signal functions with qDebug for example since they're just like any other function.

    #ifndef HEADER_H
    #define HEADER_H
    
    #include <QPushButton>
    #include <QDebug> //edit: added this too, since << syntax returns type QDebug
    
    class PushButtonWidget: public QPushButton {
        Q_OBJECT
    public:
        void callSignal() {
            emit signalFunction();
        }
    public slots:
        void slotFunction() {
            qDebug() << "The slot slotFunction was called.";
        }
    signals:
        void signalFunction() {
            qDebug() << "The signal signalFunction was called";
        }
    };
    
    
    
    #endif // HEADER_H
    


  • signals are implemented by moc you can't provide a body for them

    signals:
        void signalFunction();
    

    P.S.
    You are currently breaking the parent child mechanism as well, you should add a constructor like this one:

    public:
    explicit PushButtonWidget(QWidget* parent = NULL)
    :QPushButton (parent)
    {
    // I also suppose you want to link the signal and slot together? maybe?
    connect(this,&PushButtonWidget::signalFunction,this,&PushButtonWidget::slotFunction);
    }
    

    @DragonautX said in Why am I getting a 'not a signal declaration' error in this header file?:

    I'm pretty sure I can define signal functions with qDebug

    You can declare but not define. The qDebug part I did not get



  • Oh, that's why. Thanks, also for the mechanism fix and the connect() tip. I was going to connect via making two PushButtonWidget instances and using QObject::connect() in my "main.cpp", but using connect in the constructor is a nice idea too.

    Sorry, I meant if I could add a definition for my signal function, I thought I could add lines like qDebug() in the function block like I can for any function definition. I thought including the line qDebug in the function block was causing the problem, but I see it was actually defining the signal itself that was the problem.


Log in to reply
 

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