gpt4 book ai didi

c++ - Qt 没有用于 QProcess::finished() 信号的插槽

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:08:09 27 4
gpt4 key购买 nike

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

我无法将 QtProcess::Finished 信号连接到正确的插槽。我从标题、连接语句和函数中的 Finished() 信号文档中复制了参数列表。函数名称以 MainWindow::类标识符为前缀。我已经没有什么可以尝试的了,我希望 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

主窗口.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);

}

最佳答案

或者使用 C++14(现代编译器)

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

或者在Qt5 convention :

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

关于c++ - Qt 没有用于 QProcess::finished() 信号的插槽,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12534367/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com