syntaxhighlighter_rc.py¶
# Resource object code (Python 3)
# Created by: object code
# Created by: The Resource Compiler for Qt version 5.14.0
# WARNING! All changes made in this file will be lost!
from PySide6 import QtCore
qt_resource_data = b"\
\x00\x00\x06{\
T\
EMPLATE = app\x0aLA\
NGUAGE = C++\x0aTAR\
GET = as\
sistant\x0a\x0aCONFIG \
+= qt war\
n_on\x0aQT \
+= xml networ\
k\x0a\x0aPROJECTNAME \
= Assistan\
t\x0aDESTDIR \
= ../../bin\
\x0a\x0aFORMS += findd\
ialog.ui \x5c\x0a \
helpdialog.ui\
\x5c\x0a mainw\
indow.ui \x5c\x0a \
settingsdialo\
g.ui \x5c\x0a t\
abbedbrowser.ui \
\x5c\x0a topicc\
hooser.ui\x0a\x0aSOURC\
ES += main.cpp \x5c\
\x0a helpwin\
dow.cpp \x5c\x0a \
topicchooser.c\
pp \x5c\x0a doc\
uparser.cpp \x5c\x0a \
settingsdi\
alog.cpp \x5c\x0a \
index.cpp \x5c\x0a \
profile.c\
pp \x5c\x0a con\
fig.cpp \x5c\x0a \
finddialog.cpp\
\x5c\x0a helpd\
ialog.cpp \x5c\x0a \
mainwindow.c\
pp \x5c\x0a tab\
bedbrowser.cpp\x0a\x0a\
HEADERS +\
= helpwindow.h \x5c\
\x0a topicch\
ooser.h \x5c\x0a \
docuparser.h \x5c\
\x0a setting\
sdialog.h \x5c\x0a \
index.h \x5c\x0a \
profile.h \
\x5c\x0a finddi\
alog.h \x5c\x0a \
helpdialog.h \x5c\x0a\
mainwind\
ow.h \x5c\x0a t\
abbedbrowser.h \x5c\
\x0a config.\
h\x0a\x0aRESOURCES += \
assistant.qrc\x0a\x0aD\
EFINES += QT_KEY\
WORDS\x0a#DEFINES +\
= QT_PALMTOPCEN\
TER_DOCS\x0a!networ\
k:DEFINES \
+= QT_INTERNAL_\
NETWORK\x0aelse:QT \
+= network\x0a!xml:\
DEFINES \
+= QT_IN\
TERNAL_XML\x0aelse:\
QT += xml\x0ainclud\
e( ../../src/qt_\
professional.pri\
)\x0a\x0awin32 {\x0a \
LIBS += -lshell3\
2\x0a RC_FILE = \
assistant.rc\x0a}\x0a\x0a\
macos {\x0a ICON\
= assistant.icn\
s\x0a TARGET = a\
ssistant\x0a# QM\
AKE_INFO_PLIST =\
Info_mac.plist\x0a\
}\x0a\x0a#target.path \
= $$[QT_INSTALL_\
BINS]\x0a#INSTALLS \
+= target\x0a\x0a#assi\
stanttranslation\
s.files = *.qm\x0a#\
assistanttransla\
tions.path = $$[\
QT_INSTALL_TRANS\
LATIONS]\x0a#INSTAL\
LS += assistantt\
ranslations\x0a\x0aTRA\
NSLATIONS \
= assistant_de.\
ts \x5c\x0a \
assistant\
_fr.ts\x0a\x0a\x0aunix:!c\
ontains(QT_CONFI\
G, zlib):LIBS +=\
-lz\x0a\x0a\x0atarget.pa\
th=$$[QT_INSTALL\
_BINS]\x0aINSTALLS \
+= target\x0a\
"
qt_resource_name = b"\
\x00\x08\
\x0e\x84\x7fC\
\x00e\
\x00x\x00a\x00m\x00p\x00l\x00e\x00s\
\x00\x07\
\x0c\xe8G\xe5\
\x00e\
\x00x\x00a\x00m\x00p\x00l\x00e\
"
qt_resource_struct = b"\
\x00\x00\x00\x00\x00\x02\x00\x00\x00\x01\x00\x00\x00\x01\
\x00\x00\x00\x00\x00\x00\x00\x00\
\x00\x00\x00\x00\x00\x02\x00\x00\x00\x01\x00\x00\x00\x02\
\x00\x00\x00\x00\x00\x00\x00\x00\
\x00\x00\x00\x16\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\
\x00\x00\x01e\xaf\x16\xd2\xa1\
"
def qInitResources():
QtCore.qRegisterResourceData(0x03, qt_resource_struct, qt_resource_name, qt_resource_data)
def qCleanupResources():
QtCore.qUnregisterResourceData(0x03, qt_resource_struct, qt_resource_name, qt_resource_data)
qInitResources()
syntaxhighlighter.py¶
"""PySide6 port of the widgets/richtext/syntaxhighlighter example from Qt v5.x"""
import sys
import re
from PySide6.QtCore import (QFile, Qt, QTextStream)
from PySide6.QtGui import (QColor, QFont, QKeySequence, QSyntaxHighlighter,
QTextCharFormat)
from PySide6.QtWidgets import (QApplication, QFileDialog, QMainWindow,
QPlainTextEdit)
import syntaxhighlighter_rc
class MainWindow(QMainWindow):
def __init__(self, parent=None):
QMainWindow.__init__(self, parent)
self._highlighter = Highlighter()
self.setup_file_menu()
self.setup_editor()
self.setCentralWidget(self._editor)
self.setWindowTitle(self.tr("Syntax Highlighter"))
def new_file(self):
self._editor.clear()
def open_file(self, path=""):
file_name = path
if not file_name:
file_name, _ = QFileDialog.getOpenFileName(self, self.tr("Open File"), "",
"qmake Files (*.pro *.prf *.pri)")
if file_name:
inFile = QFile(file_name)
if inFile.open(QFile.ReadOnly | QFile.Text):
stream = QTextStream(inFile)
self._editor.setPlainText(stream.readAll())
def setup_editor(self):
variable_format = QTextCharFormat()
variable_format.setFontWeight(QFont.Bold)
variable_format.setForeground(Qt.blue)
self._highlighter.add_mapping("\\b[A-Z_]+\\b", variable_format)
single_line_comment_format = QTextCharFormat()
single_line_comment_format.setBackground(QColor("#77ff77"))
self._highlighter.add_mapping("#[^\n]*", single_line_comment_format)
quotation_format = QTextCharFormat()
quotation_format.setBackground(Qt.cyan)
quotation_format.setForeground(Qt.blue)
self._highlighter.add_mapping("\".*\"", quotation_format)
function_format = QTextCharFormat()
function_format.setFontItalic(True)
function_format.setForeground(Qt.blue)
self._highlighter.add_mapping("\\b[a-z0-9_]+\\(.*\\)", function_format)
font = QFont()
font.setFamily("Courier")
font.setFixedPitch(True)
font.setPointSize(10)
self._editor = QPlainTextEdit()
self._editor.setFont(font)
self._highlighter.setDocument(self._editor.document())
def setup_file_menu(self):
file_menu = self.menuBar().addMenu(self.tr("&File"))
new_file_act = file_menu.addAction(self.tr("&New..."))
new_file_act.setShortcut(QKeySequence(QKeySequence.New))
new_file_act.triggered.connect(self.new_file)
open_file_act = file_menu.addAction(self.tr("&Open..."))
open_file_act.setShortcut(QKeySequence(QKeySequence.Open))
open_file_act.triggered.connect(self.open_file)
quit_act = file_menu.addAction(self.tr("E&xit"))
quit_act.setShortcut(QKeySequence(QKeySequence.Quit))
quit_act.triggered.connect(self.close)
help_menu = self.menuBar().addMenu("&Help")
help_menu.addAction("About &Qt", qApp.aboutQt)
class Highlighter(QSyntaxHighlighter):
def __init__(self, parent=None):
QSyntaxHighlighter.__init__(self, parent)
self._mappings = {}
def add_mapping(self, pattern, format):
self._mappings[pattern] = format
def highlightBlock(self, text):
for pattern, format in self._mappings.items():
for match in re.finditer(pattern, text):
start, end = match.span()
self.setFormat(start, end - start, format)
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MainWindow()
window.resize(640, 512)
window.show()
window.open_file(":/examples/example")
sys.exit(app.exec_())
syntaxhighlighter.qrc¶
<!DOCTYPE RCC><RCC version="1.0">
<qresource prefix="/" >
<file>examples/example</file>
</qresource>
</RCC>
© 2021 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.