專案實戰:Qt終端命令模擬工具 v1.0.0(實時獲取命令列輸出,執行指令,模擬操作)
需求
Demo演示
功能描述 v1.0.0
專案模組化部署
原始碼
TerminatorWidget.h
#ifndef TERMINATORWIDGET_H#define TERMINATORWIDGET_H#include <QWidget>#include <QThread>#include <QTextCursor>#include "TerminatorManager.h"namespace Ui {class TerminatorWidget;}class TerminatorWidget : public QWidget{ Q_OBJECTpublic: explicit TerminatorWidget(QWidget *parent = 0); ~TerminatorWidget();protected: void initControl();protected slots: void slot_recvData(QByteArray byteArray); void slot_recvDataError(QByteArray byteArray);private slots: void on_pushButton_exec_clicked(); void on_pushButton_stop_clicked(); void on_pushButton_clear_clicked();private: Ui::TerminatorWidget *ui;private: QThread *_pTerminatorManagerThread; TerminatorManager * _pTerminatorManager;};#endif // TERMINATORWIDGET_H
TerminatorManager.h
#ifndef TERMINATORMANAGER_H#define TERMINATORMANAGER_H#include <QObject>#include <QProcess>#include <QTextCodec>class TerminatorManager : public QObject{ Q_OBJECTpublic: explicit TerminatorManager(QObject *parent = 0);public: bool getRunning() const; void wirte(QByteArray byteArray); void stop(); void ctrlC();signals: void signal_recvData(QByteArray byteArray); void signal_recvDataError(QByteArray byteArray);public slots: void slot_start(); void slot_stop();protected: void initControl();protected slots: void slot_stateChanged(QProcess::ProcessState newState); void slot_readyReadStandardOutput(); void slot_readyReadStandardError(); void slot_write(QByteArray byteArray); void slot_ctrlC();private: bool _running; // 是否執行 QProcess *_pProcess; // 程式 QTextCodec *_pTextCodec; // 編碼,讀取資料轉化編碼時用};#endif // TERMINATORMANAGER_H
TerminatorManager.cpp
#include "TerminatorManager.h"#include <QDebug>#include <QDateTime>//#define LOG qDebug()<<__FILE__<<__LINE__//#define LOG qDebug()<<__FILE__<<__LINE__<<__FUNCTION__//#define LOG qDebug()<<__FILE__<<__LINE__<<QThread()::currentThread()//#define LOG qDebug()<<__FILE__<<__LINE__<<QDateTime::currentDateTime().toString("yyyy-MM-dd")#define LOG qDebug()<<__FILE__<<__LINE__<<QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss:zzz")TerminatorManager::TerminatorManager(QObject *parent) : QObject(parent), _running(false), _pProcess(0), _pTextCodec(0){ initControl();}bool TerminatorManager::getRunning() const{ return _running;}void TerminatorManager::wirte(QByteArray byteArray){ QMetaObject::invokeMethod(this, "slot_write", Q_ARG(QByteArray, byteArray));}void TerminatorManager::ctrlC(){ QMetaObject::invokeMethod(this, "slot_ctrlC");}void TerminatorManager::slot_start(){ if(_running) { LOG << "It's already running!!!"; return; } // 初始化子執行緒中的相關類 if(!_pProcess) { _pProcess = new QProcess(); connect(_pProcess, SIGNAL(stateChanged(QProcess::ProcessState)), this, SLOT(slot_stateChanged(QProcess::ProcessState))); connect(_pProcess, SIGNAL(readyReadStandardOutput()), this, SLOT(slot_readyReadStandardOutput())); connect(_pProcess, SIGNAL(readyReadStandardError()), this, SLOT(slot_readyReadStandardError()));#ifndef LINUX _pProcess->start("cmd"); _pProcess->waitForStarted();#else// _pProcess->start("bash"); _pProcess->start("sh"); _pProcess->waitForStarted();#endif } _running = true;}void TerminatorManager::slot_stop(){ if(!_running) { LOG << "It's not running!!!"; return; } if(_pProcess) { _pProcess->kill(); _pProcess->close(); _pProcess->waitForFinished(); _pProcess->deleteLater(); _pProcess = 0; } _running = false;}void TerminatorManager::initControl(){ // linux下有可能獲取失敗 _pTextCodec = QTextCodec::codecForName("System");}void TerminatorManager::slot_stateChanged(QProcess::ProcessState newState){ LOG << newState;}void TerminatorManager::slot_readyReadStandardOutput(){ QByteArray byteArray = _pProcess->readAllStandardOutput();// QByteArray byteArray = _pProcess->readAll(); QString str; // 轉換為unicode if(_pTextCodec) { str = _pTextCodec->toUnicode(byteArray); }else{ str = QString(byteArray); } emit signal_recvData(str.toUtf8());}void TerminatorManager::slot_readyReadStandardError(){ QByteArray byteArray = _pProcess->readAllStandardError(); QString str; // 轉換為unicode if(_pTextCodec) { str = _pTextCodec->toUnicode(byteArray); }else{ str = QString(byteArray); } emit signal_recvDataError(str.toUtf8());}void TerminatorManager::slot_write(QByteArray byteArray){ if(!_pProcess) { LOG << "Is't not running"; return; } LOG << QString(byteArray); LOG << _pProcess; _pProcess->write(byteArray); LOG;}void TerminatorManager::slot_ctrlC(){ if(!_running) { LOG << "It's not running!!!"; return; } slot_stop(); slot_start();}
工程模板
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/70010283/viewspace-2934035/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- SecureCRT 9.3.2 終端模擬SSH工具Securecrt
- 實時檢視模擬器的日誌輸出
- ZOC for Mac:最 佳終端模擬器,提升您的Mac命令列體驗Mac命令列
- 好用的終端SSH模擬工具:SecureCRT for macSecurecrtMac
- Thinking in Java---多執行緒模擬:銀行出納員模擬+飯店模擬+汽車裝配工廠模擬ThinkingJava執行緒
- Putty for Mac(終端模擬器)Mac
- 終端模擬器:Serial for MacMac
- js模擬實現列舉效果JS
- 實戰:Mailivery 模擬登入AI
- Undistract-me:當長時間執行的終端命令完成時獲取通知
- 前端實時搜尋框模擬前端
- mac電腦上命令列啟動安卓模擬器Mac命令列安卓
- 終端模擬器ZOC7 for MacMac
- Web 模擬終端部落格系統Web
- web模擬終端部落格系統Web
- 實戰模擬│JWT 登入認證JWT
- adb shell命令--命令建立模擬器並啟動,命令
- SecureCRT for Mac(終端模擬器/SSH客戶端)SecurecrtMac客戶端
- Linux c程式中獲取shell指令碼輸出(如獲取system命令輸出)LinuxC程式指令碼
- 模擬銀行排隊時間—4佇列佇列
- shell模擬“多執行緒”執行緒
- TortoiseSVN 命令 (命令列執行工具)命令列
- modelsim指令碼模擬設計(三)帶標頭檔案模擬指令碼
- 智駕模擬測試實戰之自動泊車HiL模擬測試
- Go實現ssh執行遠端命令及遠端終端Go
- 終端SSH工具:SecureCRT MacOS 多種網路協議終端模擬軟體SecurecrtMac協議
- JavaScript專題之模擬實現newJavaScript
- JavaScript專題之模擬實現bindJavaScript
- Linux使用time命令獲取命令執行時間Linux
- ZOC for Mac(最好用的終端模擬器)Mac
- 實戰模擬監控MySQL服務shell指令碼小結薦MySql指令碼
- 模擬linux下的ls -l命令Linux
- C語言實戰!!!:商城系統模擬C語言
- Git 沙盒模擬實戰(基礎篇)Git
- 實戰模擬│單點登入 SSO 的實現
- Git常用命令列終端指令Git命令列
- java:模擬棧操作Java
- promise的模擬實現Promise