Please nominate your Qt Champions for 2021!

New signal slot syntax attempt

  • Hi! I'm trying to implement new signal slot syntax. Documentation is [here].(
    My aim is to connect signal from object to simple function. I get error:

    main.cpp:14: error: no matching function for call to 'QObject::connect(SigGen&, void (SigGen::*)(), void (&)())'
       QObject::connect(siggen, &SigGen::sgAction, handler);

    My code:

    #include <QCoreApplication>
    #include <iostream>
    #include <QObject>
    #include "siggen.h"
    void handler(){
      std::cout << "got signal" << std::endl;
    int main(int argc, char *argv[])
      SigGen siggen;
      QObject::connect(siggen, &SigGen::sgAction, handler);
      QCoreApplication a(argc, argv);
      std::cout << "main prog start" << std::endl;
      return a.exec();


    #ifndef SIGGEN_H
    #define SIGGEN_H
    #include <QObject>
    class SigGen : public QObject
      explicit SigGen(QObject *parent = 0);
      void action(void);
      void sgAction(void);
    public slots:
    #endif // SIGGEN_H


    #include "siggen.h"
    SigGen::SigGen(QObject *parent) : QObject(parent)
    void SigGen::action()
      emit sgAction();

  • Moderators



    connect(sender, &Sender::valueChanged, someFunction);

    sender needs to be a pointer to your object.
    So in your case try one of the following changes:

     SigGen *siggen = new SigGen(this);
    // or
    QObject::connect(&siggen, &SigGen::sgAction, handler);

  • @J.Hilk Thanks!

  • Lifetime Qt Champion


    Some additions to @J-Hilk,

    1. always create your QCore/Gui/Application as the first thing you do in your application. It setups Qt's internal so it will work properly.
    2. In your main function, if you allocated siggen on the heap, then delete it before returning the result of app.exec() for proper cleanup. Otherwise pass it your QCoreApplication object as parent.

Log in to reply