3个重载都不能转换所有的参数类型

3个重载都不能转换所有的参数类型

本文介绍了C2665:'QObject :: connect':3个重载都不能转换所有的参数类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在主

         QProcess process;
        QObject::connect(&process, &QProcess::error, [](QProcess::ProcessError error)
        {
            qDebug() << error;
        }, Qt::QueuedConnection);
        bool launched = process.startDetached("D:\temp.exe");

并且在编译时产生此错误

and it is generating this error while compiling

    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)'

有人请帮助我,告诉我我做错了什么。

can someone please help me out and tell me what i am doing wrong.

我想连接来自 QProcess class to my lambda

I want to connect a signal from the QProcess class to my lambda

推荐答案

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

I shouldn't post this answer, but to be honest it is not same question, it is more complex.

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

First of all, why first version doesn't work. Because you can't use additional argument (connection type) without providing receiver. It means that next is wrong.

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 。我在这个文件中做一些更改,只是为了更准确(不要更改此文件!)。

If you wonder why, look at the qobject.h. I make some changes in this file, just to be more accurate (don't change this file!).

//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.");
    //...

其次,当您运行此代码时, / p>

Secondly when you run this code, you will get:

因此,您需要添加 qRegisterMetaType&

So you need add qRegisterMetaType<QProcess::ProcessError>("QProcess::ProcessError"); before the connection.

所以最终版本是:

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");

这篇关于C2665:'QObject :: connect':3个重载都不能转换所有的参数类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 18:42