專案實戰: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
- Linux c程式中獲取shell指令碼輸出(如獲取system命令輸出)LinuxC程式指令碼
- 實戰:Mailivery 模擬登入AI
- 終端模擬器:Serial for MacMac
- Putty for Mac(終端模擬器)Mac
- shell指令碼命令 執行python檔案&python命令列執行python程式碼指令碼Python命令列
- Go實現ssh執行遠端命令及遠端終端Go
- 三國英雄對戰——控制檯輸出模擬
- mumu模擬器 adb shell 登入後執行命令 Inappropriate ioctl for deviceAPPdev
- Git常用命令列終端指令Git命令列
- 命令啟動android studio 模擬器Android
- 命令列執行python時找不到模組怎麼解決命令列Python
- [NOIP 2024 模擬2]陣列操作陣列
- modelsim指令碼模擬設計(三)帶標頭檔案模擬指令碼
- mac電腦上命令列啟動安卓模擬器Mac命令列安卓
- 前端實時搜尋框模擬前端
- PHP執行耗時指令碼實時輸出內容PHP指令碼
- 實戰模擬│JWT 登入認證JWT
- web模擬終端部落格系統Web
- Web 模擬終端部落格系統Web
- 終端模擬器ZOC7 for MacMac
- python實現批次執行命令列Python命令列
- Linux curl 命令模擬 POST/GET 請求Linux
- 模擬銀行排隊時間—4佇列佇列
- ADAMS 指令碼模擬指令碼
- ansible 命令列模命令列
- JavaScript專題之模擬實現bindJavaScript
- JavaScript專題之模擬實現newJavaScript
- Windos bat批處理指令碼,判斷是終端命令列執行,還是雙擊執行BAT指令碼命令列
- 終端SSH工具:SecureCRT MacOS 多種網路協議終端模擬軟體SecurecrtMac協議
- SecureCRT for Mac(終端模擬器/SSH客戶端)SecurecrtMac客戶端
- 智駕模擬測試實戰之自動泊車HiL模擬測試
- 實戰模擬│單點登入 SSO 的實現
- Git 沙盒模擬實戰(基礎篇)Git
- C語言實戰!!!:商城系統模擬C語言
- ZOC for Mac(最好用的終端模擬器)Mac