stopping qprocess from QDialog
In my app I use a qprocess which starts on button clicked event in mainwindow.
Code:
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
Code:
void dialog::on_StopButton_clicked()
{
MainWindow *mainwin=new MainWindow(this);
connect(ui->StopButton,SIGNAL(clicked()),mainwin,SLOT(on_stopButton_clicked()));
}
mainwindow.cpp
Code:
void MainWindow::on_stopButton_clicked()
{
commandProcess.kill(); //even tried commandProcess.close();
ui->progressBar->hide();
}
but the qprocess is not stopping......plzz help.........
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.
Re: stopping qprocess from QDialog
Place in mainwindow the row with connect(...)
and in your dialog emit signal
Re: stopping qprocess from QDialog
Quote:
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
Code:
void Dialog::on_StopButton_clicked()
{
MainWindow *mainwin;
connect(ui->cmdStopButton,SIGNAL(clicked()),mainwin,SLOT(on_stopButton_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???
Re: stopping qprocess from QDialog
Quote:
but instead of closing the qprocess of mainwindow, the whole program exits unexpectedly....
Well, you try to use non existing object:
Code:
MainWindow *mainwin;// its not an object, its just a pointer (invalid)
connect(ui->cmdStopButton,SIGNAL(clicked()),mainwin,SLOT(on_stopButton_clicked()));
Note that this is a general C++ issue: about pointers
Quote:
when the user clicks stop button the qprocess in the mainwindow must stop
This must be the same mainWindow instance that started the process:
Code:
MainWindow *mainwin=new MainWindow(this);
connect(ui->StopButton,SIGNAL(clicked()),mainwin,SLOT(on_stopButton_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.
Re: stopping qprocess from QDialog
got solution to the above probleem........
Dialog.h
Code:
signals:
void stopprocess();
Dialog.cpp
Code:
//constructor
this->parent = parent;
connect(ui->StopButton,SIGNAL(clicked()),this,SLOT(on_StopButton_clicked()));
connect(this,SIGNAL(stopprocess()),parent,SLOT(on_stopButton_clicked()));
void CmdOutputDialog::on_cmdStopButton_clicked()
{
emit stopprocess();
}
works properly now......