No slot QProcess::finished() signal
Asked Answered
P

3

8

I am trying to run a command line program, gphoto2 from my Qt app running on Linux, and read the results that it outputs to Standard Output and Standard Error.

The GUI in this proof of concept program is a single push button and a label that is used to display the output from Standard Error and Standard Output.

I'm having trouble connecting the QProcess::finished signal to the correct slot. I copied the arguments list from the finished() signal documentation in the header, the connect statement, and the function.

The function name is prefixed with the MainWindow:: class identifier.

mainwindow.h:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H
    
#include <QMainWindow>
#include <QString>
#include <QProcess>
#include <QObject>
    
namespace Ui {
    class MainWindow;
}
    
class MainWindow : public QMainWindow
{
    Q_OBJECT
        
public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
    void reply2();    
        
private slots:
    void on_pushButton_clicked();
    void on_cameraControlExit(int exitCode, QProcess::ExitStatus exitStatus);
    
    
private:
    Ui::MainWindow *ui;
    QProcess* cameraControl;
};
    
#endif // MAINWINDOW_H

mainwindow.cpp:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QProcess>
#include <QShortcut>
#include <QDebug>
        
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    cameraControl = new QProcess(this);            
}
    
MainWindow::~MainWindow()
{
    delete ui;
    cameraControl->close();
    delete cameraControl;
}
    
void MainWindow::on_pushButton_clicked()
{
    //connect the camera control finished signal to the slot that will read the exit code and
    //place the std output string from the process into a label on the form
    connect(cameraControl, SIGNAL(finished(int , QProcess::ExitStatus )),
            this, SLOT(MainWindow::on_cameraControlExit(int exitCode, QProcess::ExitStatus exitStatus)));
    
    //Disable the ui button do we don't get double presses
    ui->pushButton->setDisabled(true);
    
    //setup the gphoto2 arguments list
    QStringList args;      
    args.append("--capture-image-and-download");
        
    //start the camera control
    cameraControl->start("gphoto2",args);
    
    //wait for the process to finish or 30 seconds whichever comes first
    cameraControl->waitForFinished(30000);
}
    
void MainWindow::on_cameraControlExit(int exitCode, QProcess::ExitStatus exitStatus)
{
    qDebug() << cameraControl->errorString();
    qDebug() << cameraControl->readAllStandardError();
    qDebug() << cameraControl->readAllStandardOutput();
    
    ui->pushButton->setEnabled(true);
}
Prewar answered 21/9, 2012 at 16:23 Comment(0)
S
5

I believe the following will work:

connect(cameraControl, SIGNAL(finished(int , QProcess::ExitStatus )), this, SLOT(on_cameraControlExit(int , QProcess::ExitStatus )));
Sessoms answered 21/9, 2012 at 16:29 Comment(1)
Also you may want to rename your slot on_cameraControlExit and remove the on_ prefix. Why I say that is Qt may warn that it can not automatically connecting a slot if you name your slots with that prefix. on_pushButton_clicked is good because it can automatically connect that to your pushButton.Sessoms
R
13

Or with C++14 (modern compilers)

QObject::connect(cameraControl, qOverload<int, QProcess::ExitStatus >(&QProcess::finished), this, &MainWindow::on_cameraControlExit);

Or in the Qt5 convention:

QObject::connect(cameraControl, static_cast<void(QProcess::*)(int, QProcess::ExitStatus)>(&QProcess::finished), this, &MainWindow::on_cameraControlExit);
Rentier answered 11/8, 2017 at 18:24 Comment(0)
S
5

I believe the following will work:

connect(cameraControl, SIGNAL(finished(int , QProcess::ExitStatus )), this, SLOT(on_cameraControlExit(int , QProcess::ExitStatus )));
Sessoms answered 21/9, 2012 at 16:29 Comment(1)
Also you may want to rename your slot on_cameraControlExit and remove the on_ prefix. Why I say that is Qt may warn that it can not automatically connecting a slot if you name your slots with that prefix. on_pushButton_clicked is good because it can automatically connect that to your pushButton.Sessoms
S
0

I had the same issue. I think it happens because finished signal function is overloaded with 2 signatures and the compiler has trouble to infer the type:

void finished(int exitCode); 
void finished(int exitCode, QProcess::ExitStatus exitStatus);

Here is my quick-and-dirty workaround:

1) Open qprocess.h

2) Comment the "shorter" signatures:

//  void finished(int exitCode); 

3) Then connect finished-signal with your lambda slot:

 QObject::connect(&process, &QProcess::finished, [=](int exitCode, QProcess::ExitStatus exitStatus){
       qDebug() << "finished. Exit code: " + exitCode ;
    });
Sowers answered 29/10, 2015 at 21:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.