xiaowei
xiaowei

Reputation: 23

How to make QProcess stop by button or keyboard in GUI?

Here I want to make a recorder by ffmpeg.exe.

And, I found the commend line, succeed running and generate the video file. And I know press "Esc" or "q" on keyboard can terminal

Now, I want to use GUI to control the recoder(ffmpeg.exe). I select Qt here, and my work environment is windows 7 sp1.

I use QProcess to execute it, you will see following.

But I have no idea for terminal the process.

The code list: main.cpp is simple.

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

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

    return a.exec();
}

mainwindow.h

#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

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??
}

There have some ideas for me?

or, have other solutions for my recorder?

Upvotes: 2

Views: 954

Answers (1)

Erik
Erik

Reputation: 2213

The following is working fine for me:

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

kill(thisPid, SIGINT);
QTimer::singleShot( 2000, pro, SLOT( tryTerminate() ) );
QTimer::singleShot( 5000, pro, SLOT( kill() ) );

This will first get the process id of the process and then

  1. Send an interrupt signal to the process (if you are under windows you will need to adapt this part)
  2. connects a one-shot timer to execute a tryTerminate after 2 seconds
  3. connects a one-shot timer to execute a kill after 5 seconds.

Let me know if this helps

Upvotes: 1

Related Questions