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. Help rolling own lambda connect for Qt 4.8

Help rolling own lambda connect for Qt 4.8

Scheduled Pinned Locked Moved Solved General and Desktop
23 Posts 6 Posters 7.9k Views 2 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.
  • KroMignonK KroMignon

    @SPlatten Your welcome, I'm curious if this has been useful to you.

    SPlattenS Offline
    SPlattenS Offline
    SPlatten
    wrote on last edited by
    #5

    @KroMignon Trying it now...will let you know when I've tested it.

    Kind Regards,
    Sy

    1 Reply Last reply
    0
    • KroMignonK KroMignon

      @SPlatten Your welcome, I'm curious if this has been useful to you.

      SPlattenS Offline
      SPlattenS Offline
      SPlatten
      wrote on last edited by
      #6

      @KroMignon , it could the very old tools that this company is using:

      gcc (Gentoo 4.6.3 p1.13,pie-0.5.2) and Qt 4.8.4

      When I try to compile including the <functional> header which is present I get:

      ../clsLambda.h:29:5: error: 'function' in namespace 'std' does not name a type
      ../clsLambda.h:32:28: error: expected ')' before '<' token
      ../clsLambda.h: In member function 'void clsLambda::slot()':
      ../clsLamdba.h:37:24: error: 'mpFN' was not declared in this scope
      make: *** [moc_clsLambda.o] Error 1
      12:27:31 The process "/usr/bin/make" exited with code 2
      

      The header:

      #include <functional>
      #include <QObject>
      
      class clsLambda : public QObject {
          Q_OBJECT
      
      private:
          std::function<void()>mpFN;                                    //This is line 29
      
      public:
          clsLambda(std::function<void()> pFN, QObject* pParent = 0)    //This is line 32
                  : mpFN(pFN) {
          }
      public slots:
          void slot() { if ( mpFN ) mpFN(); }                           //This is line 37
      };
      

      Kind Regards,
      Sy

      SPlattenS KroMignonK 2 Replies Last reply
      0
      • SPlattenS SPlatten

        @KroMignon , it could the very old tools that this company is using:

        gcc (Gentoo 4.6.3 p1.13,pie-0.5.2) and Qt 4.8.4

        When I try to compile including the <functional> header which is present I get:

        ../clsLambda.h:29:5: error: 'function' in namespace 'std' does not name a type
        ../clsLambda.h:32:28: error: expected ')' before '<' token
        ../clsLambda.h: In member function 'void clsLambda::slot()':
        ../clsLamdba.h:37:24: error: 'mpFN' was not declared in this scope
        make: *** [moc_clsLambda.o] Error 1
        12:27:31 The process "/usr/bin/make" exited with code 2
        

        The header:

        #include <functional>
        #include <QObject>
        
        class clsLambda : public QObject {
            Q_OBJECT
        
        private:
            std::function<void()>mpFN;                                    //This is line 29
        
        public:
            clsLambda(std::function<void()> pFN, QObject* pParent = 0)    //This is line 32
                    : mpFN(pFN) {
            }
        public slots:
            void slot() { if ( mpFN ) mpFN(); }                           //This is line 37
        };
        
        SPlattenS Offline
        SPlattenS Offline
        SPlatten
        wrote on last edited by
        #7

        I manged to remove the compiler warnings and compile by changing the class to:

        #include <QObject>
        
        class clsLambda : public QObject {
            Q_OBJECT
        
        private:
            void(*mpFN)();
        
        public:
            clsLambda(void(*pFN)()) : mpFN(pFN) {
            }
        public slots:
            void slot() { if ( mpFN ) mpFN(); }
        };
        

        Kind Regards,
        Sy

        1 Reply Last reply
        0
        • SPlattenS SPlatten

          @KroMignon , it could the very old tools that this company is using:

          gcc (Gentoo 4.6.3 p1.13,pie-0.5.2) and Qt 4.8.4

          When I try to compile including the <functional> header which is present I get:

          ../clsLambda.h:29:5: error: 'function' in namespace 'std' does not name a type
          ../clsLambda.h:32:28: error: expected ')' before '<' token
          ../clsLambda.h: In member function 'void clsLambda::slot()':
          ../clsLamdba.h:37:24: error: 'mpFN' was not declared in this scope
          make: *** [moc_clsLambda.o] Error 1
          12:27:31 The process "/usr/bin/make" exited with code 2
          

          The header:

          #include <functional>
          #include <QObject>
          
          class clsLambda : public QObject {
              Q_OBJECT
          
          private:
              std::function<void()>mpFN;                                    //This is line 29
          
          public:
              clsLambda(std::function<void()> pFN, QObject* pParent = 0)    //This is line 32
                      : mpFN(pFN) {
              }
          public slots:
              void slot() { if ( mpFN ) mpFN(); }                           //This is line 37
          };
          
          KroMignonK Offline
          KroMignonK Offline
          KroMignon
          wrote on last edited by
          #8

          @SPlatten said in Help rolling own lambda connect for Qt 4.8:

          When I try to compile including the <functional> header which is present I get:

          Did you enable C++11 support?
          I guess you have to add -std=c++11 in CXXFLAGS.
          With qmake, you can add in pro file QMAKE_CXXFLAGS += -std=c++11
          Or it may also be -std=c++0x, depending on GCC version.

          It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

          SPlattenS 1 Reply Last reply
          0
          • SPlattenS SPlatten

            I want to create a lambda connect for Qt 4.8, using a later version of Qt just isn't an option at the moment, so I would like to add my own.

            Is this something that is possible to do with Qt 4.8 and the toolset it has available?

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

            @SPlatten you could use verdigris
            https://github.com/woboq/verdigris

            that should work, but no guarantee :D


            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.

            SPlattenS 1 Reply Last reply
            1
            • KroMignonK KroMignon

              @SPlatten said in Help rolling own lambda connect for Qt 4.8:

              When I try to compile including the <functional> header which is present I get:

              Did you enable C++11 support?
              I guess you have to add -std=c++11 in CXXFLAGS.
              With qmake, you can add in pro file QMAKE_CXXFLAGS += -std=c++11
              Or it may also be -std=c++0x, depending on GCC version.

              SPlattenS Offline
              SPlattenS Offline
              SPlatten
              wrote on last edited by
              #10

              @KroMignon , I've modified the pro file:

              QT += core gui
              CONFIG += c++11
              CONFIG += sdk_no_version_check
              QMAKE_CXXFLAGS += -std=c++11
              ...
              

              When I try to build I get:

              ccplus: error: unrecognized command line option '-std=c++11'
              

              Kind Regards,
              Sy

              KroMignonK 1 Reply Last reply
              0
              • J.HilkJ J.Hilk

                @SPlatten you could use verdigris
                https://github.com/woboq/verdigris

                that should work, but no guarantee :D

                SPlattenS Offline
                SPlattenS Offline
                SPlatten
                wrote on last edited by
                #11

                @J-Hilk , I cannot access that site as its blocked by the corporate firewall.

                Kind Regards,
                Sy

                J.HilkJ 1 Reply Last reply
                0
                • SPlattenS SPlatten

                  @J-Hilk , I cannot access that site as its blocked by the corporate firewall.

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

                  @SPlatten GitHub ?


                  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.

                  SPlattenS 1 Reply Last reply
                  0
                  • J.HilkJ J.Hilk

                    @SPlatten GitHub ?

                    SPlattenS Offline
                    SPlattenS Offline
                    SPlatten
                    wrote on last edited by
                    #13

                    @J-Hilk I clicked on link and was met with:

                    Error Message: Access to the requested URL has been denied (blocked-category-message)
                    

                    Kind Regards,
                    Sy

                    J.HilkJ 1 Reply Last reply
                    0
                    • SPlattenS SPlatten

                      @KroMignon , I've modified the pro file:

                      QT += core gui
                      CONFIG += c++11
                      CONFIG += sdk_no_version_check
                      QMAKE_CXXFLAGS += -std=c++11
                      ...
                      

                      When I try to build I get:

                      ccplus: error: unrecognized command line option '-std=c++11'
                      
                      KroMignonK Offline
                      KroMignonK Offline
                      KroMignon
                      wrote on last edited by
                      #14

                      @SPlatten said in Help rolling own lambda connect for Qt 4.8:

                      When I try to build I get:
                      ccplus: error: unrecognized command line option '-std=c++11'

                      You are using GCC 4.6 (which is very old!), so you should use -std=c++0x to enabled C++11 support (cf. https://gcc.gnu.org/gcc-4.6/cxx0x_status.html)

                      It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                      SPlattenS 1 Reply Last reply
                      2
                      • SPlattenS SPlatten

                        @J-Hilk I clicked on link and was met with:

                        Error Message: Access to the requested URL has been denied (blocked-category-message)
                        
                        J.HilkJ Offline
                        J.HilkJ Offline
                        J.Hilk
                        Moderators
                        wrote on last edited by
                        #15

                        @SPlatten well, here's the general website:
                        https://woboq.com/blog/verdigris-qt-without-moc.html


                        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
                        0
                        • KroMignonK KroMignon

                          @SPlatten said in Help rolling own lambda connect for Qt 4.8:

                          When I try to build I get:
                          ccplus: error: unrecognized command line option '-std=c++11'

                          You are using GCC 4.6 (which is very old!), so you should use -std=c++0x to enabled C++11 support (cf. https://gcc.gnu.org/gcc-4.6/cxx0x_status.html)

                          SPlattenS Offline
                          SPlattenS Offline
                          SPlatten
                          wrote on last edited by
                          #16

                          @KroMignon , its not working, with the changes to the pro file so builds without problems and the clsLambda now like:

                          class clsLambda : public QObject {
                              Q_OBJECT
                          
                          private:
                              std::function<void()>mpFN; 
                          
                          public:
                              clsLambda(std::function<void()> pFN) : mpFN(pFN) {
                              }
                          public slots:
                              void lambdaSlot() { if ( mpFN ) mpFN(); }
                          };
                          

                          The test code:

                          clsLambda objLambda([&]() {
                              qDebug() << "HERE";
                          });
                          bool blnRC(QObject::connect(pobjAct, SIGNAL(triggered()), &objLambda, SLOT(lambdaSlot())));
                          qDebug() < blnRC;
                          

                          blnRC is always true, but when a menu is selected there is no sign of HERE in the Application Output.

                          Kind Regards,
                          Sy

                          jsulmJ 1 Reply Last reply
                          0
                          • Christian EhrlicherC Offline
                            Christian EhrlicherC Offline
                            Christian Ehrlicher
                            Lifetime Qt Champion
                            wrote on last edited by
                            #17

                            @SPlatten said in Help rolling own lambda connect for Qt 4.8:

                            clsLambda objLambda(& {
                            qDebug() << "HERE";
                            });

                            C++ basics - how long does this object live?

                            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                            Visit the Qt Academy at https://academy.qt.io/catalog

                            SPlattenS 3 Replies Last reply
                            1
                            • Christian EhrlicherC Christian Ehrlicher

                              @SPlatten said in Help rolling own lambda connect for Qt 4.8:

                              clsLambda objLambda(& {
                              qDebug() << "HERE";
                              });

                              C++ basics - how long does this object live?

                              SPlattenS Offline
                              SPlattenS Offline
                              SPlatten
                              wrote on last edited by
                              #18

                              @Christian-Ehrlicher , the lambda is an inline function, what do you mean?

                              Kind Regards,
                              Sy

                              KroMignonK 1 Reply Last reply
                              0
                              • Christian EhrlicherC Christian Ehrlicher

                                @SPlatten said in Help rolling own lambda connect for Qt 4.8:

                                clsLambda objLambda(& {
                                qDebug() << "HERE";
                                });

                                C++ basics - how long does this object live?

                                SPlattenS Offline
                                SPlattenS Offline
                                SPlatten
                                wrote on last edited by
                                #19

                                @Christian-Ehrlicher , sorry, I know exactly what you meant and I will change the code to allow for a life of the object outside of the initial set-up, the example on the original link:
                                https://silmor.de/qtstuff.lambda.php

                                Is IMHO wrong and I agree with you.

                                Kind Regards,
                                Sy

                                1 Reply Last reply
                                0
                                • SPlattenS SPlatten

                                  @Christian-Ehrlicher , the lambda is an inline function, what do you mean?

                                  KroMignonK Offline
                                  KroMignonK Offline
                                  KroMignon
                                  wrote on last edited by
                                  #20

                                  @SPlatten said in Help rolling own lambda connect for Qt 4.8:

                                  the lambda is an inline function, what do you mean?

                                  with [&](){} you are capturing any current variable by reference... it'is not the best/cleanest way to do ;)

                                  It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                                  1 Reply Last reply
                                  0
                                  • SPlattenS SPlatten

                                    @KroMignon , its not working, with the changes to the pro file so builds without problems and the clsLambda now like:

                                    class clsLambda : public QObject {
                                        Q_OBJECT
                                    
                                    private:
                                        std::function<void()>mpFN; 
                                    
                                    public:
                                        clsLambda(std::function<void()> pFN) : mpFN(pFN) {
                                        }
                                    public slots:
                                        void lambdaSlot() { if ( mpFN ) mpFN(); }
                                    };
                                    

                                    The test code:

                                    clsLambda objLambda([&]() {
                                        qDebug() << "HERE";
                                    });
                                    bool blnRC(QObject::connect(pobjAct, SIGNAL(triggered()), &objLambda, SLOT(lambdaSlot())));
                                    qDebug() < blnRC;
                                    

                                    blnRC is always true, but when a menu is selected there is no sign of HERE in the Application Output.

                                    jsulmJ Offline
                                    jsulmJ Offline
                                    jsulm
                                    Lifetime Qt Champion
                                    wrote on last edited by
                                    #21
                                    This post is deleted!
                                    1 Reply Last reply
                                    0
                                    • Christian EhrlicherC Christian Ehrlicher

                                      @SPlatten said in Help rolling own lambda connect for Qt 4.8:

                                      clsLambda objLambda(& {
                                      qDebug() << "HERE";
                                      });

                                      C++ basics - how long does this object live?

                                      SPlattenS Offline
                                      SPlattenS Offline
                                      SPlatten
                                      wrote on last edited by
                                      #22

                                      @Christian-Ehrlicher , thank you for the nudge in the right direction:

                                      Added to the class:

                                      QList<clsLambda*> mlstLambdas;
                                      

                                      In the code:

                                      clsLambda* pobjLambda(new clsLambda([pobjAct]() {
                                          const QString cstrMID(pobjAct->property(clsMainWnd::mscszPropertyMenuID));
                                          qDebug() << cstrMID;
                                      }));
                                      mlstLambdas.append(pobjLambda);
                                      QObject::connect(pobjAck, SIGNAL(triggered()), pobjLambda, SLOT(lambdaSlot())));
                                      

                                      It works really well.

                                      Kind Regards,
                                      Sy

                                      S 1 Reply Last reply
                                      0
                                      • SPlattenS SPlatten

                                        @Christian-Ehrlicher , thank you for the nudge in the right direction:

                                        Added to the class:

                                        QList<clsLambda*> mlstLambdas;
                                        

                                        In the code:

                                        clsLambda* pobjLambda(new clsLambda([pobjAct]() {
                                            const QString cstrMID(pobjAct->property(clsMainWnd::mscszPropertyMenuID));
                                            qDebug() << cstrMID;
                                        }));
                                        mlstLambdas.append(pobjLambda);
                                        QObject::connect(pobjAck, SIGNAL(triggered()), pobjLambda, SLOT(lambdaSlot())));
                                        

                                        It works really well.

                                        S Offline
                                        S Offline
                                        SimonSchroeder
                                        wrote on last edited by
                                        #23

                                        @SPlatten Since clsLambda already inherits from QObject I would use this as parent during creation. Then you don't have to keep a list and think about when to delete and remove lambdas from the list.

                                        Also, you have so much code to write just to use a lambda. This would push me to write the slots in the original way. Maybe you want to implement a macros that will do all the extra work (I'm not entirely sure if you could easily pass a lambda as argument to the macro). I would prefer to write this:

                                        CONNECT_LAMBDA(pobjAck, SIGNAL(triggered()), [pobjAct]() {...});
                                        

                                        In order to pick a unique name for pobjLambda you can use __LINE__ appended to your name which makes the name of the lambda object unique within each file (which is sufficient for a variable with local scope).

                                        From the top of my head (untested):

                                        #define CONNECT_LAMBDA(OBJ, SIG, LAMBDA) \
                                        clsLambda* pobjLambda##__LINE__ (new clsLambda(LAMBDA)); \
                                        mlstLambdas.append(pobjLambda##__LINE__) \
                                        QObject::connect(OBJ, SIG, pobjLambda##__LINE__, SLOT(lambdaSlot()));
                                        

                                        I believe that __LINE__ is the line the macro is used in, such that even the macro is multiple lines it always evaluates to the same line.

                                        1 Reply Last reply
                                        1

                                        • Login

                                        • Login or register to search.
                                        • First post
                                          Last post
                                        0
                                        • Categories
                                        • Recent
                                        • Tags
                                        • Popular
                                        • Users
                                        • Groups
                                        • Search
                                        • Get Qt Extensions
                                        • Unsolved