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. New signal slot syntax attempt

New signal slot syntax attempt

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 1.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.
  • F Offline
    F Offline
    fatinbrain
    wrote on last edited by
    #1

    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();
    }
    
    J.HilkJ 1 Reply Last reply
    0
    • F fatinbrain

      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();
      }
      
      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #2

      @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);
      

      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      F 1 Reply Last reply
      2
      • J.HilkJ J.Hilk

        @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);
        
        F Offline
        F Offline
        fatinbrain
        wrote on last edited by
        #3

        @J.Hilk Thanks!

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          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