C2665: "QObject :: connect": none of the three overloads can convert all argument types

I have the following code in the main

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

      

and it generates this error when 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 and tell me what I am doing wrong.

I want to connect a signal from a class QProcess

to my lambda

+2


source to share


1 answer


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

First of all why the first version doesn't work. Because you cannot use an extra argument (connection type) without providing receiver

. This means the following is wrong.

connect(&process, static_cast<void (QProcess::*)(QProcess::ProcessError)>
        (&QProcess::error),[=](QProcess::ProcessError pError) {
        qWarning() << "error " << pError;
},Qt::QueuedConnection);

      

But the following is correct:

connect(&process, static_cast<void (QProcess::*)(QProcess::ProcessError)>
        (&QProcess::error), this , [=](QProcess::ProcessError pError) {
        qWarning() << "error " << pError;
},Qt::QueuedConnection);

      

If you are wondering why, take a look at qobject.h

. I am making some changes to this file to be more precise (do not modify 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.");
    //...

      

Second, when you run this code, you get:

QObject :: connect: cannot sort arguments of type "QProcess :: ProcessError" (make sure "QProcess :: ProcessError" with qRegisterMetaType ().)

So, before connecting, you need to add qRegisterMetaType<QProcess::ProcessError>("QProcess::ProcessError");

.

So the final version:

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


source







All Articles