PDA

View Full Version : stopping qprocess from QDialog



mohit_king
11th February 2011, 11:53
In my app I use a qprocess which starts on button clicked event in mainwindow.



public :
QProcess commandProcess; // mainwindow.h


I also got a Qdialog which has a Stop button in it. Now when the user clicks stop button the qprocess in the mainwindow must stop, but it aint stopping.

I tried connecting it as below:
dialog.cpp



void dialog::on_StopButton_clicked()
{
MainWindow *mainwin=new MainWindow(this);
connect(ui->StopButton,SIGNAL(clicked()),mainwin,SLOT(on_stopB utton_clicked()));
}


mainwindow.cpp


void MainWindow::on_stopButton_clicked()
{
commandProcess.kill(); //even tried commandProcess.close();
ui->progressBar->hide();
}

but the qprocess is not stopping......plzz help.........

nish
11th February 2011, 11:58
your code has several problems.
1. if you create a new mainwindow in stopbutton slot, then your original qprocess in original mainwindow cant be changed in anyway.
2. you are connecting the signal in the stop slot, which does not make any sense.

there are many solutions for example,
1. store the pointer to your main window in your dialog and then stop the process through that pointer
2. (better) generate a signal from your qdialog and capture it in mainwindow.

Archa4
11th February 2011, 14:12
Place in mainwindow the row with connect(...)
and in your dialog emit signal

mohit_king
12th February 2011, 05:26
Re: stopping qprocess from QDialog
your code has several problems.
1. if you create a new mainwindow in stopbutton slot, then your original qprocess in original mainwindow cant be changed in anyway.
2. you are connecting the signal in the stop slot, which does not make any sense.

there are many solutions for example,
1. store the pointer to your main window in your dialog and then stop the process through that pointer
2. (better) generate a signal from your qdialog and capture it in mainwindow.


Of ur two solutions provided i tried the first solution.....

DIALOG.cpp



void Dialog::on_StopButton_clicked()
{
MainWindow *mainwin;
connect(ui->cmdStopButton,SIGNAL(clicked()),mainwin,SLOT(on_st opButton_clicked()));
}

but instead of closing the qprocess of mainwindow, the whole program exits unexpectedly....

and about your second solution then, i didn't get it??.....can u elaborate it plzzz.....

How do u emit a QProcess signal???

stampede
12th February 2011, 06:40
but instead of closing the qprocess of mainwindow, the whole program exits unexpectedly....
Well, you try to use non existing object:

MainWindow *mainwin;// its not an object, its just a pointer (invalid)
connect(ui->cmdStopButton,SIGNAL(clicked()),mainwin,SLOT(on_st opButton_clicked()));
Note that this is a general C++ issue: about pointers (http://www.linuxconfig.org/c-understanding-pointers)


when the user clicks stop button the qprocess in the mainwindow must stop
This must be the same mainWindow instance that started the process:

MainWindow *mainwin=new MainWindow(this);
connect(ui->StopButton,SIGNAL(clicked()),mainwin,SLOT(on_stopB utton_clicked()));
This makes no sense, because the 'mainwin' is new instance, which didn't started your running process.

Better solution is 'solution 2' proposed by nish. Create a signal in your dialog class, connect it to your mainWindow slot, the one where you stop QProcess ( remember, it must be the same mainWindow that started the process ), and emit this signal from the method called on stop button click.

mohit_king
14th February 2011, 07:56
got solution to the above probleem........

Dialog.h


signals:
void stopprocess();



Dialog.cpp


//constructor
this->parent = parent;
connect(ui->StopButton,SIGNAL(clicked()),this,SLOT(on_StopButt on_clicked()));
connect(this,SIGNAL(stopprocess()),parent,SLOT(on_ stopButton_clicked()));

void CmdOutputDialog::on_cmdStopButton_clicked()
{
emit stopprocess();

}


works properly now......