I want to connect a signal with non-void signature to a lambda function. My code looks like the following
QTimeLine *a = new QTimeLine(DURATION, this);
connect(a, &QTimeLine::valueChanged, [a,this](qreal r) mutable { this->setMaximumHeight(r);});
in a way similar to the SIGNAL-SLOT approach:
connect(a, SIGNAL(valueChanged(qreal),this,SLOT(doStuff(qreal)));
My connect-to-lambda compiles, but it won't change this->height()
.
What did I get wrong? How should I write the lambda so that it takes the qreal
from valueChanged
?
I read the related documentation, but I couldn't find useful examples.
****EDIT****
In fact it works, I was getting the QTimeLine settings wrong. And yes, I don't need to capture a
.
I was trying to animate a custom insertRow()
method of a QTableWidget.
I also made the lambda change the height of the table row instead of the contained widget's. For reference, here's the working snippet:
QTimeLine *a = new QTimeLine(DURATION,this);
connect(a,&QTimeLine::valueChanged,[this](qreal r) mutable {
this->list->setRowHeight(0,r * ROW::HEIGHT);
});
a->start();
Thanks a lot for the quick replies anyway.
Should just work. Here is a complete SSCCE that demonstrates it working. Check what you are doing different in principles.
#include <QTimeLine>
#include <QObject>
#include <QDebug>
#include <QCoreApplication>
class Foo
{
void setMaximumHeight(int h) {height = h; qDebug() << "Height:" << height;}
public:
void doStuff() { QObject::connect(&timeLine, &QTimeLine::valueChanged, [this](qreal r) mutable { setMaximumHeight(r);}); timeLine.start(); }
int maximumHeight() const { return height; }
int height{0};
int DURATION{100};
QTimeLine timeLine{DURATION};
};
int main(int argc, char **argv)
{
QCoreApplication application(argc, argv);
Foo foo;
foo.doStuff();
return application.exec();
}
TEMPLATE = app
TARGET = main
QT = core
CONFIG += c++11
SOURCES += main.cpp
qmake && make && ./main