Help rolling own lambda connect for Qt 4.8
-
@KroMignon Trying it now...will let you know when I've tested it.
-
@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 2The 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 }; -
@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 2The 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 };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(); } }; -
@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 2The 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 };@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++11in CXXFLAGS.
With qmake, you can add in pro fileQMAKE_CXXFLAGS += -std=c++11
Or it may also be-std=c++0x, depending on GCC version. -
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?
@SPlatten you could use verdigris
https://github.com/woboq/verdigristhat should work, but no guarantee :D
-
@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++11in CXXFLAGS.
With qmake, you can add in pro fileQMAKE_CXXFLAGS += -std=c++11
Or it may also be-std=c++0x, depending on GCC version.@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' -
@SPlatten you could use verdigris
https://github.com/woboq/verdigristhat should work, but no guarantee :D
-
@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'@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++0xto enabled C++11 support (cf. https://gcc.gnu.org/gcc-4.6/cxx0x_status.html) -
@J-Hilk I clicked on link and was met with:
Error Message: Access to the requested URL has been denied (blocked-category-message)@SPlatten well, here's the general website:
https://woboq.com/blog/verdigris-qt-without-moc.html -
@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++0xto enabled C++11 support (cf. https://gcc.gnu.org/gcc-4.6/cxx0x_status.html)@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.
-
@SPlatten said in Help rolling own lambda connect for Qt 4.8:
clsLambda objLambda(& {
qDebug() << "HERE";
});C++ basics - how long does this object live?
-
@SPlatten said in Help rolling own lambda connect for Qt 4.8:
clsLambda objLambda(& {
qDebug() << "HERE";
});C++ basics - how long does this object live?
@Christian-Ehrlicher , the lambda is an inline function, what do you mean?
-
@SPlatten said in Help rolling own lambda connect for Qt 4.8:
clsLambda objLambda(& {
qDebug() << "HERE";
});C++ basics - how long does this object live?
@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.phpIs IMHO wrong and I agree with you.
-
@Christian-Ehrlicher , the lambda is an inline function, what do you mean?
-
@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.
This post is deleted! -
@SPlatten said in Help rolling own lambda connect for Qt 4.8:
clsLambda objLambda(& {
qDebug() << "HERE";
});C++ basics - how long does this object live?
@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.
-
@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.
@SPlatten Since
clsLambdaalready inherits fromQObjectI would usethisas 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
pobjLambdayou 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.