7

In following code I want to connect lambda function to QProcess::error signal:

void Updater::start() {
    QProcess process;
    QObject::connect(&process, &QProcess::error, [=] (QProcess::ProcessError error) {
        qWarning() << "error " << error;
    });
    process.start("MyProgram");
    process.waitForFinished();
}

But I get strange error:

error: no matching function for call to 'Updater::connect(QProcess* [unresolved overloaded function type], Updater::start()::)' });

What I do wrong here? The code executes inside method of class derived from QObject. The project configured to work with c++11.

I use Qt 5.3.1 on Linux x32 with gcc 4.9.2

1

2 Answers 2

8

Problem is that the QProcess has another error() method, so compiler just doesn't know which method use. If you want to deal with overloaded methods, you should use next:

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

Yes, it looks ugly, but there is no another way (only old syntax?).

This special line tells compiler that you want to use void QProcess::error(QProcess::ProcessError error), so now there is no any ambiguity

More information you can find here.

Sign up to request clarification or add additional context in comments.

3 Comments

see also stackoverflow.com/a/16795664/846250 for a small snippet to make the overload declaration "automatic"
Finding that static_cast requirement was a tricky one, thanks.
Just a side-note: same applies to the finished signal: static_cast<void (QProcess::*)(int, QProcess::ExitStatus)>(&QProcess::finished). Gotta love C++ spaghetti syntax. LOL
2

For those who are using Qt 5.6 or later, the QProcess::error signal is deprecated. You can use the QProcess::errorOccurred signal instead to avoid the naming ambiguity and complicated casting.

QProcess process;
connect(&process, &QProcess::errorOccurred, [=](QProcess::ProcessError error) {
    qWarning() << error;
});
process.start("MyProgram");
process.waitForFinished();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.