Qt获取Linux终端命令数据

用Qt新建一个无窗体的工程后,用代码的方式创建控件和布局,左边的编辑框为Linux命令,右边的为命令的执行结果,经验证,与终端执行结果一样,代码如下:

1. mainwindow.h 文件代码:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QLabel>
#include <QPlainTextEdit>
#include <QPushButton>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QGridLayout>
#include <QSplitter>

class MainWindow : public QMainWindow
{
    Q_OBJECT
    
public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
    
private slots:
    void onCommandClicked();
    void onClearClicked();
    void onExitClicked();
    
private:
    QWidget*            m_pMainWidget;
    QLabel*             m_pLblCommand;
    QPlainTextEdit*     m_pEdtCommand;
    QLabel*             m_pLblResult;
    QPlainTextEdit*     m_pEdtResult;
    QPushButton*        m_pBtnCommand;
    QPushButton*        m_pBtnClear;
    QPushButton*        m_pBtnExit;
    
    QVBoxLayout*        m_pVLayCommand;
    QVBoxLayout*        m_pVLayResult;
    QHBoxLayout*        m_pHLayButton;
    QGridLayout*        m_pGridMain;
};
#endif // MAINWINDOW_H

2. mainwindow.cpp 文件代码:

#include "mainwindow.h"
#include <cassert>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    m_pMainWidget = new QWidget(this);
    this->setCentralWidget(m_pMainWidget);
    
    QFont currFont;
    currFont.setFamily(tr("宋体"));
    currFont.setPointSize(20);
    
    QPalette pal;
    pal.setColor(QPalette::ColorRole::WindowText, Qt::blue);
    
    m_pLblCommand = new QLabel(tr("命令:"), m_pMainWidget);
    m_pLblCommand->setFont(currFont);
    
    m_pEdtCommand = new QPlainTextEdit(m_pMainWidget);
    m_pEdtCommand->setFont(currFont);
    m_pEdtCommand->setPalette(pal);
    
    m_pLblResult = new QLabel(tr("执行结果:"), m_pMainWidget);
    m_pLblResult->setFont(currFont);
    
    m_pEdtResult = new QPlainTextEdit(m_pMainWidget);
    currFont.setPointSize(15);
    m_pEdtResult->setFont(currFont);
    m_pEdtResult->setPalette(pal);
    m_pEdtResult->setReadOnly(true);
    currFont.setPointSize(20);
    
    m_pBtnCommand = new QPushButton(tr("执行命令"), m_pMainWidget);
    m_pBtnCommand->setFont(currFont);
    
    m_pBtnClear = new QPushButton(tr("清空命令"), m_pMainWidget);
    m_pBtnClear->setFont(currFont);
    
    m_pBtnExit = new QPushButton(tr("退出"), m_pMainWidget);
    m_pBtnExit->setFont(currFont);
    
    m_pVLayCommand = new QVBoxLayout;
    m_pVLayCommand->addWidget(m_pLblCommand);
    m_pVLayCommand->addWidget(m_pEdtCommand);
    m_pVLayCommand->setAlignment(Qt::AlignLeft);
    
    m_pVLayResult = new QVBoxLayout;
    m_pVLayResult->addWidget(m_pLblResult);
    m_pVLayResult->addWidget(m_pEdtResult);
    m_pVLayResult->setAlignment(Qt::AlignLeft);
    
    m_pHLayButton = new QHBoxLayout;
    m_pHLayButton->addWidget(m_pBtnCommand);
    m_pHLayButton->addWidget(m_pBtnClear);
    m_pHLayButton->addStretch();
    m_pHLayButton->addWidget(m_pBtnExit);
    
    m_pGridMain = new QGridLayout(m_pMainWidget);
    m_pGridMain->addLayout(m_pVLayCommand, 0, 0);
    m_pGridMain->addLayout(m_pVLayResult, 0, 1);
    m_pGridMain->addLayout(m_pHLayButton, 1, 0, 1, 2);
    
    m_pGridMain->setMargin(20);
    m_pGridMain->setSpacing(20);
    
    connect(m_pBtnCommand, SIGNAL(clicked()), SLOT(onCommandClicked()));
    connect(m_pBtnClear, SIGNAL(clicked()), SLOT(onClearClicked()));
    connect(m_pBtnExit, SIGNAL(clicked()), SLOT(onExitClicked()));
    
    m_pEdtCommand->setFocus();
    this->resize(900, 500);
}

MainWindow::~MainWindow()
{
}

void MainWindow::onCommandClicked()
{
    m_pEdtResult->clear();
    QString strCmd = m_pEdtCommand->toPlainText();
    FILE* fp = popen(strCmd.toLocal8Bit().data(), "r");
    
    if (nullptr != fp)
    {
        char buf[1024] = {0};
        char result[2000] = {0};
        
        while (fgets(buf, sizeof(buf), fp))
        {
            strcat(result, buf);
            if (strlen(result) > sizeof(buf))
            {
                break;
            }
        }
        
        m_pEdtResult->setPlainText(tr("%1").arg(result));
        pclose(fp);         // 记得释放资源
        fp = nullptr;
    }
}

void MainWindow::onClearClicked()
{
    m_pEdtCommand->clear();
    m_pEdtCommand->setFocus();
}

void MainWindow::onExitClicked()
{
    this->close();
}

3. 运行界面如下所示:

4. 如何用popen执行多条Linux命令?

(此处参考了:https://www.cnblogs.com/linwenbin/p/10943737.html )

原文如下:

(1) 命令被分号“;”分隔,这些命令会顺序执行下去;
(2) 命令被“&&”分隔,这些命令会顺序执行下去,遇到执行错误的命令停止;
(3) 命令被双竖线“||”分隔,这些命令会顺序执行下去,遇到执行成功的命令停止,后面的所有命令都将不会执行.

但在Linux中,连续的命令之间只有用";"和"&&"才有效,"||"是无效的,这是python和Linux的差别之处吧.

如下所示,在命令框中输入多条命令,然后执行该连续命令,就可看到正确的执行结果:

 

  • 3
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

宏笋

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值