【问题标题】:QT, can't bind QProcess::finished(int,QProcess::ExitStatus) signal to lambdaQT,无法将 QProcess::finished(int,QProcess::ExitStatus) 信号绑定到 lambda
【发布时间】:2019-02-19 08:14:15
【问题描述】:

我使用的是 c++11 和 QT 5.12。
我正在尝试将 QProcess::finished(int,QProcess::ExitCode) 信号连接到 lambda,但使用代码

QProcess PlayerProcess;
connect(PlayerProcess, &QProcess::finished,
[=](int exitCode, QProcess::ExitStatus exitStatus)
{
 //  Function body
}

编译器说

../Qt/5.12.1/gcc_64/include/QtCore/qobject.h:300:13: note:   no known conversion for argument 1 from ‘QProcess’ to ‘const Object* {aka const QProcess*}’
../Qt/5.12.1/gcc_64/include/QtCore/qobject.h:308:13: note: candidate: template<class Func1, class Func2> static typename std::enable_if<(QtPrivate::FunctionPointer<Func2>::ArgumentCount == -1), QMetaObject::Connection>::type QObject::connect(const typename QtPrivate::FunctionPointer<Func>::Object*, Func1, const QObject*, Func2, Qt::ConnectionType)
             connect(const typename QtPrivate::FunctionPointer<Func1>::Object *sender, Func1 signal, const QObject *context, Func2 slot,
             ^~~~~~~
../Qt/5.12.1/gcc_64/include/QtCore/qobject.h:308:13: note:   template argument deduction/substitution failed:
../Launcher/mainwindow.cpp:184:9: note:   candidate expects 5 arguments, 3 provided
         );

谷歌搜索了一下,我能找到的唯一相关问题是 MainWindow 类不是从 QObject 派生的(但我的 MainWindow 是从派生自 QWidget 的 QMainWindow 派生的),或者编译器无法解决重载的 QProcess ::finished 信号(可以是 (int) 或 (int,QProcess::ExitCode),但为此我尝试了我能找到的两个快速修复:

void  (QProcess::* mySignal)(int, QProcess::ExitStatus) = &QProcess::finished;
auto mySignal2 = QOverload<int,QProcess::ExitStatus>::of(&QProcess::finished);

但同时使用编译器错误不会改变。

我错过了什么?

提前致谢。

【问题讨论】:

  • QObject::connect 函数在参数中接受指针,而不是值/引用。您需要在代码中传递&amp;PlayerProcess
  • 哦。解决了,谢谢。我不能说我觉得自己很愚蠢,因为我不知道它需要一个指针:P

标签: qt signals qprocess


【解决方案1】:

正如@ymoreau 所说,QObject::connect 函数需要参数作为指针,所以我用 &PlayerProcess 更改了连接的第一个参数。

然后,使用两个显式重载之一解决了重载 QProcess::finished 问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-02-06
    • 1970-01-01
    • 2021-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多