qt-C++笔记之模拟实现一个linux终端窗口
code review!
文章目录
- qt-C++笔记之模拟实现一个linux终端窗口
- 一.运行
- 二.main.cpp
- 三.不足,待改进点
一.运行
二.main.cpp
代码
#include <QApplication>
#include <QPlainTextEdit>
#include <QLineEdit>
#include <QVBoxLayout>
#include <QKeyEvent>
#include <QProcess>
#include <QScrollBar>
#include <QPushButton>class TerminalWidget : public QWidget
{// Q_OBJECTpublic:TerminalWidget(QWidget *parent = nullptr): QWidget(parent){// 创建终端输出控件outputTextEdit = new QPlainTextEdit(this);outputTextEdit->setReadOnly(true);outputTextEdit->setStyleSheet("background-color: black; color: white;");// 创建用户输入控件inputLineEdit = new QLineEdit(this);inputLineEdit->setStyleSheet("background-color: black; color: white; border: 1px solid white;");// 创建清空按钮clearButton = new QPushButton("Clear Terminal", this);clearButton->setStyleSheet("color: white;"); // 设置按钮上的文本颜色为白色connect(clearButton, &QPushButton::clicked, this, &TerminalWidget::clearTerminal);// 连接输入控件的信号到槽函数connect(inputLineEdit, &QLineEdit::returnPressed, this, &TerminalWidget::processCommand);// 创建垂直布局并添加控件QVBoxLayout *layout = new QVBoxLayout(this);layout->addWidget(outputTextEdit);layout->addWidget(inputLineEdit);layout->addWidget(clearButton);// 设置布局setLayout(layout);// 设置窗口背景颜色setStyleSheet("background-color: black;");// 设置窗口标题setWindowTitle("Terminal");// 设置窗口大小resize(800, 600);// 设置焦点到输入框inputLineEdit->setFocus();}protected:void keyPressEvent(QKeyEvent *event) override{if (event->key() == Qt::Key_Up){// 处理上箭头键,浏览历史命令if (historyIndex > 0 && historyIndex <= commandHistory.size()){historyIndex--;inputLineEdit->setText(commandHistory.at(historyIndex));inputLineEdit->selectAll();}}else if (event->key() == Qt::Key_Down){// 处理下箭头键,浏览历史命令if (historyIndex >= 0 && historyIndex < commandHistory.size() - 1){historyIndex++;inputLineEdit->setText(commandHistory.at(historyIndex));inputLineEdit->selectAll();}else if (historyIndex == commandHistory.size() - 1){historyIndex = commandHistory.size();inputLineEdit->clear();}}else{QWidget::keyPressEvent(event);}}private slots:void processCommand(){// 获取用户输入的命令QString command = inputLineEdit->text();// 清空输入框inputLineEdit->clear();// 执行命令并将输出写入终端QProcess process;process.start(command);process.waitForFinished();QString output = process.readAll();// 添加命令到历史记录commandHistory.append(command);historyIndex = commandHistory.size();// 显示命令和输出outputTextEdit->appendPlainText("$ " + command);outputTextEdit->appendPlainText(output);// 将滚动条滚动到底部,以显示最新的输出outputTextEdit->verticalScrollBar()->setValue(outputTextEdit->verticalScrollBar()->maximum());// 设置焦点到输入框inputLineEdit->setFocus();}void clearTerminal(){// 清空输入框和输出窗口inputLineEdit->clear();outputTextEdit->clear();// 设置焦点到输入框inputLineEdit->setFocus();}private:QPlainTextEdit *outputTextEdit;QLineEdit *inputLineEdit;QPushButton *clearButton;QStringList commandHistory;int historyIndex = 0;
};int main(int argc, char *argv[])
{QApplication app(argc, argv);TerminalWidget terminal;terminal.show();return app.exec();
}
三.不足,待改进点
- 1.无法呈现top这种命令的效果
- 2.无法通过clear这样的命令清空终端显示,只能按钮清空
- 3.无法呈现ping www.baidu.com这种命令的效果,与第1点相同
若有什么解决方案可私信我,一起交流学习呀!