gpt4 book ai didi

c++ - 在 QT5 中将信号连接到 c++11 lambda

转载 作者:搜寻专家 更新时间:2023-10-31 02:11:28 24 4
gpt4 key购买 nike

我尝试将 QDoubleSpinBox 信号连接到 c++11 lamda:

QObject::connect(sbox_roughness, &QDoubleSpinBox::valueChanged,
[=]() { std::cout << "value changed" << std::endl; });

这给了我:

error: no matching function for call to ‘Game::connect(QDoubleSpinBox*&, <unresolved overloaded function type>, Game::initGui()::<lambda()>)’
[=]() { std::cout << "value changed" << std::endl; });
note: no known conversion for argument 2 from ‘<unresolved overloaded function type>’ to ‘const char*’

我试过了 this :

QObject::connect(sbox_roughness, static_cast<void (QDoubleSpinBox::*)(int)>(
&QDoubleSpinBox::valueChanged),
[=]() { std::cout << "value changed" << std::endl; });

给我:

error: no matches converting function ‘valueChanged’ to type ‘void (class QDoubleSpinBox::*)(int)’
&QDoubleSpinBox::valueChanged),

我在这里错过了什么?

最佳答案

QDoubleSpinBox spinbox;

QObject::connect(&spinbox, &QDoubleSpinBox::valueChanged,
[]() { qDebug() << "value changed"; });

这会失败,因为 QDoubleSpinBox 有两个 valueChanged 信号,您无法在此处决定要连接到哪一个。

QObject::connect(&spinbox, static_cast<void (QDoubleSpinBox::*)(int)>(
&QDoubleSpinBox::valueChanged),
[]() { qDebug() << "value changed"; });

这失败了,因为 QDoubleSpinBox 没有 valueChanged(int) 信号。

这会起作用:

QObject::connect(&spinbox, static_cast<void (QDoubleSpinBox::*)(double)>(
&QDoubleSpinBox::valueChanged),
[]() { qDebug() << "value changed"; });

QObject::connect(&spinbox, static_cast<void (QDoubleSpinBox::*)(const QString &)>(
&QDoubleSpinBox::valueChanged),
[]() { qDebug() << "value changed"; });

关于c++ - 在 QT5 中将信号连接到 c++11 lambda,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43515852/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com