问题描述
在下面的代码中,我想将lambda函数连接到QProcess ::错误信号:
In following code I want to connect lambda function to QProcess::error signal:
void Updater::start() {
QProcess process;
QObject::connect(&process, &QProcess::error, [=] (QProcess::ProcessError error) {
qWarning() << "error " << error;
});
process.start("MyProgram");
process.waitForFinished();
}
但我遇到奇怪的错误:
我在这里做错了什么?该代码执行从QObject派生的类的内部方法。该项目配置为可与c ++ 11一起使用。
What I do wrong here? The code executes inside method of class derived from QObject. The project configured to work with c++11.
我在Linux x32上使用Qt 5.3.1和gcc 4.9.2
I use Qt 5.3.1 on Linux x32 with gcc 4.9.2
推荐答案
问题是 QProcess
还有另一个方法,因此编译器只是不知道使用哪种方法。如果要处理重载方法,则应使用下一个:
Problem is that the QProcess
has another error()
method, so compiler just doesn't know which method use. If you want to deal with overloaded methods, you should use next:
QProcess process;
connect(&process, static_cast<void (QProcess::*)(QProcess::ProcessError)>
(&QProcess::error), [=](QProcess::ProcessError pError) {
qWarning() << "error " << pError;
});
process.start("MyProgram");
process.waitForFinished();
是的,看起来很丑,但是没有别的办法(只有旧语法?)。
Yes, it looks ugly, but there is no another way (only old syntax?).
此特殊行告诉编译器您要使用 void QProcess :: error(QProcess :: ProcessError error)
,所以现在没有任何歧义
This special line tells compiler that you want to use void QProcess::error(QProcess::ProcessError error)
, so now there is no any ambiguity
。
这篇关于将lambda函数连接到QProcess :: error时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!