QT中调用外部程序:QProcess的使用
程序员文章站
2022-05-28 11:10:47
...
程序共包含以下有一个文件:
//: main.cpp
#include "process.h"
#include
#include
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QTextCodec::setCodecForTr(QTextCodec::codecForLocale());
QTextCodec::setCodecForCStrings(QTextCodec::codecForLocale());
Process w;
w.show();
return a.exec();
}
//: process.h
#ifndef PROCESS_H
#define PROCESS_H
#include
class QLabel;
class QLineEdit;
class QProcess;
class QPushButton;
class QTextEdit;
class Process : public QWidget
{
Q_OBJECT
public:
Process(QWidget *parent = 0, Qt::WFlags flags = 0);
~Process();
private slots:
void runClicked();
void readOutput();
private:
QProcess* cmd;
QString output;
//用于保存输出结果
QLabel* inputLabel;
QLineEdit* inputEdit;
QPushButton* runButton;
QLabel* outputLabel;
QTextEdit* outputEdit;
};
#endif // PROCESS_H
//: process.cpp
#include
#include "process.h"
Process::Process(QWidget *parent, Qt::WFlags flags = 0)
: QWidget(parent, flags)
{
inputLabel = new QLabel(tr("请输入命令:"));
inputEdit = new QLineEdit;
runButton = new QPushButton(tr("运行"));
outputLabel = new QLabel(tr("运行结果如下:"));
outputEdit = new QTextEdit;
outputEdit->setReadOnly(true);
QHBoxLayout* hlayout = new QHBoxLayout;
hlayout->addWidget(inputEdit);
hlayout->addWidget(runButton);
QVBoxLayout* layout = new QVBoxLayout;
layout->addWidget(inputLabel);
layout->addLayout(hlayout);
layout->addWidget(outputLabel);
layout->addWidget(outputEdit);
setLayout(layout);
cmd = new QProcess;
connect(inputEdit, SIGNAL(returnPressed()), this, SLOT(runClicked()));
connect(runButton, SIGNAL(clicked(bool)), this, SLOT(runClicked()));
connect(cmd, SIGNAL(readyRead()), this, SLOT(readOutput()));
resize(500, 300);
}
Process::~Process()
{
}
void Process::runClicked()
{
QString input = inputEdit->text();
//输入指令
cmd->start(input);
output = tr("");
outputEdit->setText(output);
}
void Process::readOutput()
{
output += cmd->readAll();
outputEdit->setText(output);
}
转载于:https://my.oschina.net/hemiya/blog/275128
推荐阅读
-
SQL Server中调用C#类中的方法实例(使用.NET程序集)
-
Qt程序中调用C#编写的dll(推荐)
-
Angular外部使用js调用Angular控制器中的函数方法或变量用法示例
-
SQL Server中调用C#类中的方法实例(使用.NET程序集)
-
浅析C# 使用Process调用外部程序中所遇到的参数问题
-
Angular外部使用js调用Angular控制器中的函数方法或变量用法示例
-
Qt程序中调用C#编写的dll(推荐)
-
Qt工作笔记-使用Qt中QProcess与iostream中system调用外部进程
-
QT中调用外部程序:QProcess的使用
-
简单介绍C#使用Process调用外部程序中所遇到的参数问题的代码示例