gpt4 book ai didi

c++ - C2665 : 'QObject::connect' : none of the 3 overloads could convert all the arguments types

转载 作者:搜寻专家 更新时间:2023-10-31 01:00:16 25 4
gpt4 key购买 nike

我在主程序中有以下代码

         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/

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