gpt4 book ai didi

qt - Q流程问题,流程输出

转载 作者:行者123 更新时间:2023-12-02 10:34:11 25 4
gpt4 key购买 nike

我正在尝试弄清楚 QProcess 的用法。我查看了 Qt 文档,但没有运气。
http://doc.qt.io/qt-4.8/qprocess.html

问题示例。

示例 1:以下代码有效。

    #include <QtCore/QCoreApplication>
#include <QTextStream>
#include <QByteArray>
#include <QProcess>

int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QTextStream qout(stdout);

QProcess cmd;

cmd.start("cmd");
if (!cmd.waitForStarted()) {
return false;
}

cmd.waitForReadyRead();
QByteArray result = cmd.readAll();
//qout << result.data() << endl; //console junk captured, doesn't show.

//My test command
cmd.write("echo hello");
cmd.write("\n");

//Capture my result
cmd.waitForReadyRead();
//This is my command shown by cmd, I don't show it, capture & discard it.
result = cmd.readLine();
//Read result of my command ("hello") and the rest of output like cur dir.
result = cmd.readAll();
qout << result.data();

qout << "\n\n---End, bye----" << endl;
return a.exec();
}

上述代码的输出

hello

F:\Dev_Qt\expControllingExtConsoleApps-build-desktop>

---End, bye----

问题是,如果我尝试通过 Qprocess 和 cmd 控制台以这种方式使用 ipconfig 或 7zip,我将无法看到 ipconfig 或 7zip 的任何输出。我不知道是否完成了任何操作,如果完成了某些操作,为什么我看不到输出?下面的代码说明了这一点。

示例 2: 不起作用。无法使用 ipconfig。

#include <QtCore/QCoreApplication>
#include <QTextStream>
#include <QByteArray>
#include <QString>
#include <QProcess>

int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QTextStream qout(stdout);

QProcess cmd2;
cmd2.setWorkingDirectory("C:/Program Files/7-Zip"); //not needed in this example.
cmd2.setReadChannel(QProcess::StandardOutput);
cmd2.setProcessChannelMode(QProcess::MergedChannels);

cmd2.start("cmd");
if (!cmd2.waitForStarted())
{
qout << "Error: Could not start!" << endl;
return false;
}

cmd2.waitForReadyRead();
QByteArray result = cmd2.readAll();
qout << result.data() << endl; //Console version info, etc.

//My command
cmd2.write("ipconfig");
cmd2.write("\n");

//Capture output of ipconfig command
//DOES NOT WORK!!
cmd2.waitForReadyRead();
while (! cmd2.atEnd())
{
result = cmd2.readLine();
qout << result;
result.clear();
}
qout << endl;

qout << "\n\n---end----" << endl;
return a.exec();

}

输出如下,缺少 ipconfig 连接信息结果。根本没有捕获 ipconfig 的输出。

Microsoft Windows XP [Version5.1.2600] (C) Copyright 1985-2001 Microsoft Corp.

C:\Program Files\7-Zip> ipconfig

---end----

应该更像这样(带有 ipconfig 结果)。

Microsoft Windows XP [Version5.1.2600] (C) Copyright 1985-2001 Microsoft Corp.

C:\Documents andSettings\noname>ipconfig

Windows IP Configuration

Ethernet adapter Local AreaConnection:

    Connection-specific DNS Suffix  . :
IP Address. . . . . . . . . . . . : 192.172.148.135
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . : 192.172.148.177

C:\Documents and Settings\noname>

显然,输出应该与上面略有不同,但作为“ipconfig”输出的连接信息应该已被捕获。同样,如果我尝试通过 cmd 控制台使用 7zip...我看不到/捕获 7zip 的任何输出。所以我的问题是如何通过 QProcess 和 cmd 控制台使用 ipconfig 和 7zip 等命令行应用程序并查看这些应用程序的输出结果?

示例 3:7zip 不起作用

#include <QtCore/QCoreApplication>
#include <QTextStream>
#include <QByteArray>
#include <QProcess>

int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QTextStream qout(stdout);

QProcess cmd2;
cmd2.setWorkingDirectory("C:/Program Files/7-Zip");
cmd2.setReadChannel(QProcess::StandardOutput);
cmd2.setProcessChannelMode(QProcess::MergedChannels);

cmd2.start("cmd");
if (!cmd2.waitForStarted()) {
return false;
}

//My Command
cmd2.write("7z.exe");
cmd2.write("\n");

