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. Why am I getting a 'not a signal declaration' error in this header file?
Qt 6.11 is out! See what's new in the release blog

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

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 5.3k 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
    DragonautX
    wrote on last edited by DragonautX
    #1

    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
    
    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by VRonin
      #2

      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

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

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

        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.

        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