Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/141.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何在GUI中通过按钮或键盘停止QProcess?_C++_Qt_Ffmpeg - Fatal编程技术网

C++ 如何在GUI中通过按钮或键盘停止QProcess?

C++ 如何在GUI中通过按钮或键盘停止QProcess?,c++,qt,ffmpeg,C++,Qt,Ffmpeg,这里我想用ffmpeg.exe制作一个录音机 我找到了推荐行,成功运行并生成了视频文件。我知道在can终端上按键盘上的“Esc”或“q” 现在,我想使用GUI来控制录制器(ffmpeg.exe)。我在这里选择Qt,我的工作环境是Windows7SP1 我使用QProcess来执行它,您将看到以下内容 但我对这个过程一无所知 代码列表: main.cpp很简单 #include "mainwindow.h" #include <QApplication> int main(int a

这里我想用ffmpeg.exe制作一个录音机

我找到了推荐行,成功运行并生成了视频文件。我知道在can终端上按键盘上的“Esc”或“q”

现在,我想使用GUI来控制录制器(ffmpeg.exe)。我在这里选择Qt,我的工作环境是Windows7SP1

我使用QProcess来执行它,您将看到以下内容

但我对这个过程一无所知

代码列表: main.cpp很简单

#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}
#包括“mainwindow.h”
#包括
int main(int argc,char*argv[])
{
质量保证申请a(argc、argv);
主窗口w;
w、 show();
返回a.exec();
}
主窗口

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QProcess>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private:
    QProcess *pro;
    QString recorder;
    QString outputName;
    QString options;

private slots:
    void startRecode();
    void stopRecode();
};

#endif // MAINWINDOW_H
\ifndef主窗口
#定义主窗口
#包括
#包括
名称空间用户界面{
类主窗口;
}
类主窗口:公共QMainWindow
{
Q_对象
公众:
显式主窗口(QWidget*parent=0);
~main窗口();
私人:
QProcess*pro;
QString记录器;
QString输出名称;
QString选项;
专用插槽:
void startRecode();
void stopRecode();
};
#endif//main窗口
mainwindow.cpp

#include "mainwindow.h"

#include <QProcess>
#include <QTest>
#include <QPushButton>
#include <QVBoxLayout>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    pro(new QProcess)
{
    QDateTime current_date_time = QDateTime::currentDateTime();
    QString current_date = current_date_time.toString("yyyy-MM-dd-hh_mm_ss");

    recorder = "E:\\bin\\ffmpeg.exe";
    outputName = current_date + ".mp4";
    options = "-f gdigrab -framerate 6 -i desktop -vcodec libx264 -preset:v ultrafast -tune:v zerolatency -hide_banner -report";
    //-t 00:00:05"; this option can limit the process running time, and work well no GUI, but I don't want to use given time to stop recording.

    QWidget *centerWindow = new QWidget;
    setCentralWidget(centerWindow);

    QVBoxLayout *mainLayout = new QVBoxLayout;
    QPushButton *startButton = new QPushButton("start recording");
    QPushButton *stopButton = new QPushButton("stop recording");
    mainLayout->addWidget(startButton);
    mainLayout->addWidget(stopButton);

    centerWindow->setLayout(mainLayout);

    connect(startButton, SIGNAL(clicked()), this, SLOT(startRecode()));
    connect(stopButton, SIGNAL(clicked()), this, SLOT(stopRecode()));
}

MainWindow::~MainWindow()
{
    delete pro;
}

void MainWindow::startRecode()
{
    pro->execute(QString(recorder + " " +options +" " + outputName));
}

void MainWindow::stopRecode(){
    pro->terminate(); // not work
    //pro->close(); // not work, too
    //pro->kill(); // not work, T_T

    //how to terminate the process by pushbutton??
}
#包括“mainwindow.h”
#包括
#包括
#包括
#包括
主窗口::主窗口(QWidget*父窗口):
QMainWindow(父级),
pro(新QProcess)
{
QDateTime current_date_time=QDateTime::currentDateTime();
QString current_date=当前日期时间。toString(“yyyy-MM-dd-hh_MM_ss”);
recorder=“E:\\bin\\ffmpeg.exe”;
outputName=当前_日期+“.mp4”;
options=“-f gdigrab-framerate 6-i desktop-vcodec libx264-preset:v ultrafast-tune:v zerolatency-hide_banner-report”;
//-t 00:00:05“此选项可以限制进程的运行时间,并且在没有GUI的情况下运行良好,但我不想使用给定的时间停止录制。
QWidget*centerWindow=新的QWidget;
setCentralWidget(中心窗口);
QVBoxLayout*mainLayout=新的QVBoxLayout;
QPushButton*startButton=新的QPushButton(“开始录制”);
QPushButton*stopButton=新的QPushButton(“停止录制”);
mainLayout->addWidget(开始按钮);
mainLayout->addWidget(停止按钮);
中心窗口->设置布局(主布局);
连接(开始按钮、信号(单击())、此、插槽(开始代码());
连接(stopButton,信号(clicked()),此,插槽(stopRecode());
}
MainWindow::~MainWindow()
{
删除pro;
}
void主窗口::startRecode()
{
pro->execute(QString(记录器+“”+选项+“”+输出名));
}
void主窗口::stopRecode(){
pro->terminate();//不工作
//pro->close();//也不起作用
//pro->kill();//不起作用,T
//如何通过按钮终止进程??
}
有什么好主意吗


或者,我的录音机还有其他解决方案吗?

以下对我来说很好:

#include <QTimer>
#include <signal.h>
[...]
int thisPid = pro->pid();

kill(thisPid, SIGINT);
QTimer::singleShot( 2000, pro, SLOT( tryTerminate() ) );
QTimer::singleShot( 5000, pro, SLOT( kill() ) );
#包括
#包括
[...]
int thisPid=pro->pid();
杀死(thisPid,SIGINT);
QTimer::singleShot(2000,pro,SLOT(tryTerminate());
QTimer::singleShot(5000,pro,SLOT(kill());
这将首先获取进程的进程id,然后

  • 向进程发送中断信号(如果您在windows下,则需要调整此部分)
  • 连接一个一次性计时器,在2秒后执行
    tryTerminate
  • 连接一个一次性计时器,以在5秒后执行
    kill

  • 让我知道这是否有帮助。

    pro->kill()
    应该可以工作。您如何知道进程没有停止,它还在任务管理器中运行?@cmannett85 en…
    pro-kill()
    将做出“无响应”对于我的GUI。而且,正如你所说,GUI进程已经关闭,但ffmpeg.exe仍在运行,我需要在任务管理器中杀死ffmpeg.exe。呃……有没有办法向进程发送信号(当我在cmd.exe中按ESC键时),进程可以终止ffmpeg.exe进程?实际上根据docs
    execute()
    是此类的静态成员,因此可能应该使用
    pro::execute()调用它
    。我在某个地方读到,按你的方式调用它可能会导致问题。@Nadarian谢谢你的建议!我没有注意到执行
    是一个静态成员函数。是的,正如你所说,我的使用可能会导致一些问题。我在本地解决了这个问题。而回到我的问题,
    execute
    不能调用无限循环应用程序。…根据您的经验,使用
    标准详细说明
    (不是静态成员函数)更适合我?