首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >C2665:'QObject::connect‘:3种重载都不能转换所有参数类型

C2665:'QObject::connect‘:3种重载都不能转换所有参数类型
EN

Stack Overflow用户
提问于 2015-07-16 21:26:07
回答 1查看 2.5K关注 0票数 2

我主要有以下代码

代码语言:javascript
运行
复制
         QProcess process;
        QObject::connect(&process, &QProcess::error, [](QProcess::ProcessError error)
        {
            qDebug() << error;
        }, Qt::QueuedConnection);
        bool launched = process.startDetached("D:\temp.exe");

它在编译时生成此错误。

代码语言:javascript
运行
复制
    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

EN

回答 1

Stack Overflow用户

发布于 2015-07-17 05:33:38

我不应该发布这个答案,但老实说,这不是same question,它更复杂。

首先,为什么第一个版本不能工作。因为如果不提供receiver,就不能使用附加参数(连接类型)。这意味着下一个是错误的。

代码语言:javascript
运行
复制
connect(&process, static_cast<void (QProcess::*)(QProcess::ProcessError)>
        (&QProcess::error),[=](QProcess::ProcessError pError) {
        qWarning() << "error " << pError;
},Qt::QueuedConnection);

但下一步是正确的:

代码语言:javascript
运行
复制
connect(&process, static_cast<void (QProcess::*)(QProcess::ProcessError)>
        (&QProcess::error), this , [=](QProcess::ProcessError pError) {
        qWarning() << "error " << pError;
},Qt::QueuedConnection);

如果你想知道为什么,请看qobject.h。我在这个文件中做了一些修改,只是为了更准确(不要更改这个文件!)

代码语言:javascript
运行
复制
//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.");
    //...

其次,当您运行此代码时,您将得到:

连接:不能将类型为'QProcess::ProcessError‘的参数排队(确保'QProcess::ProcessError’使用qRegisterMetaType()注册。)

因此,您需要在连接之前添加qRegisterMetaType<QProcess::ProcessError>("QProcess::ProcessError");

因此,最终版本是:

代码语言:javascript
运行
复制
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");
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31464822

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档