New signal slot syntax attempt



  • Hi! I'm trying to implement new signal slot syntax. Documentation is [here].(http://wiki.qt.io/New_Signal_Slot_Syntax)
    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:
    main.cpp:

    #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);
    
      siggen.action();
    
    
      QCoreApplication a(argc, argv);
      std::cout << "main prog start" << std::endl;
    
      return a.exec();
    }
    

    siggen.h:

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

    siggen.cpp:

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


  • @fatinbrain

    hi,

    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

    Hi,

    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
 

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