本文主要是介绍QProcess 使用实例,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
QProcess运行一个命令行,比如ipconfig示例cpp主要的代码如下所示:
QStringList m_Arguments;
m_Arguments << "/c" << "ipconfig";
QString m_Command("cmd.exe");
QString m_Input;
run();
/
QString run()
{
QProcess process;
process.setReadChannel(QProcess::StandardOutput);
process.start(m_Command, m_Arguments);
//启动完毕
connect(&process, &QProcess::started, this, [=]()
{
int ttt = 1;
});
//捕获到消息时
connect(&process, &QProcess::readyReadStandardOutput, this, [=]()
{
int ttt = 1;
});
//完成
connect(&process, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished), this,
[=](int exitCode, QProcess::ExitStatus exitStatus)
{
if (exitStatus == QProcess::NormalExit)
{
int ttt = 1;
}
else {
int ttt = 1;
}
});
//状态改变
connect(&process, &QProcess::stateChanged, this, [=](QProcess::ProcessState state)
{
int ttt = 1;
switch (state)
{
case QProcess::NotRunning:
ttt = 1;
break;
case QProcess::Starting:
ttt = 1;
break;
case QProcess::Running:
ttt = 1;
break;
default:
int ttt = 1;
break;
}
});
bool success = process.waitForStarted();
QString output, error;
if (success)
{
if (!m_Input.isEmpty())
{
process.write(m_Input.toStdString().c_str());
}
if (process.waitForFinished(5000))
{
output = QString::fromLocal8Bit(process.readAllStandardOutput()).trimmed();
error = QString::fromLocal8Bit(process.readAllStandardError()).trimmed();
}
}
int code = process.exitCode();
if (!error.isEmpty() || !success || code != 0)
{
}
return output;
}
这篇关于QProcess 使用实例的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!