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