Qt c ++ connect signal with non-void signature to lambda

I want to connect a signal with a non-void signature to a lambda function. My code looks like this

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 doesn't change this->height()

. What am I wrong? How to write a lambda so that it is qreal

from valueChanged

? I've read the related documentation but haven't found any useful examples.

**** **** EDIT

It actually works, I used the QTimeLine settings incorrectly. And yes, I don't need to capture a

. I am trying to animate a native insertRow()

QTableWidget method . I also made a lambda to change the row height of the table, not the containing widget. For reference, here's a 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();

      

Anyway, thanks for the quick answers.

+3


source to share


1 answer


It should just work. Here is a complete SSCCE that demonstrates that it works. Check what you do differently in the principles.

main.cpp

#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();
}

      

main.pro



TEMPLATE = app
TARGET = main
QT = core
CONFIG += c++11
SOURCES += main.cpp

      

Build and run

qmake && make && ./main

      

+1


source







All Articles