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. How to use new signal/slot syntax in Qt5.6?
QtWS25 Last Chance

How to use new signal/slot syntax in Qt5.6?

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 6 Posters 3.9k Views
  • 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.
  • kahlenbergK Offline
    kahlenbergK Offline
    kahlenberg
    wrote on last edited by kahlenberg
    #1

    Hi,
    I have a console application, I want to connect a signal from an object to a function in main.cpp. According to new signal/slot syntax it is possible. There is no need to have an object in order to connect signals and slots. How can I do that?

    main.h

    ....
    void dumpReceivedData(int type, QByteArray *msg);
    

    main.cpp

    int main()
    {
    ...
    Server server;
    QObject::connect(&server, Server::msgOut, dumpReceivedData);
    ....
    }
    void dumpReceivedData(int type, QByteArray *msg)
    {
    qDebug() << msg->constData();
    }
    

    server.h

    ...
    signals:
    static void msgOut(int type, QByteArray *msg);
    

    server.cpp

    void foo()
    {
    ...
    QByteArray *msg = new QByteArray;
    msg->append(aTcpSocket->readAll());
    emit msgOut(1, msg);
    }
    

    I am getting compile error:

    no matching function for call to QObject::connect(Server *, void(&)(int, QByteArray*), void(&)(int, QByteArray*))
    

    Thanks.

    J.HilkJ 1 Reply Last reply
    0
    • kahlenbergK kahlenberg

      Hi,
      I have a console application, I want to connect a signal from an object to a function in main.cpp. According to new signal/slot syntax it is possible. There is no need to have an object in order to connect signals and slots. How can I do that?

      main.h

      ....
      void dumpReceivedData(int type, QByteArray *msg);
      

      main.cpp

      int main()
      {
      ...
      Server server;
      QObject::connect(&server, Server::msgOut, dumpReceivedData);
      ....
      }
      void dumpReceivedData(int type, QByteArray *msg)
      {
      qDebug() << msg->constData();
      }
      

      server.h

      ...
      signals:
      static void msgOut(int type, QByteArray *msg);
      

      server.cpp

      void foo()
      {
      ...
      QByteArray *msg = new QByteArray;
      msg->append(aTcpSocket->readAll());
      emit msgOut(1, msg);
      }
      

      I am getting compile error:

      no matching function for call to QObject::connect(Server *, void(&)(int, QByteArray*), void(&)(int, QByteArray*))
      

      Thanks.

      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #2

      @kahlenberg

      Taken from the docu

      QObject::connect(const QObject *sender, PointerToMemberFunction signal, const QObject *receiver, PointerToMemberFunction method, Qt::ConnectionType type = Qt::AutoConnection)
      

      in the case you describted:

      QObject::connect(&server, &Server::msgOut, this, &MyClass::dumpReceivedData);
      

      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.

      kahlenbergK 1 Reply Last reply
      0
      • hskoglundH Offline
        hskoglundH Offline
        hskoglund
        wrote on last edited by
        #3

        Hi, try moving the &, like this:

        ...
        Server server;
        QObject::connect(server, &Server::msgOut, dumpReceivedData);
        ....
        
        1 Reply Last reply
        0
        • J.HilkJ J.Hilk

          @kahlenberg

          Taken from the docu

          QObject::connect(const QObject *sender, PointerToMemberFunction signal, const QObject *receiver, PointerToMemberFunction method, Qt::ConnectionType type = Qt::AutoConnection)
          

          in the case you describted:

          QObject::connect(&server, &Server::msgOut, this, &MyClass::dumpReceivedData);
          
          kahlenbergK Offline
          kahlenbergK Offline
          kahlenberg
          wrote on last edited by
          #4

          @J.Hilk
          Thanks for reply but as I described above, I don't want to connect signals/slot between two objects, instead, I want to connect one object's signal to a function in main.cpp, where there is no class.

          J.HilkJ beeckscheB 2 Replies Last reply
          0
          • kahlenbergK kahlenberg

            @J.Hilk
            Thanks for reply but as I described above, I don't want to connect signals/slot between two objects, instead, I want to connect one object's signal to a function in main.cpp, where there is no class.

            J.HilkJ Offline
            J.HilkJ Offline
            J.Hilk
            Moderators
            wrote on last edited by
            #5

            @kahlenberg
            Ah, I see, you want a lambda connection to a signal, ok

            connect(&server, &Server::msgOut, [=]{ myFunction();});
            //if you want to pass variables too
            connect(&server, &Server::msgOut, [=](QVariant a){
                //do stuff with a or not
                myFunction(a);});
            

            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.

            1 Reply Last reply
            2
            • kahlenbergK kahlenberg

              @J.Hilk
              Thanks for reply but as I described above, I don't want to connect signals/slot between two objects, instead, I want to connect one object's signal to a function in main.cpp, where there is no class.

              beeckscheB Offline
              beeckscheB Offline
              beecksche
              wrote on last edited by beecksche
              #6

              @kahlenberg

              You can also connect your signal with a static function. It's nearly the same with the connection to a lamba function, like @J-Hilk said.

              static void staticSlot() 
              {
               ...
              }
              
              int main()
              {
               ...
               QObject::connect(&yourObject, &YourObject::yourSignal, staticSlot);
               ...
              }
              

              So in your case you just should add static to your void dumpReceivedData(int type, QByteArray *msg) function.

              jsulmJ kahlenbergK 2 Replies Last reply
              1
              • VRoninV Offline
                VRoninV Offline
                VRonin
                wrote on last edited by
                #7

                QObject::connect(&server, &Server::msgOut, dumpReceivedData);

                "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
                0
                • beeckscheB beecksche

                  @kahlenberg

                  You can also connect your signal with a static function. It's nearly the same with the connection to a lamba function, like @J-Hilk said.

                  static void staticSlot() 
                  {
                   ...
                  }
                  
                  int main()
                  {
                   ...
                   QObject::connect(&yourObject, &YourObject::yourSignal, staticSlot);
                   ...
                  }
                  

                  So in your case you just should add static to your void dumpReceivedData(int type, QByteArray *msg) function.

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @beecksche static is of no use in this case. See http://stackoverflow.com/questions/558122/what-is-a-static-function

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • beeckscheB beecksche

                    @kahlenberg

                    You can also connect your signal with a static function. It's nearly the same with the connection to a lamba function, like @J-Hilk said.

                    static void staticSlot() 
                    {
                     ...
                    }
                    
                    int main()
                    {
                     ...
                     QObject::connect(&yourObject, &YourObject::yourSignal, staticSlot);
                     ...
                    }
                    

                    So in your case you just should add static to your void dumpReceivedData(int type, QByteArray *msg) function.

                    kahlenbergK Offline
                    kahlenbergK Offline
                    kahlenberg
                    wrote on last edited by
                    #9

                    @beecksche, @VRonin
                    with or without static definition of the function dumpReceivedData
                    Compile error:

                    no matching function call to 'QObject::connect(Server *, void(*)(int, QByteArray*), void (&)(int, QByteArray*))'
                    

                    @J-Hilk

                    QObject::connect(&server, &Server::msgOut, [=](int type, QByteArray *msg)
                    {
                        dumpReceivedData(type, msg);
                    }
                    );
                    

                    compile error:

                    no matching function for call to QObject::connect(Server *, void(*)(int, QByteArray*), main(int, char**)::__lambda0)
                    
                    1 Reply Last reply
                    0
                    • VRoninV Offline
                      VRoninV Offline
                      VRonin
                      wrote on last edited by
                      #10

                      this works for me, could you test it?

                      #include <QApplication>
                      #include <QTimer>
                      #include <QDebug>
                      #include <QDateTime>
                      void printTime(){
                          qDebug() << QDateTime::currentDateTime();
                      }
                      
                      int main(int argc, char* argv[])
                      {
                          QApplication app(argc, argv);
                          QTimer t;
                          t.setInterval(1000);
                          QObject::connect(&t, &QTimer::timeout, printTime);
                          t.start();
                          return app.exec();
                      }
                      

                      "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
                      • kahlenbergK Offline
                        kahlenbergK Offline
                        kahlenberg
                        wrote on last edited by
                        #11

                        It worked. I declared the function dumpReceivedData as static mistakenly. I deleted static keyword and it worked.
                        I also tested lambda function. It also worked.
                        Thanks a lot.

                        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