Qt C++将具有非空签名的信号连接到lambda
Qt C++ connect signal with non-void signature to lambda
我想将具有非void签名的信号连接到lambda函数。我的代码看起来像下面的
QTimeLine *a = new QTimeLine(DURATION, this);
connect(a, &QTimeLine::valueChanged, [a,this](qreal r) mutable { this->setMaximumHeight(r);});
以类似于SIGNAL-SLOT方法的方式:
connect(a, SIGNAL(valueChanged(qreal),this,SLOT(doStuff(qreal)));
我对lambda的连接进行了编译,但它不会更改this->height()
。我做错了什么?我应该如何编写lambda,使其从valueChanged
获取qreal
?我阅读了相关文档,但找不到有用的示例。
****编辑***
事实上,它是有效的,我得到了错误的QTimeLine设置。是的,我不需要捕捉a
。我试图为QTableWidget的自定义insertRow()
方法设置动画。我还让lambda更改了表行的高度,而不是所包含的小部件的高度。作为参考,以下是工作片段:
QTimeLine *a = new QTimeLine(DURATION,this);
connect(a,&QTimeLine::valueChanged,[this](qreal r) mutable {
this->list->setRowHeight(0,r * ROW::HEIGHT);
});
a->start();
无论如何,非常感谢您的快速回复。
应该能正常工作。这是一个完整的SSCCE,证明它的工作。检查你所做的事情在原则上的不同。
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
构建并运行
qmake && make && ./main
相关文章:
- 如何使用新语法QT5将信号连接到信号?
- 使用std::bind将Qt信号连接到插槽
- 为什么信号连接会使套接字客户端对象失效?
- 如何将信号连接到类的所有实例的插槽
- Qt:将受保护的 QListWidget::itemChanged 信号连接到插槽
- QML组件与信号连接
- Qt:如何将不同类别的静态信号连接到插槽
- QT:将信号连接到具有移动语义的插槽
- 将一个信号连接到多个插槽 Qt
- Qt5:无法将信号连接到插槽
- 这个Qt DBus信号连接代码是否正确
- QtQuick2 无法将应用程序窗口关闭信号连接到方法(C++新手)
- 将返回对象的信号连接到没有参数的插槽
- 如何使用新的QObject::连接语法与函数指针将QSslSocket::错误信号连接到插槽
- Qt将信号连接到插槽
- Qt5 将自定义窗口关闭信号连接到其他窗口
- 将所有对象信号连接到单个插槽
- 如何使用QT5中的lambdas将具有int参数的信号连接到具有枚举参数with的插槽
- 可以将多个信号连接到单个插槽
- 是否可以在没有接收器实例的情况下将信号连接到静态插槽