Skip to content

Commit 43a0f97

Browse files
committed
Compatibility with Qt 6
1 parent 835c369 commit 43a0f97

File tree

2 files changed

+30
-15
lines changed

2 files changed

+30
-15
lines changed

QCodeEditor.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,15 @@
2020
modified to be heavily dependent on the BN theme system.
2121
'''
2222

23-
from PySide2.QtCore import Qt, QRect, QRegExp
24-
from PySide2.QtWidgets import QWidget, QTextEdit, QPlainTextEdit
25-
from PySide2.QtGui import (QPainter, QFont, QSyntaxHighlighter, QTextFormat, QTextCharFormat)
23+
import binaryninjaui
24+
if "qt_major_version" in binaryninjaui.__dict__ and binaryninjaui.qt_major_version == 6:
25+
from PySide6.QtCore import Qt, QRect, QRegularExpression
26+
from PySide6.QtWidgets import QWidget, QTextEdit, QPlainTextEdit
27+
from PySide6.QtGui import (QPainter, QFont, QSyntaxHighlighter, QTextFormat, QTextCharFormat)
28+
else:
29+
from PySide2.QtCore import Qt, QRect, QRegularExpression
30+
from PySide2.QtWidgets import QWidget, QTextEdit, QPlainTextEdit
31+
from PySide2.QtGui import (QPainter, QFont, QSyntaxHighlighter, QTextFormat, QTextCharFormat)
2632
from binaryninjaui import (getMonospaceFont, getThemeColor, ThemeColor)
2733

2834

@@ -90,8 +96,8 @@ def __init__(self, document):
9096
# Multi-line strings (expression, flag, style)
9197
# FIXME: The triple-quotes in these two lines will mess up the
9298
# syntax highlighting from this point onward
93-
self.tri_single = (QRegExp("'''"), 1, STYLES['string2'])
94-
self.tri_double = (QRegExp('"""'), 2, STYLES['string2'])
99+
self.tri_single = (QRegularExpression("'''"), 1, STYLES['string2'])
100+
self.tri_double = (QRegularExpression('"""'), 2, STYLES['string2'])
95101

96102
rules = []
97103

@@ -128,7 +134,7 @@ def __init__(self, document):
128134
]
129135

130136
# Build a QRegExp for each pattern
131-
self.rules = [(QRegExp(pat), index, fmt)
137+
self.rules = [(QRegularExpression(pat), index, fmt)
132138
for (pat, index, fmt) in rules]
133139

134140

@@ -257,7 +263,7 @@ def paintEvent(self, event):
257263

258264
def getWidth(self):
259265
count = self.editor.blockCount()
260-
width = self.fontMetrics().width(str(count)) + 10
266+
width = self.fontMetrics().horizontalAdvance(str(count)) + 10
261267
return width
262268

263269
def updateWidth(self):

__init__.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,26 @@
44
import os
55
import re
66
import codecs
7-
from PySide2.QtWidgets import (QLineEdit, QPushButton, QApplication, QTextEdit, QWidget,
8-
QVBoxLayout, QHBoxLayout, QDialog, QFileSystemModel, QTreeView, QLabel, QSplitter,
9-
QInputDialog, QMessageBox, QHeaderView, QMenu, QAction, QKeySequenceEdit,
10-
QPlainTextEdit)
11-
from PySide2.QtCore import (QDir, QObject, Qt, QFileInfo, QItemSelectionModel, QSettings, QUrl)
12-
from PySide2.QtGui import (QFont, QFontMetrics, QDesktopServices, QKeySequence, QIcon)
7+
import binaryninjaui
8+
from binaryninjaui import (getMonospaceFont, UIAction, UIActionHandler, Menu, DockHandler)
9+
if "qt_major_version" in binaryninjaui.__dict__ and binaryninjaui.qt_major_version == 6:
10+
from PySide6.QtWidgets import (QLineEdit, QPushButton, QApplication, QTextEdit, QWidget,
11+
QVBoxLayout, QHBoxLayout, QDialog, QFileSystemModel, QTreeView, QLabel, QSplitter,
12+
QInputDialog, QMessageBox, QHeaderView, QMenu, QKeySequenceEdit,
13+
QPlainTextEdit)
14+
from PySide6.QtCore import (QDir, QObject, Qt, QFileInfo, QItemSelectionModel, QSettings, QUrl)
15+
from PySide6.QtGui import (QAction, QFont, QFontMetrics, QDesktopServices, QKeySequence, QIcon)
16+
else:
17+
from PySide2.QtWidgets import (QLineEdit, QPushButton, QApplication, QTextEdit, QWidget,
18+
QVBoxLayout, QHBoxLayout, QDialog, QFileSystemModel, QTreeView, QLabel, QSplitter,
19+
QInputDialog, QMessageBox, QHeaderView, QMenu, QKeySequenceEdit, QAction,
20+
QPlainTextEdit)
21+
from PySide2.QtCore import (QDir, QObject, Qt, QFileInfo, QItemSelectionModel, QSettings, QUrl)
22+
from PySide2.QtGui import (QFont, QFontMetrics, QDesktopServices, QKeySequence, QIcon)
1323
from binaryninja import user_plugin_path
1424
from binaryninja.plugin import PluginCommand, MainThreadActionHandler
1525
from binaryninja.mainthread import execute_on_main_thread
1626
from binaryninja.log import (log_error, log_debug)
17-
from binaryninjaui import (getMonospaceFont, UIAction, UIActionHandler, Menu, DockHandler)
1827
import numbers
1928
from .QCodeEditor import QCodeEditor, PythonHighlighter
2029

@@ -164,7 +173,7 @@ def __init__(self, context, parent=None):
164173
font = getMonospaceFont(self)
165174
self.edit.setFont(font)
166175
font = QFontMetrics(font)
167-
self.edit.setTabStopWidth(4 * font.width(' ')) #TODO, replace with settings API
176+
self.edit.setTabStopDistance(4 * font.horizontalAdvance(' ')) #TODO, replace with settings API
168177

169178
#Files
170179
self.files = QFileSystemModel()

0 commit comments

Comments
 (0)