欢迎来到天天文库
浏览记录
ID:41035809
大小:115.50 KB
页数:9页
时间:2019-08-14
《QT中调用外部程序_QProcess的使用》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、QT中调用外部程序:QProcess的使用本文通过QProcess,实现了在Qt中调用外部程序(控制台程序),并将输出重定向到了一个QTextEdit里。效果如下图所示:运行界面如下:程序共包含以下有一个文件:viewplaincopytoclipboardprint?//:main.cpp#include"process.h"#include#includeintmain(intargc,char*argv[]){QApplicationa(argc,argv);QTextCodec::setCodecForTr(
2、QTextCodec::codecForLocale());QTextCodec::setCodecForCStrings(QTextCodec::codecForLocale());Processw;w.show();returna.exec();}//:main.cpp#include"process.h"#include#includeintmain(intargc,char*argv[]){QApplicationa(argc,argv);QTextCodec::setCodecForTr(QTextCode
3、c::codecForLocale());QTextCodec::setCodecForCStrings(QTextCodec::codecForLocale());Processw;w.show();returna.exec();}viewplaincopytoclipboardprint?//:process.h#ifndefPROCESS_H#definePROCESS_H#includeclassQLabel;classQLineEdit;classQProcess;classQPushButton;classQTextEdit;classP
4、rocess:publicQWidget{Q_OBJECTpublic:Process(QWidget*parent=0,Qt::WFlagsflags=0);~Process();privateslots:voidrunClicked();voidreadOutput();private:QProcess*cmd;QStringoutput;QLabel*inputLabel;QLineEdit*inputEdit;QPushButton*runButton;QLabel*outputLabel;QTextEdit*outputEdit;};#endif//PROC
5、ESS_H//:process.h#ifndefPROCESS_H#definePROCESS_H#includeclassQLabel;classQLineEdit;classQProcess;classQPushButton;classQTextEdit;classProcess:publicQWidget{Q_OBJECTpublic:Process(QWidget*parent=0,Qt::WFlagsflags=0);~Process();privateslots:voidrunClicked();voidreadOutput();priv
6、ate:QProcess*cmd;QStringoutput;QLabel*inputLabel;QLineEdit*inputEdit;QPushButton*runButton;QLabel*outputLabel;QTextEdit*outputEdit;};#endif//PROCESS_Hviewplaincopytoclipboardprint?//:process.cpp#include#include"process.h"Process::Process(QWidget*parent,Qt::WFlagsflags):QWidget(pa
7、rent,flags){inputLabel=newQLabel(tr("请输入命令:"));inputEdit=newQLineEdit;runButton=newQPushButton(tr("运行"));outputLabel=newQLabel(tr("运行结果如下:"));outputEdit=newQTextEdit;outputEdit->setReadOnly(true);QHBoxLayout*hlayout=newQHBoxLayout;hlayout->addWidget(inputEdit);hlayout->addWidge
此文档下载收益归作者所有