Results 1 to 9 of 9

Thread: QProcess: finished() signal not emitted running Powershell script

  1. #1
    Join Date
    Jan 2015
    Location
    Tremp, Spain
    Posts
    14
    Thanks
    6
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default QProcess: finished() signal not emitted running Powershell script

    In my application, I convert an Excel spreadsheet into a tab-delimited file. This is done by running a Windows Powershell script into a QProcess.
    My problem is that the finished() signal is never emitted, although the conversion is done successfully. And yes, I receive stateChanged() signal.

    Powershell script
    param ([string]$ent = $null, [string]$sal = $null)
    $xlCSV = -4158 #value for tab delimited file
    $Excel = New-Object -Com Excel.Application
    $Excel.visible = $False
    $Excel.displayalerts=$False
    $b = $Excel.Workbooks.Open($ent)
    $b.SaveAs($sal,$xlCSV)
    $Excel.quit()
    exit 0 #tested without exit, with exit and with exit 0

    Header:
    Qt Code:
    1. #ifndef DIALOG_H
    2. #define DIALOG_H
    3. #include <QDialog>
    4. #include <QProcess>
    5. #include <QDebug>
    6.  
    7. namespace Ui {
    8. class Dialog;
    9. }
    10.  
    11. class Dialog : public QDialog
    12. {
    13. Q_OBJECT
    14.  
    15. public:
    16. explicit Dialog(QWidget *parent = 0);
    17. ~Dialog();
    18. QProcess *proces;
    19.  
    20. private:
    21. Ui::Dialog *ui;
    22. private slots:
    23. void procFinish(int estat);
    24. void procState(QProcess::ProcessState estat);
    25. };
    26.  
    27. #endif // DIALOG_H
    To copy to clipboard, switch view to plain text mode 
    Source:
    Qt Code:
    1. #include "dialog.h"
    2. #include "ui_dialog.h"
    3.  
    4. Dialog::Dialog(QWidget *parent) :
    5. QDialog(parent),
    6. ui(new Ui::Dialog)
    7. {
    8. ui->setupUi(this);
    9. QString program = "C:/Windows/System32/WindowsPowerShell/v1.0/powershell.exe";
    10. QStringList params;
    11. params << "-File" << "C:/Garsineu/ps_excel.ps1" << "-ent" << "C:/Garsineu/PROVAGALATEA.xls" << "-sal" << "C:/Garsineu/PROVAGALATEA.tab";
    12. proces = new QProcess();
    13. connect(proces, SIGNAL(finished(int)), this, SLOT(procFinish(int)));
    14. connect(proces, SIGNAL(stateChanged(QProcess::ProcessState)), this, SLOT(procState(QProcess::ProcessState)));
    15. proces->start(program, params);
    16. }
    17.  
    18. Dialog::~Dialog()
    19. {
    20. delete ui;
    21. }
    22.  
    23. void Dialog::procFinish(int estat)
    24. {
    25. qDebug() << "Finished";
    26. }
    27.  
    28. void Dialog::procState(QProcess::ProcessState estat)
    29. {
    30. qDebug() << estat;
    31. }
    To copy to clipboard, switch view to plain text mode 
    Although the conversion is successful and the states 1 (Started) and 2 (Running) are shown, the message "Finished" is never displayed.
    I also tried to do it synchronously, by waitForFinished () method, which is always timed out.
    The application needs to know when the process is complete, to carry out further actions on the converted file.

    I appreciate your help. Thank you.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QProcess: finished() signal not emitted running Powershell script

    Does the script end/exit if you run it manually in a console window?

    Cheers,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    Laureta (11th January 2015)

  4. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QProcess: finished() signal not emitted running Powershell script

    Take a look at the console output from your program at run time and/or the return value from connect(). It is likely that you are receiving warning that QProcess does not have a signal matching "finished(int)" and that connect() returns false (Qt4) or a QMetaObject::Connection that evaluates as false (Qt5). The signal is QProcess::finished(int exitCode, QProcess::ExitStatus exitStatus)

  5. The following 2 users say thank you to ChrisW67 for this useful post:

    anda_skoa (11th January 2015), Laureta (11th January 2015)

  6. #4
    Join Date
    Jan 2015
    Location
    Tremp, Spain
    Posts
    14
    Thanks
    6
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: QProcess: finished() signal not emitted running Powershell script

    Does the script end/exit if you run it manually in a console window?
    Yes, running the script in Powershell console performs conversion and closes the console. I have also tried replacing the last line of script by:

    Get-Host.SetShouldExit(0)

    Which is supposed provides an exit code.

    Quote Originally Posted by ChrisW67 View Post
    Take a look at the console output from your program at run time and/or the return value from connect(). It is likely that you are receiving warning that QProcess does not have a signal matching "finished(int)" and that connect() returns false (Qt4) or a QMetaObject::Connection that evaluates as false (Qt5). The signal is QProcess::finished(int exitCode, QProcess::ExitStatus exitStatus)
    I changed the connection and slot accordingly:
    Qt Code:
    1. connect(proces, SIGNAL(finished(int,QProcess::ExitStatus)), SLOT(procFinish(int,QProcess::ExitStatus)));
    2.  
    3. void Dialog::procFinish(int exitC, QProcess::ExitStatus exitS) {
    4. qDebug() << exitC << exitS;
    5. }
    To copy to clipboard, switch view to plain text mode 
    The result of connect evaluates to TRUE. With the same (no) result.
    Last edited by Laureta; 11th January 2015 at 08:58.

  7. #5
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QProcess: finished() signal not emitted running Powershell script

    Have you tried connecting to the QProcess' error(QProcess::ProcessError) signal to see if any error occurs?

    By the way, your code leaks the QProcess instance (but this does not cause the problem being discussed).

  8. #6
    Join Date
    Jan 2015
    Location
    Tremp, Spain
    Posts
    14
    Thanks
    6
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: QProcess: finished() signal not emitted running Powershell script

    Quote Originally Posted by yeye_olive View Post
    Have you tried connecting to the QProcess' error(QProcess::ProcessError) signal to see if any error occurs?

    By the way, your code leaks the QProcess instance (but this does not cause the problem being discussed).
    I have added the connect and slot to capture the possible error; no errors reported.
    Qt Code:
    1. connect(proces, SIGNAL(error(QProcess::ProcessError)), this, SLOT(procError(QProcess::ProcessError)));
    2.  
    3. void Dialog::procError(QProcess::ProcessError error) {
    4. qDebug() << error;
    5. }
    To copy to clipboard, switch view to plain text mode 

  9. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QProcess: finished() signal not emitted running Powershell script

    What if you exchange your external process call with a different one, e.g. running notepad or something like that? Do you get the finished signal when you exit that external program?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. The following user says thank you to wysota for this useful post:

    Laureta (11th January 2015)

  11. #8
    Join Date
    Jan 2015
    Location
    Tremp, Spain
    Posts
    14
    Thanks
    6
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: QProcess: finished() signal not emitted running Powershell script

    Quote Originally Posted by wysota View Post
    What if you exchange your external process call with a different one, e.g. running notepad or something like that? Do you get the finished signal when you exit that external program?
    I have another application that calls mysqldump to backup and restore the database, and I get finished signal.
    Although I don't like, I found a workaround: at the last line of script I write something (e.g. "End") to stdout. Then I test the "end" as follows:

    Qt Code:
    1. connect(proces, SIGNAL(readyReadStandardOutput()), this, SLOT(test()));
    2.  
    3. void Dialog::test() {
    4. qDebug() << proces->readAllStandardOutput(); // "End"
    5. }
    To copy to clipboard, switch view to plain text mode 
    I reviewed all documentation I found about Powershell, including my Holy Bible -I mean Windows 7 Resource Kit (paperback) Perhaps that's one of the Microsoft's undocumented quirks...

    Edit: (3 min. later)
    I found the solution (no Qt problem). Apparently powershell does not run in the same way from the console or when is called from another program (noninteractive). If I add at end of script:

    Get-Process Powershell | Stop-Process
    I Stop * really * powershell and get finished signal, with QProcess::ExitStatus = 0.
    The slot:
    Qt Code:
    1. void Dialog::procState(QProcess::ProcessState estat) {
    2. qDebug() << estat;
    3. }
    To copy to clipboard, switch view to plain text mode 
    now shows the states: 1 (starting), 2 (running) and 0 (not running).

    Thanks to all of you for making me think a little longer.
    Last edited by Laureta; 11th January 2015 at 20:17.

  12. #9
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QProcess: finished() signal not emitted running Powershell script

    You might also try the "-NonInteractive" argument to the Powershell.exe and see if that allows the exit.

Similar Threads

  1. QNetworkAccessManager no finished() signal emitted
    By realperson in forum Qt Programming
    Replies: 4
    Last Post: 18th January 2018, 08:42
  2. QProcess finishes but never emits finished signal
    By BettaUseYoNikes in forum Qt Programming
    Replies: 2
    Last Post: 3rd December 2011, 08:01
  3. Running an script using QProcess
    By DiegoTc in forum Newbie
    Replies: 1
    Last Post: 31st December 2010, 18:02
  4. Replies: 0
    Last Post: 24th May 2010, 08:11
  5. QProcess object not emitting finished(int) signal
    By Tiansen in forum Qt Programming
    Replies: 13
    Last Post: 14th November 2008, 12:17

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
  •  
Qt is a trademark of The Qt Company.