我在主
中有以下代码 QProcess process;
QObject::connect(&process, &QProcess::error, [](QProcess::ProcessError error)
{
qDebug() << error;
}, Qt::QueuedConnection);
bool launched = process.startDetached("D:\temp.exe");
并在编译时生成此错误
D:\main.cpp:5: error: C2665: 'QObject::connect' : none of the 3 overloads could convert all the argument types c:\qt\5.3\msvc2013_64\include\qtcore\qobject.h(205): could be
'QMetaObject::Connection QObject::connect(const QObject *,const char
*,const char *,Qt::ConnectionType) const' c:\qt\5.3\msvc2013_64\include\qtcore\qobject.h(201): or
'QMetaObject::Connection QObject::connect(const QObject *,const QMetaMethod &,const QObject *,const QMetaMethod &,Qt::ConnectionType)' c:\qt\5.3\msvc2013_64\include\qtcore\qobject.h(198): or
'QMetaObject::Connection QObject::connect(const QObject *,const char
*,const QObject *,const char *,Qt::ConnectionType)' while trying to match the argument list '(QProcess *, overloaded-function, RunGUIMode::<lambda_5d6e7ee926a623cea2a0e4469253d55f>, Qt::ConnectionType)'
有人可以帮帮我,告诉我我做错了什么。
我想将QProcess
类的信号连接到我的lambda
答案 0 :(得分:4)
我不应该发布这个答案,但说实话它不是same question,它更复杂。
首先,为什么第一个版本不起作用。因为您无法提供receiver
而无法使用其他参数(连接类型)。这意味着接下来是错误的。
connect(&process, static_cast<void (QProcess::*)(QProcess::ProcessError)>
(&QProcess::error),[=](QProcess::ProcessError pError) {
qWarning() << "error " << pError;
},Qt::QueuedConnection);
但接下来是正确的:
connect(&process, static_cast<void (QProcess::*)(QProcess::ProcessError)>
(&QProcess::error), this , [=](QProcess::ProcessError pError) {
qWarning() << "error " << pError;
},Qt::QueuedConnection);
如果您想知道原因,请查看qobject.h
。我在此文件中进行了一些更改,只是为了更准确(不要更改此文件!)。
//first
//connect to a functor
template <typename Func1, typename Func2>
static inline typename QtPrivate::QEnableIf<QtPrivate::FunctionPointer<Func2>::ArgumentCount == -1, QMetaObject::Connection>::Type
connect(const typename QtPrivate::FunctionPointer<Func1>::Object *sender, Func1 signal, Func2 slot)
{
qDebug("There is no here argument for connection, isn't it?");
return connect(sender, signal, sender, slot, Qt::DirectConnection);
}
//second
//connect to a functor, with a "context" object defining in which event loop is going to be executed
template <typename Func1, typename Func2>
static inline typename QtPrivate::QEnableIf<QtPrivate::FunctionPointer<Func2>::ArgumentCount == -1, QMetaObject::Connection>::Type
connect(const typename QtPrivate::FunctionPointer<Func1>::Object *sender, Func1 signal, const QObject *context, Func2 slot,
Qt::ConnectionType type = Qt::AutoConnection)
{
qDebug("This will be called, and as you can see you need specify the context if you want to use connection type.");
//...
其次,当您运行此代码时,您将获得:
QObject :: connect:无法对类型的参数进行排队 &#39;另外,QProcess ::的processError&#39; (确保&#39; QProcess :: ProcessError&#39;是 使用qRegisterMetaType()注册。)
所以你需要在连接之前添加qRegisterMetaType<QProcess::ProcessError>("QProcess::ProcessError");
。
所以最终版本是:
qRegisterMetaType<QProcess::ProcessError>("QProcess::ProcessError");
QProcess process;
connect(&process, static_cast<void (QProcess::*)(QProcess::ProcessError)>
(&QProcess::error), this , [=](QProcess::ProcessError pError) {
qWarning() << "error " << pError;
},Qt::QueuedConnection);
process.start("MyProgram");
bool launched = process.startDetached("example");