//Capture output of ipconfig command
cmd2.waitForReadyRead();
QByteArray result;

while (! cmd2.atEnd()) {
result = cmd2.readLine();
qout << result;
result.clear();
}
qout << endl;

qout << "\n\n---end----" << endl;
return a.exec();
}

输出如下。不显示 7zip 中的任何内容。

Microsoft Windows XP [Version5.1.2600] (C) Copyright 1985-2001 Microsoft Corp.

C:\Program Files\7-Zip>7z.exe

---end----

输出预计将遵循...

Microsoft Windows XP [Version5.1.2600] (C) Copyright 1985-2001 Microsoft Corp.

C:\Documents and Settings\noname>cdC:\Program Files\7-Zip

C:\Program Files\7-Zip>7z.exe

7-Zip 9.15 beta Copyright (c)1999-2010 Igor Pavlov 2010-06-20

Usage: 7z [...]<archive_name> [<file_names>...][<@listfiles...>]

a: Add files to archive
b: Benchmark d: Delete files fromarchive e: Extract files fromarchive (without using directorynames) l: List contents of archive
t: Test integrity of archive u:Update files to archive x: eXtractfiles with full paths
-ai[r[-|0]]{@listfile|!wildcard}: Include archives
-ax[r[-|0]]{@listfile|!wildcard}: eXclude archives -bd: Disablepercentage indicator
-i[r[-|0]]{@listfile|!wildcard}: Include filenames -m{Parameters}:set compression Method
-o{Directory}: set Output directory -p{Password}: set Password -r[-|0]: Recurse subdirectories -scs{UTF-8 |WIN | DOS}: set charset for list files-sfx[{name}]: Create SFX archive -si[{name}]: read data from stdin -slt: show technical information for l (List) command -so: write data tostdout -ssc[-]: set sensitive casemode -ssw: compress shared files
-t{Type}: Set type of archive -u[-][p#][q#][r#][x#][y#][z#][!newArchiveName]:Update options -v{Size}[b|k|m|g]:Create volumes -w[{path}]: assignWork directory. Empty path means atemporary directory
-x[r[-|0]]]{@listfile|!wildcard}: eXclude filenames -y: assume Yes onall queries

C:\Program Files\7-Zip>

最佳答案

e我发现一个大问题。在 Windows 下,您可以按 Enter 键发出命令。写作

cmd.write("command");
cmd.write("\n");


仅仅编写还不够

cmd.write("command");
cmd.write("\n\r");

注意结尾的\r。试试这个,它应该工作得更好,我所说的更好是指 7zip。我不知道你是否能让 ipconfig 正常工作。

祝你好运,并致以诚挚的问候
d

编辑这是一个可行的解决方案:


#include <QtCore/QCoreApplication>
#include <QtCore/QProcess>
#include <QtCore/QString>
#include <QtCore/QTextStream>

// Not clean, but fast
QProcess *g_process = NULL;

// Needed as a signal catcher
class ProcOut : public QObject
{
Q_OBJECT
public:
ProcOut (QObject *parent = NULL);
virtual ~ProcOut() {};

public slots:
void readyRead();
void finished();
};

ProcOut::ProcOut (QObject *parent /* = NULL */):
QObject(parent)
{}

void
ProcOut::readyRead()
{
if (!g_process)
return;

QTextStream out(stdout);
out << g_process->readAllStandardOutput() << endl;
}

void
ProcOut::finished()
{
QCoreApplication::exit (0);
}

int main (int argc, char **argv)
{
QCoreApplication *app = new QCoreApplication (argc, argv);

ProcOut *procOut = new ProcOut();
g_process = new QProcess();

QObject::connect (g_process, SIGNAL(readyReadStandardOutput()),
procOut, SLOT(readyRead()));
QObject::connect (g_process, SIGNAL(finished (int, QProcess::ExitStatus)),
procOut, SLOT(finished()));

g_process->start (QLatin1String ("cmd"));
g_process->waitForStarted();

g_process->write ("ipconfig\n\r");

// Or cmd won't quit
g_process->write ("exit\n\r");

int result = app->exec();

// Allright, process finished.
delete procOut;
procOut = NULL;

delete g_process;
g_process = NULL;

delete app;
app = NULL;

// Lets us see the results
system ("pause");

return result;
}

#include "main.moc"

希望有帮助。它每次在我的机器上都能工作。

关于qt - Q流程问题,流程输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5890955/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com