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 8.5k 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.
  • 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