我在 main 中有以下代码
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
最佳答案
我不应该发布这个答案,但说实话,它不是 same question ,情况比较复杂。
首先,为什么第一个版本不起作用。因为如果不提供 receiver
则无法使用附加参数(连接类型) 。这意味着 next 是错误的。
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: Cannot queue arguments of type 'QProcess::ProcessError' (Make sure 'QProcess::ProcessError' is registered using 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");
关于c++ - C2665 : 'QObject::connect' : none of the 3 overloads could convert all the arguments types,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31464822/