mirror of
https://github.com/crystalidea/qt6windows7.git
synced 2025-07-02 07:15:27 +08:00
qt 6.5.1 original
This commit is contained in:
38
examples/widgets/richtext/syntaxhighlighter/CMakeLists.txt
Normal file
38
examples/widgets/richtext/syntaxhighlighter/CMakeLists.txt
Normal file
@ -0,0 +1,38 @@
|
||||
# Copyright (C) 2022 The Qt Company Ltd.
|
||||
# SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
cmake_minimum_required(VERSION 3.16)
|
||||
project(syntaxhighlighter LANGUAGES CXX)
|
||||
|
||||
if(NOT DEFINED INSTALL_EXAMPLESDIR)
|
||||
set(INSTALL_EXAMPLESDIR "examples")
|
||||
endif()
|
||||
|
||||
set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/widgets/richtext/syntaxhighlighter")
|
||||
|
||||
find_package(Qt6 REQUIRED COMPONENTS Core Gui Widgets)
|
||||
|
||||
qt_standard_project_setup()
|
||||
|
||||
qt_add_executable(syntaxhighlighter
|
||||
highlighter.cpp highlighter.h
|
||||
main.cpp
|
||||
mainwindow.cpp mainwindow.h
|
||||
)
|
||||
|
||||
set_target_properties(syntaxhighlighter PROPERTIES
|
||||
WIN32_EXECUTABLE TRUE
|
||||
MACOSX_BUNDLE TRUE
|
||||
)
|
||||
|
||||
target_link_libraries(syntaxhighlighter PRIVATE
|
||||
Qt6::Core
|
||||
Qt6::Gui
|
||||
Qt6::Widgets
|
||||
)
|
||||
|
||||
install(TARGETS syntaxhighlighter
|
||||
RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}"
|
||||
BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}"
|
||||
LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}"
|
||||
)
|
108
examples/widgets/richtext/syntaxhighlighter/highlighter.cpp
Normal file
108
examples/widgets/richtext/syntaxhighlighter/highlighter.cpp
Normal file
@ -0,0 +1,108 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#include "highlighter.h"
|
||||
|
||||
//! [0]
|
||||
Highlighter::Highlighter(QTextDocument *parent)
|
||||
: QSyntaxHighlighter(parent)
|
||||
{
|
||||
HighlightingRule rule;
|
||||
|
||||
keywordFormat.setForeground(Qt::darkBlue);
|
||||
keywordFormat.setFontWeight(QFont::Bold);
|
||||
const QString keywordPatterns[] = {
|
||||
QStringLiteral("\\bchar\\b"), QStringLiteral("\\bclass\\b"), QStringLiteral("\\bconst\\b"),
|
||||
QStringLiteral("\\bdouble\\b"), QStringLiteral("\\benum\\b"), QStringLiteral("\\bexplicit\\b"),
|
||||
QStringLiteral("\\bfriend\\b"), QStringLiteral("\\binline\\b"), QStringLiteral("\\bint\\b"),
|
||||
QStringLiteral("\\blong\\b"), QStringLiteral("\\bnamespace\\b"), QStringLiteral("\\boperator\\b"),
|
||||
QStringLiteral("\\bprivate\\b"), QStringLiteral("\\bprotected\\b"), QStringLiteral("\\bpublic\\b"),
|
||||
QStringLiteral("\\bshort\\b"), QStringLiteral("\\bsignals\\b"), QStringLiteral("\\bsigned\\b"),
|
||||
QStringLiteral("\\bslots\\b"), QStringLiteral("\\bstatic\\b"), QStringLiteral("\\bstruct\\b"),
|
||||
QStringLiteral("\\btemplate\\b"), QStringLiteral("\\btypedef\\b"), QStringLiteral("\\btypename\\b"),
|
||||
QStringLiteral("\\bunion\\b"), QStringLiteral("\\bunsigned\\b"), QStringLiteral("\\bvirtual\\b"),
|
||||
QStringLiteral("\\bvoid\\b"), QStringLiteral("\\bvolatile\\b"), QStringLiteral("\\bbool\\b")
|
||||
};
|
||||
for (const QString &pattern : keywordPatterns) {
|
||||
rule.pattern = QRegularExpression(pattern);
|
||||
rule.format = keywordFormat;
|
||||
highlightingRules.append(rule);
|
||||
//! [0] //! [1]
|
||||
}
|
||||
//! [1]
|
||||
|
||||
//! [2]
|
||||
classFormat.setFontWeight(QFont::Bold);
|
||||
classFormat.setForeground(Qt::darkMagenta);
|
||||
rule.pattern = QRegularExpression(QStringLiteral("\\bQ[A-Za-z]+\\b"));
|
||||
rule.format = classFormat;
|
||||
highlightingRules.append(rule);
|
||||
//! [2]
|
||||
|
||||
//! [3]
|
||||
singleLineCommentFormat.setForeground(Qt::red);
|
||||
rule.pattern = QRegularExpression(QStringLiteral("//[^\n]*"));
|
||||
rule.format = singleLineCommentFormat;
|
||||
highlightingRules.append(rule);
|
||||
|
||||
multiLineCommentFormat.setForeground(Qt::red);
|
||||
//! [3]
|
||||
|
||||
//! [4]
|
||||
quotationFormat.setForeground(Qt::darkGreen);
|
||||
rule.pattern = QRegularExpression(QStringLiteral("\".*\""));
|
||||
rule.format = quotationFormat;
|
||||
highlightingRules.append(rule);
|
||||
//! [4]
|
||||
|
||||
//! [5]
|
||||
functionFormat.setFontItalic(true);
|
||||
functionFormat.setForeground(Qt::blue);
|
||||
rule.pattern = QRegularExpression(QStringLiteral("\\b[A-Za-z0-9_]+(?=\\()"));
|
||||
rule.format = functionFormat;
|
||||
highlightingRules.append(rule);
|
||||
//! [5]
|
||||
|
||||
//! [6]
|
||||
commentStartExpression = QRegularExpression(QStringLiteral("/\\*"));
|
||||
commentEndExpression = QRegularExpression(QStringLiteral("\\*/"));
|
||||
}
|
||||
//! [6]
|
||||
|
||||
//! [7]
|
||||
void Highlighter::highlightBlock(const QString &text)
|
||||
{
|
||||
for (const HighlightingRule &rule : std::as_const(highlightingRules)) {
|
||||
QRegularExpressionMatchIterator matchIterator = rule.pattern.globalMatch(text);
|
||||
while (matchIterator.hasNext()) {
|
||||
QRegularExpressionMatch match = matchIterator.next();
|
||||
setFormat(match.capturedStart(), match.capturedLength(), rule.format);
|
||||
}
|
||||
}
|
||||
//! [7] //! [8]
|
||||
setCurrentBlockState(0);
|
||||
//! [8]
|
||||
|
||||
//! [9]
|
||||
int startIndex = 0;
|
||||
if (previousBlockState() != 1)
|
||||
startIndex = text.indexOf(commentStartExpression);
|
||||
|
||||
//! [9] //! [10]
|
||||
while (startIndex >= 0) {
|
||||
//! [10] //! [11]
|
||||
QRegularExpressionMatch match = commentEndExpression.match(text, startIndex);
|
||||
int endIndex = match.capturedStart();
|
||||
int commentLength = 0;
|
||||
if (endIndex == -1) {
|
||||
setCurrentBlockState(1);
|
||||
commentLength = text.length() - startIndex;
|
||||
} else {
|
||||
commentLength = endIndex - startIndex
|
||||
+ match.capturedLength();
|
||||
}
|
||||
setFormat(startIndex, commentLength, multiLineCommentFormat);
|
||||
startIndex = text.indexOf(commentStartExpression, startIndex + commentLength);
|
||||
}
|
||||
}
|
||||
//! [11]
|
46
examples/widgets/richtext/syntaxhighlighter/highlighter.h
Normal file
46
examples/widgets/richtext/syntaxhighlighter/highlighter.h
Normal file
@ -0,0 +1,46 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#ifndef HIGHLIGHTER_H
|
||||
#define HIGHLIGHTER_H
|
||||
|
||||
#include <QSyntaxHighlighter>
|
||||
#include <QTextCharFormat>
|
||||
#include <QRegularExpression>
|
||||
|
||||
QT_BEGIN_NAMESPACE
|
||||
class QTextDocument;
|
||||
QT_END_NAMESPACE
|
||||
|
||||
//! [0]
|
||||
class Highlighter : public QSyntaxHighlighter
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
Highlighter(QTextDocument *parent = nullptr);
|
||||
|
||||
protected:
|
||||
void highlightBlock(const QString &text) override;
|
||||
|
||||
private:
|
||||
struct HighlightingRule
|
||||
{
|
||||
QRegularExpression pattern;
|
||||
QTextCharFormat format;
|
||||
};
|
||||
QList<HighlightingRule> highlightingRules;
|
||||
|
||||
QRegularExpression commentStartExpression;
|
||||
QRegularExpression commentEndExpression;
|
||||
|
||||
QTextCharFormat keywordFormat;
|
||||
QTextCharFormat classFormat;
|
||||
QTextCharFormat singleLineCommentFormat;
|
||||
QTextCharFormat multiLineCommentFormat;
|
||||
QTextCharFormat quotationFormat;
|
||||
QTextCharFormat functionFormat;
|
||||
};
|
||||
//! [0]
|
||||
|
||||
#endif // HIGHLIGHTER_H
|
15
examples/widgets/richtext/syntaxhighlighter/main.cpp
Normal file
15
examples/widgets/richtext/syntaxhighlighter/main.cpp
Normal file
@ -0,0 +1,15 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#include "mainwindow.h"
|
||||
|
||||
#include <QApplication>
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
QApplication app(argc, argv);
|
||||
MainWindow window;
|
||||
window.resize(640, 512);
|
||||
window.show();
|
||||
return app.exec();
|
||||
}
|
88
examples/widgets/richtext/syntaxhighlighter/mainwindow.cpp
Normal file
88
examples/widgets/richtext/syntaxhighlighter/mainwindow.cpp
Normal file
@ -0,0 +1,88 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#include <QtWidgets>
|
||||
|
||||
#include "mainwindow.h"
|
||||
|
||||
//! [0]
|
||||
MainWindow::MainWindow(QWidget *parent)
|
||||
: QMainWindow(parent)
|
||||
{
|
||||
setupFileMenu();
|
||||
setupHelpMenu();
|
||||
setupEditor();
|
||||
|
||||
setCentralWidget(editor);
|
||||
setWindowTitle(tr("Syntax Highlighter"));
|
||||
}
|
||||
//! [0]
|
||||
|
||||
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 QTextEdit;
|
||||
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"), QKeySequence::New,
|
||||
this, &MainWindow::newFile);
|
||||
fileMenu->addAction(tr("&Open..."), QKeySequence::Open,
|
||||
this, [this](){ openFile(); });
|
||||
fileMenu->addAction(tr("E&xit"), QKeySequence::Quit,
|
||||
qApp, &QApplication::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);
|
||||
}
|
38
examples/widgets/richtext/syntaxhighlighter/mainwindow.h
Normal file
38
examples/widgets/richtext/syntaxhighlighter/mainwindow.h
Normal file
@ -0,0 +1,38 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#ifndef MAINWINDOW_H
|
||||
#define MAINWINDOW_H
|
||||
|
||||
#include "highlighter.h"
|
||||
|
||||
#include <QMainWindow>
|
||||
|
||||
QT_BEGIN_NAMESPACE
|
||||
class QTextEdit;
|
||||
QT_END_NAMESPACE
|
||||
|
||||
//! [0]
|
||||
class MainWindow : public QMainWindow
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
MainWindow(QWidget *parent = nullptr);
|
||||
|
||||
public slots:
|
||||
void about();
|
||||
void newFile();
|
||||
void openFile(const QString &path = QString());
|
||||
|
||||
private:
|
||||
void setupEditor();
|
||||
void setupFileMenu();
|
||||
void setupHelpMenu();
|
||||
|
||||
QTextEdit *editor;
|
||||
Highlighter *highlighter;
|
||||
};
|
||||
//! [0]
|
||||
|
||||
#endif // MAINWINDOW_H
|
@ -0,0 +1,12 @@
|
||||
QT += widgets
|
||||
requires(qtConfig(filedialog))
|
||||
|
||||
HEADERS = highlighter.h \
|
||||
mainwindow.h
|
||||
SOURCES = highlighter.cpp \
|
||||
mainwindow.cpp \
|
||||
main.cpp
|
||||
|
||||
# install
|
||||
target.path = $$[QT_INSTALL_EXAMPLES]/widgets/richtext/syntaxhighlighter
|
||||
INSTALLS += target
|
Reference in New Issue
Block a user