QProcess:未收到运行 Powershell 脚本的完成()信号 [英] QProcess: not receiving finished() signal running Powershell script
问题描述
我正在开发一个 Qt 应用程序,其中包括转换 Excel 电子表格中以标签文件分隔的文本.这是通过运行 Windows Powershell 脚本来完成的.
我的问题是 QProcess 中的 finished() 信号从未发出,尽管转换已成功完成.是的,我收到 stateChanged() 信号.
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.
(改编自这个问题)
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 应用标题
(为了测试,最小的情况是一个没有其他小部件的 QDialog.)
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 应用 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;
}
尽管转换成功并显示状态 1(已启动)和状态 2(正在运行),但永远不会显示消息已完成".
我也尝试同步做,通过waitForFinished()方法,总是超时.
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.
- Windows 7 Professional 64 位
- Powershell v1.0 x86
- 带有 minGW491_32 的 Qt 5.4.0
感谢您的帮助.谢谢.
推荐答案
我找到了解决方案.显然,Powershell 从控制台或从另一个程序(非交互式)调用时不会以相同的方式运行.如果我在 ps_excel.ps1 脚本末尾添加:
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
代替
exit 0
我真的停止Powershell并获得完成信号,QProcess::ExitStatus = 0.
I Stop really Powershell and get finished signal, with QProcess::ExitStatus = 0.
这篇关于QProcess:未收到运行 Powershell 脚本的完成()信号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!