Qt QProcess :: finished()信号没有这样的插槽

时间:2012-09-21 16:23:59

标签: c++ qt

我正在尝试运行命令行程序,gphoto2来自我在Linux中运行的Qt应用程序并读取它输出到标准输出和标准错误的结果。此概念验证程序中的GUI是单个按钮和标签,用于显示标准错误和标准输出的输出。

我在将QtProcess :: Finished信号连接到正确的插槽时遇到问题。我从头文件,连接语句和函数中的Finished()信号文档中复制了参数列表。函数名称以MainWindow :: class标识符为前缀。我已经没有什么可尝试的了,我希望StackOverflow中的某个人能够指出问题所在。

The Header file:
#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);

}

3 个答案:

答案 0 :(得分:5)

我相信以下内容可行:

connect(cameraControl, SIGNAL(finished(int , QProcess::ExitStatus )), this, SLOT(on_cameraControlExit(int , QProcess::ExitStatus )));

答案 1 :(得分:3)

Qt5 convention

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

答案 2 :(得分:0)

我有同样的问题。我认为这是因为完成的信号函数超载了2个签名,并且编译器无法推断出类型:

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

以下是我的快速解决方法:

1)打开qprocess.h

2)评论“较短”的签名:

//  void finished(int exitCode); 

3)然后将完成信号与lambda插槽连接:

 QObject::connect(&process, &QProcess::finished, [=](int exitCode, QProcess::ExitStatus exitStatus){
       qDebug() << "finished. Exit code: " + exitCode ;
    });