用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的差别之处吧.
如下所示,在命令框中输入多条命令,然后执行该连续命令,就可看到正确的执行结果: