Domkrt
Domkrt

Reputation: 85

Qt QProcess::kill() not kill the process

I develop a GUI app in Qt.

This runs a Python program, with QProcess. In myapp.h file is the pr def:

QProcess *pr = new QProcess(this);

In constructor is this:

QString file = "program/path.py";
pr->start("[...]/Programs/Python/Python38-32/python.exe", QStringList() << file);

And the destructor have this code:

pr->kill();
delete ui;

If I run this program, I don't have errors. I close the window, but this process still run, but I don't know why.

I'm working in Windows.

Upvotes: 0

Views: 1101

Answers (1)

Parisa.H.R
Parisa.H.R

Reputation: 3908

I test this example and kill() works.

Maybe your process already was started in your system and the start function didn't work correctly.

Try this : I have 2 pushbuttons for the start and kill the process :

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QProcess>

QT_BEGIN_NAMESPACE
namespace Ui
{
class MainWindow;
}
QT_END_NAMESPACE

class MainWindow: public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);

    ~MainWindow();

private slots:
    void  on_pb_start_clicked();

    void  on_pb_kill_clicked();

private:
    Ui::MainWindow *ui;
    QProcess       *pr;
};

#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent):
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

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

void  MainWindow::on_pb_start_clicked()
{
    pr = new QProcess(this);

    pr->start("vlc");
}

void  MainWindow::on_pb_kill_clicked()
{
    pr->kill();
}

OutPut:

enter image description here

Upvotes: 1

Related Questions