Search code examples
qtqprocessqtembedded

Need to quit a process from inside Qt GUI, just as it is started


I am trying to run C++ executables placed inside SBC6845 [inside /ftest as shown ]. Now these executables are running with

while(1){
// around 250-300 lines of code here
}

infinite loop. Now when I run only the codes from terminal, I can kill them whenever I want to. But I am unable to kill them while running from inside the gui. I execute these codes with Qprocess like this:

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

connect(ui->pushButton, SIGNAL(pressed()), SLOT(vcm_test()));
connect(ui->pushButton_2, SIGNAL(pressed()), SLOT(offloader_test()));
connect(ui->pushButton_3, SIGNAL(pressed()), SLOT(quit_vcm()));
connect(ui->pushButton_4, SIGNAL(pressed()), SLOT(quit_offloader()));}
void MainWindow::vcm_test(){
   QProcess::execute("/ftest/vcm_test_2 \r\n");
}
void MainWindow::offloader_test(){
    QProcess::execute("/ftest/off_test_2 \r\n");
}  
void MainWindow::quit_vcm(){
    QProcess::execute("\x001a \r\n");
} 
void MainWindow::quit_offloader(){
    QProcess::execute("\x001a \r\n");   
}

Now the problem is when the pushbutton or pushbutton_2 i.e. vcm_test() or offloader_test() is invoked the gui becomes unresponsive. Since the gui keeps waiting for the code in /ftest to finish quit option does not work and I have to quit it from the terminal again. Also quitting from terminal closes both the code and the gui.

I have tried searching solutions and used threads too. But I get segmentation error while starting a thread from pushbutton.

I need to be able to quit the process while it is being executed (modification of this code or any new idea is very much appreciated). I am a newbie so please ignore my poor coding skills. Thanks.


Solution

  • QProcess::execute(..) waits for the process to finish, that is why your GUI is freezing. Use QProcess::start(..) instead. To quit the process use the QProcess::close() function

    Try this:

    QProcess *myProcess = new QProcess(this);
    myProcess->start("/ftest/vcm_test_2");
    

    And when you want to close the process:

    myProcess->close();
    

    You can also connect your pushbutton's clicked signal to the process' kill slot:

    connect(ui->pushButton_3, SIGNAL(clicked()), myProcess, SLOT(kill());