QProcess: not receiving finished() signal running Powershell script

[亡魂溺海] 提交于 2021-02-20 04:36:01

问题


I am developing a Qt application that, among other things, converts an Excel spreadsheet in a text delimited with tab file. This is done by running a Windows Powershell script.
My problem is that the finished() signal from the QProcess is never emitted, although the conversion is done successfully. And yes, I receive stateChanged() signal.

Powershell script (ps_excel.ps1)

(adapted from this question)

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

Qt app header

(For testing, the minimal case is a QDialog without other widgets.)

#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>
#include <QProcess>
#include <QDebug>

namespace Ui {
class Dialog;
}

class Dialog : public QDialog
{
    Q_OBJECT

    public:
    explicit Dialog(QWidget *parent = 0);
    ~Dialog();
    QProcess *proces;

    private:
        Ui::Dialog *ui;
    private slots:
        void procFinish(int estat);
        void procState(QProcess::ProcessState estat);
};

#endif // DIALOG_H

Qt app C++

#include "dialog.h"
#include "ui_dialog.h"

Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
    ui->setupUi(this);
    QString program = "C:/Windows/System32/WindowsPowerShell/v1.0/powershell.exe";
    QStringList params;
    params << "-File" << "C:/Garsineu/ps_excel.ps1" << "-ent" << "C:/Garsineu/PROVAGALATEA.xls" << "-sal" << "C:/Garsineu/PROVAGALATEA.tab";
    proces = new QProcess();
    connect(proces, SIGNAL(finished(int)), this, SLOT(procFinish(int)));
    connect(proces, SIGNAL(stateChanged(QProcess::ProcessState)), this, SLOT(procState(QProcess::ProcessState)));
    proces->start(program, params);
}

Dialog::~Dialog()
{
    delete ui;
}

void Dialog::procFinish(int estat)
{
    qDebug() << "Finished";
}

void Dialog::procState(QProcess::ProcessState estat)
{
    qDebug() << estat;
}

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.

Environment

  • Windows 7 Professional 64 bit
  • Powershell v1.0 x86
  • Qt 5.4.0 with minGW491_32

I appreciate your help. Thank you.


回答1:


I found the solution. 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 ps_excel.ps1 script:

Get-Process Powershell | Stop-Process

instead of

exit 0

I Stop really Powershell and get finished signal, with QProcess::ExitStatus = 0.



来源:https://stackoverflow.com/questions/27878121/qprocess-not-receiving-finished-signal-running-powershell-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!