QT中调用外部程序:QProcess的使用

运行:ipconfig

运行:ping www.baidu.com

程序共包含以下有一个文件:

  1. //: main.cpp   
  2. #include "process.h"   
  3. #include <QApplication>   
  4. #include <QTextCodec>   
  5. int main(int argc, char *argv[])   
  6. {   
  7.      QApplication a(argc, argv);   
  8.      QTextCodec::setCodecForTr(QTextCodec::codecForLocale());   
  9.      QTextCodec::setCodecForCStrings(QTextCodec::codecForLocale());   
  10.      Process w;   
  11.      w.show();   
  12.     return a.exec();   
  13. }  
  1. //: process.h   
  2. #ifndef PROCESS_H   
  3. #define PROCESS_H   
  4. #include <QWidget>   
  5. class QLabel;   
  6. class QLineEdit;   
  7. class QProcess;   
  8. class QPushButton;   
  9. class QTextEdit;   
  10. class Process : public QWidget   
  11. {   
  12.      Q_OBJECT   
  13. public:   
  14.      Process(QWidget *parent = 0, <b style="color:black;background-color:#ffff66">Qt</b>::WFlags flags = 0);   
  15.      ~Process();   
  16. private slots:   
  17.     void runClicked();   
  18.     void readOutput();   
  19. private:   
  20.      QProcess* cmd;   
  21.      QString output;       //用于保存输出结果
  22.      QLabel* inputLabel;   
  23.      QLineEdit* inputEdit;   
  24.      QPushButton* runButton;   
  25.      QLabel* outputLabel;   
  26.      QTextEdit* outputEdit;   
  27. };   
  28. #endif // PROCESS_H  

  1. //: process.cpp   
  2. #include <QtGui>   
  3. #include "process.h"   
  4. Process::Process(QWidget *parent, <b style="color:black;background-color:#ffff66">Qt</b>::WFlags flags)   
  5.      : QWidget(parent, flags)   
  6. {   
  7.      inputLabel = new QLabel(tr("请输入命令:"));   
  8.      inputEdit = new QLineEdit;   
  9.      runButton = new QPushButton(tr("运行"));   
  10.      outputLabel = new QLabel(tr("运行结果如下:"));   
  11.      outputEdit = new QTextEdit;   
  12.      outputEdit->setReadOnly(true);   
  13.      QHBoxLayout* hlayout = new QHBoxLayout;   
  14.      hlayout->addWidget(inputEdit);   
  15.      hlayout->addWidget(runButton);   
  16.      QVBoxLayout* layout = new QVBoxLayout;   
  17.      layout->addWidget(inputLabel);   
  18.      layout->addLayout(hlayout);   
  19.      layout->addWidget(outputLabel);   
  20.      layout->addWidget(outputEdit);   
  21.      setLayout(layout);   
  22.        
  23.      cmd = new QProcess;   
  24.      connect(inputEdit, SIGNAL(returnPressed()), this, SLOT(runClicked()));   
  25.      connect(runButton, SIGNAL(clicked(bool)), this, SLOT(runClicked()));   
  26.      connect(cmd, SIGNAL(readyRead()), this, SLOT(readOutput()));   
  27.      resize(500, 300);   
  28. }   
  29. Process::~Process()   
  30. {   
  31. }   
  32. void Process::runClicked()   
  33. {   
  34.      QString input = inputEdit->text();    //输入指令
  35.      cmd->start(input);   
  36.      output = tr("");   
  37.      outputEdit->setText(output);   
  38. }   
  39. void Process::readOutput()   
  40. {   
  41.      output += cmd->readAll();   
  42.      outputEdit->setText(output);   
  43. }  
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值