Skip to content

Commit c6e729b

Browse files
Make splash screen widget compatible with PySide6
1 parent 5e3fb28 commit c6e729b

File tree

1 file changed

+22
-38
lines changed

1 file changed

+22
-38
lines changed

labscript_utils/splash.py

+22-38
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,28 @@
1515
from labscript_utils import dedent
1616

1717
try:
18-
from qtutils.qt import QtWidgets, QtCore, QtGui
18+
from qtutils.qt import QtWidgets, QtCore, QtGui, QT_ENV
1919
except ImportError as e:
2020
if 'DLL load failed' in str(e):
2121
msg = """Failed to load Qt DLL. This can be caused by application shortcuts
2222
not being configured to activate conda environments. Try running the
2323
following from within the activated conda environment to fix the shortcuts:
2424
25-
python -m labscript_utils.winshell --fix-shortcuts."""
25+
desktop-app install blacs lyse runmanager runviewer"""
2626
raise ImportError(dedent(msg))
2727
raise
2828

2929
Qt = QtCore.Qt
3030

31-
32-
# Set auto high-DPI scaling - this ensures pixel metrics are scaled
33-
# appropriately so that we don't get a weird mix of large fonts and small
34-
# everything else on High DPI displays:
35-
QtWidgets.QApplication.setAttribute(Qt.AA_EnableHighDpiScaling, True)
36-
# Use high res pixmaps if available, instead of rendering at low resolution and
37-
# upscaling:
38-
QtWidgets.QApplication.setAttribute(Qt.AA_UseHighDpiPixmaps, True)
31+
# These are default in Qt6 and print a warning if set
32+
if QT_ENV == 'PyQt5':
33+
# Set auto high-DPI scaling - this ensures pixel metrics are scaled
34+
# appropriately so that we don't get a weird mix of large fonts and small
35+
# everything else on High DPI displays:
36+
QtWidgets.QApplication.setAttribute(Qt.AA_EnableHighDpiScaling, True)
37+
# Use high res pixmaps if available, instead of rendering at low resolution and
38+
# upscaling:
39+
QtWidgets.QApplication.setAttribute(Qt.AA_UseHighDpiPixmaps, True)
3940

4041

4142
class Splash(QtWidgets.QFrame):
@@ -46,12 +47,13 @@ class Splash(QtWidgets.QFrame):
4647
alpha = 0.875
4748
icon_frac = 0.65
4849
BG = '#ffffff'
50+
FG = '#000000'
4951

5052
def __init__(self, imagepath):
5153
self.qapplication = QtWidgets.QApplication.instance()
5254
if self.qapplication is None:
5355
self.qapplication = QtWidgets.QApplication(sys.argv)
54-
QtWidgets.QFrame.__init__(self)
56+
super().__init__()
5557
self.icon = QtGui.QPixmap()
5658
self.icon.load(imagepath)
5759
if self.icon.isNull():
@@ -63,7 +65,7 @@ def __init__(self, imagepath):
6365
self.setWindowFlags(Qt.SplashScreen)
6466
self.setWindowOpacity(self.alpha)
6567
self.label = QtWidgets.QLabel(self.text)
66-
self.setStyleSheet("background-color: %s; font-size: 10pt" % self.BG)
68+
self.setStyleSheet(f"color: {self.FG}; background-color: {self.BG}; font-size: 10pt")
6769
# Frame not necessary on macos, and looks ugly.
6870
if sys.platform != 'darwin':
6971
self.setFrameShape(QtWidgets.QFrame.StyledPanel)
@@ -79,17 +81,11 @@ def __init__(self, imagepath):
7981
layout.addWidget(image_label)
8082
layout.addWidget(self.label)
8183

82-
center_point = QtWidgets.QDesktopWidget().availableGeometry().center()
83-
x0, y0 = center_point.x(), center_point.y()
84-
self.move(x0 - self.w // 2, y0 - self.h // 2)
85-
self._first_paint_complete = False
84+
self._paint_pending = False
8685

8786
def paintEvent(self, event):
88-
result = QtWidgets.QFrame.paintEvent(self, event)
89-
if not self._first_paint_complete:
90-
self._first_paint_complete = True
91-
self.qapplication.quit()
92-
return result
87+
self._paint_pending = False
88+
return super().paintEvent(event)
9389

9490
def show(self):
9591
QtWidgets.QFrame.show(self)
@@ -98,27 +94,15 @@ def show(self):
9894
def update_text(self, text):
9995
self.text = text
10096
self.label.setText(text)
101-
# If we are not visible yet, exec until we are painted.
102-
if not self._first_paint_complete:
103-
self.qapplication.exec_()
104-
else:
105-
self.repaint()
97+
self._paint_pending = True
98+
while self._paint_pending:
99+
QtCore.QCoreApplication.processEvents(QtCore.QEventLoop.AllEvents)
100+
QtCore.QCoreApplication.sendPostedEvents()
106101

107102

108103
if __name__ == '__main__':
109104
import time
110-
111-
MACOS = sys.platform == 'darwin'
112-
WINDOWS = sys.platform == 'win32'
113-
LINUX = sys.platform.startswith('linux')
114-
115-
if MACOS:
116-
icon = '/Users/bilbo/tmp/runmanager/runmanager.svg'
117-
elif LINUX:
118-
icon = '/home/bilbo/labscript_suite/runmanager/runmanager.svg'
119-
elif WINDOWS:
120-
icon = R'C:\labscript_suite\runmanager\runmanager.svg'
121-
105+
icon = '../../runmanager/runmanager/runmanager.svg'
122106
splash = Splash(icon)
123107
splash.show()
124108
time.sleep(1)

0 commit comments

Comments
 (0)