Skip to content

Commit

Permalink
Add Qt5 backwards compatibility for UI generation (#303)
Browse files Browse the repository at this point in the history
Attempts to fully support PyQt5 backwards compatibility based on feedback from @PrentissLiu
  • Loading branch information
landonepps authored Aug 17, 2023
1 parent 206ff99 commit 0c21a90
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
7 changes: 6 additions & 1 deletion morph/readability_settings_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@
# run again. Do not edit this file unless you know what you are doing.


from PyQt6 import QtCore, QtWidgets
try:
from PyQt6 import QtCore, QtGui, QtWidgets
except:
from PyQt5 import QtCore, QtGui, QtWidgets
QtCore.Qt.AlignmentFlag.AlignLeading = QtCore.Qt.AlignLeading
QtCore.Qt.AlignmentFlag.AlignTrailing = QtCore.Qt.AlignTrailing


class Ui_ReadabilitySettingsDialog(object):
Expand Down
7 changes: 6 additions & 1 deletion morph/readability_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@
# run again. Do not edit this file unless you know what you are doing.


from PyQt6 import QtCore, QtGui, QtWidgets
try:
from PyQt6 import QtCore, QtGui, QtWidgets
except:
from PyQt5 import QtCore, QtGui, QtWidgets
QtCore.Qt.AlignmentFlag.AlignLeading = QtCore.Qt.AlignLeading
QtCore.Qt.AlignmentFlag.AlignTrailing = QtCore.Qt.AlignTrailing


class Ui_ReadabilityDialog(object):
Expand Down
21 changes: 14 additions & 7 deletions scripts/build_ui.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
import subprocess


def build_ui(in_file, out_file):
stdout = subprocess.run(["pyuic6", in_file], stdout=subprocess.PIPE).stdout

lines = stdout.decode("utf-8").replace("__relpath__", "")

with open(out_file, "w") as sources:
sources.write(lines)
stdout = subprocess.Popen(["pyuic6", in_file], stdout=subprocess.PIPE, text=True).stdout

with open(out_file, 'w') as sources:
for line in iter(stdout.readline, ""):
if line.startswith("from PyQt6 import"):
imports = line.split("import")[1].strip()
sources.write(f"""try:
from PyQt6 import {imports}
except:
from PyQt5 import {imports}
QtCore.Qt.AlignmentFlag.AlignLeading = QtCore.Qt.AlignLeading
QtCore.Qt.AlignmentFlag.AlignTrailing = QtCore.Qt.AlignTrailing
""")
else:
sources.write(line)

build_ui("morph/readability.ui", "morph/readability_ui.py")
build_ui("morph/readability_settings.ui", "morph/readability_settings_ui.py")

0 comments on commit 0c21a90

Please sign in to comment.