最新要闻

广告

手机

iphone11大小尺寸是多少?苹果iPhone11和iPhone13的区别是什么?

iphone11大小尺寸是多少?苹果iPhone11和iPhone13的区别是什么?

警方通报辅警执法直播中被撞飞:犯罪嫌疑人已投案

警方通报辅警执法直播中被撞飞:犯罪嫌疑人已投案

家电

世界观天下!项目实战:Qt终端命令模拟工具 v1.0.0(实时获取命令行输出,执行指令,模拟ctrl+c中止操作)

来源:博客园


【资料图】

需求

在Qt软件中实现部分终端控制命令行功能,使软件内可以又好的模拟终端控制,提升软件整体契合度。

Demo演示

运行包下载地址:

CSDNf粉丝0积分下载:https://download.csdn.net/download/qq21497936/87418829  QQ群:博客首页扫码进入技术群,点击“文件”搜索“terminator”,群内与博文同步更新)

功能描述 v1.0.0

  • windows版本打开即可实时现实;
  • centOS打开抓不到默认的输出只能抓到命令执行的输出结果;
  • ubuntu等其他linux-arm没有测试;
  • 可以执行单条指令;
  • 可以清空模拟ctrl+c结束正在操作的命令;

项目模块化部署

源码

TerminatorWidget.h

#ifndef TERMINATORWIDGET_H#define TERMINATORWIDGET_H#include #include #include #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 #include #include 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 #include //#define LOG qDebug()<<__FILE__<<__LINE__//#define LOG qDebug()<<__FILE__<<__LINE__<<__FUNCTION__//#define LOG qDebug()<<__FILE__<<__LINE__<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();}

工程模板

关键词: 终端控制 功能描述 下载地址