Results 1 to 6 of 6

Thread: stopping qprocess from QDialog

  1. #1
    Join Date
    Feb 2011
    Posts
    9
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default stopping qprocess from QDialog

    In my app I use a qprocess which starts on button clicked event in mainwindow.

    Qt Code:
    1. public :
    2. QProcess commandProcess; // mainwindow.h
    To copy to clipboard, switch view to plain text mode 

    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
    Qt Code:
    1. void dialog::on_StopButton_clicked()
    2. {
    3. MainWindow *mainwin=new MainWindow(this);
    4. connect(ui->StopButton,SIGNAL(clicked()),mainwin,SLOT(on_stopButton_clicked()));
    5. }
    To copy to clipboard, switch view to plain text mode 

    mainwindow.cpp
    Qt Code:
    1. void MainWindow::on_stopButton_clicked()
    2. {
    3. commandProcess.kill(); //even tried commandProcess.close();
    4. ui->progressBar->hide();
    5. }
    To copy to clipboard, switch view to plain text mode 
    but the qprocess is not stopping......plzz help.........

  2. #2
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default 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.

  3. #3
    Join Date
    Feb 2011
    Location
    Latvia
    Posts
    139
    Thanks
    24
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: stopping qprocess from QDialog

    Place in mainwindow the row with connect(...)
    and in your dialog emit signal

  4. #4
    Join Date
    Feb 2011
    Posts
    9
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: stopping qprocess from QDialog

    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

    Qt Code:
    1. void Dialog::on_StopButton_clicked()
    2. {
    3. MainWindow *mainwin;
    4. connect(ui->cmdStopButton,SIGNAL(clicked()),mainwin,SLOT(on_stopButton_clicked()));
    5. }
    To copy to clipboard, switch view to plain text mode 
    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???

  5. #5
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: stopping qprocess from QDialog

    but instead of closing the qprocess of mainwindow, the whole program exits unexpectedly....
    Well, you try to use non existing object:
    Qt Code:
    1. MainWindow *mainwin;// its not an object, its just a pointer (invalid)
    2. connect(ui->cmdStopButton,SIGNAL(clicked()),mainwin,SLOT(on_stopButton_clicked()));
    To copy to clipboard, switch view to plain text mode 
    Note that this is a general C++ issue: about 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:
    Qt Code:
    1. MainWindow *mainwin=new MainWindow(this);
    2. connect(ui->StopButton,SIGNAL(clicked()),mainwin,SLOT(on_stopButton_clicked()));
    To copy to clipboard, switch view to plain text mode 
    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.

  6. #6
    Join Date
    Feb 2011
    Posts
    9
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: stopping qprocess from QDialog

    got solution to the above probleem........

    Dialog.h
    Qt Code:
    1. signals:
    2. void stopprocess();
    To copy to clipboard, switch view to plain text mode 

    Dialog.cpp
    Qt Code:
    1. //constructor
    2. this->parent = parent;
    3. connect(ui->StopButton,SIGNAL(clicked()),this,SLOT(on_StopButton_clicked()));
    4. connect(this,SIGNAL(stopprocess()),parent,SLOT(on_stopButton_clicked()));
    5.  
    6. void CmdOutputDialog::on_cmdStopButton_clicked()
    7. {
    8. emit stopprocess();
    9.  
    10. }
    To copy to clipboard, switch view to plain text mode 

    works properly now......

Similar Threads

  1. Stopping the download process during pop3 session.
    By kremuwa in forum Qt Programming
    Replies: 2
    Last Post: 28th July 2010, 23:41
  2. Stopping a QThread
    By Gurdt in forum Qt Programming
    Replies: 4
    Last Post: 23rd April 2009, 22:21
  3. Replies: 2
    Last Post: 21st October 2008, 20:20
  4. Stopping QTimer
    By drinu21 in forum Qt Programming
    Replies: 4
    Last Post: 16th May 2008, 15:59
  5. stopping non-singlesshot QTimer
    By quickNitin in forum Newbie
    Replies: 1
    Last Post: 15th June 2006, 10:36

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.