Ai
1 Star 12 Fork 6

unix/Qt-Project

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
mainwindow.cpp 2.24 KB
一键复制 编辑 原始数据 按行查看 历史
#include <QtWidgets>
#include "mainwindow.h"
//! [0]
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
setupFileMenu();
setupHelpMenu();
setupEditor();
setCentralWidget(editor);
setWindowTitle(tr("Syntax Highlighter"));
}
//! [0]
QSize MainWindow::sizeHint() const{
return QSize(800,600);
}
void MainWindow::about()
{
QMessageBox::about(this, tr("About Syntax Highlighter"),
tr("<p>The <b>Syntax Highlighter</b> example shows how " \
"to perform simple syntax highlighting by subclassing " \
"the QSyntaxHighlighter class and describing " \
"highlighting rules using regular expressions.</p>"));
}
void MainWindow::newFile()
{
editor->clear();
}
void MainWindow::openFile(const QString &path)
{
QString fileName = path;
if (fileName.isNull())
fileName = QFileDialog::getOpenFileName(this, tr("Open File"), "", "C++ Files (*.cpp *.h)");
if (!fileName.isEmpty()) {
QFile file(fileName);
if (file.open(QFile::ReadOnly | QFile::Text))
editor->setPlainText(file.readAll());
}
}
//! [1]
void MainWindow::setupEditor()
{
QFont font;
font.setFamily("Courier");
font.setFixedPitch(true);
font.setPointSize(10);
editor = new CodeEditor();
editor->setFont(font);
highlighter = new Highlighter(editor->document());
QFile file("mainwindow.h");
if (file.open(QFile::ReadOnly | QFile::Text))
editor->setPlainText(file.readAll());
}
//! [1]
void MainWindow::setupFileMenu()
{
QMenu *fileMenu = new QMenu(tr("&File"), this);
menuBar()->addMenu(fileMenu);
fileMenu->addAction(tr("&New"), this,
&MainWindow::newFile, QKeySequence::New);
fileMenu->addAction(tr("&Open..."),
this, [this](){ openFile(); }, QKeySequence::Open);
fileMenu->addAction(tr("E&xit"), qApp,
&QApplication::quit, QKeySequence::Quit);
}
void MainWindow::setupHelpMenu()
{
QMenu *helpMenu = new QMenu(tr("&Help"), this);
menuBar()->addMenu(helpMenu);
helpMenu->addAction(tr("&About"), this, &MainWindow::about);
helpMenu->addAction(tr("About &Qt"), qApp, &QApplication::aboutQt);
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/ax020913/qt-project.git
git@gitee.com:ax020913/qt-project.git
ax020913
qt-project
Qt-Project
master

搜索帮助