Skip to content

Commit 186af3c

Browse files
authored
[doc]: Update cookbook recipe for Qt6. (GH-116719)
1 parent 612f1ec commit 186af3c

File tree

1 file changed

+36
-13
lines changed

1 file changed

+36
-13
lines changed

Doc/howto/logging-cookbook.rst

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3418,9 +3418,10 @@ The worker thread is implemented using Qt's ``QThread`` class rather than the
34183418
:mod:`threading` module, as there are circumstances where one has to use
34193419
``QThread``, which offers better integration with other ``Qt`` components.
34203420

3421-
The code should work with recent releases of either ``PySide2`` or ``PyQt5``.
3422-
You should be able to adapt the approach to earlier versions of Qt. Please
3423-
refer to the comments in the code snippet for more detailed information.
3421+
The code should work with recent releases of either ``PySide6``, ``PyQt6``,
3422+
``PySide2`` or ``PyQt5``. You should be able to adapt the approach to earlier
3423+
versions of Qt. Please refer to the comments in the code snippet for more
3424+
detailed information.
34243425

34253426
.. code-block:: python3
34263427
@@ -3430,16 +3431,25 @@ refer to the comments in the code snippet for more detailed information.
34303431
import sys
34313432
import time
34323433
3433-
# Deal with minor differences between PySide2 and PyQt5
3434+
# Deal with minor differences between different Qt packages
34343435
try:
3435-
from PySide2 import QtCore, QtGui, QtWidgets
3436+
from PySide6 import QtCore, QtGui, QtWidgets
34363437
Signal = QtCore.Signal
34373438
Slot = QtCore.Slot
34383439
except ImportError:
3439-
from PyQt5 import QtCore, QtGui, QtWidgets
3440-
Signal = QtCore.pyqtSignal
3441-
Slot = QtCore.pyqtSlot
3442-
3440+
try:
3441+
from PyQt6 import QtCore, QtGui, QtWidgets
3442+
Signal = QtCore.pyqtSignal
3443+
Slot = QtCore.pyqtSlot
3444+
except ImportError:
3445+
try:
3446+
from PySide2 import QtCore, QtGui, QtWidgets
3447+
Signal = QtCore.Signal
3448+
Slot = QtCore.Slot
3449+
except ImportError:
3450+
from PyQt5 import QtCore, QtGui, QtWidgets
3451+
Signal = QtCore.pyqtSignal
3452+
Slot = QtCore.pyqtSlot
34433453
34443454
logger = logging.getLogger(__name__)
34453455
@@ -3511,8 +3521,14 @@ refer to the comments in the code snippet for more detailed information.
35113521
while not QtCore.QThread.currentThread().isInterruptionRequested():
35123522
delay = 0.5 + random.random() * 2
35133523
time.sleep(delay)
3514-
level = random.choice(LEVELS)
3515-
logger.log(level, 'Message after delay of %3.1f: %d', delay, i, extra=extra)
3524+
try:
3525+
if random.random() < 0.1:
3526+
raise ValueError('Exception raised: %d' % i)
3527+
else:
3528+
level = random.choice(LEVELS)
3529+
logger.log(level, 'Message after delay of %3.1f: %d', delay, i, extra=extra)
3530+
except ValueError as e:
3531+
logger.exception('Failed: %s', e, extra=extra)
35163532
i += 1
35173533
35183534
#
@@ -3539,7 +3555,10 @@ refer to the comments in the code snippet for more detailed information.
35393555
self.textedit = te = QtWidgets.QPlainTextEdit(self)
35403556
# Set whatever the default monospace font is for the platform
35413557
f = QtGui.QFont('nosuchfont')
3542-
f.setStyleHint(f.Monospace)
3558+
if hasattr(f, 'Monospace'):
3559+
f.setStyleHint(f.Monospace)
3560+
else:
3561+
f.setStyleHint(f.StyleHint.Monospace) # for Qt6
35433562
te.setFont(f)
35443563
te.setReadOnly(True)
35453564
PB = QtWidgets.QPushButton
@@ -3626,7 +3645,11 @@ refer to the comments in the code snippet for more detailed information.
36263645
app = QtWidgets.QApplication(sys.argv)
36273646
example = Window(app)
36283647
example.show()
3629-
sys.exit(app.exec_())
3648+
if hasattr(app, 'exec'):
3649+
rc = app.exec()
3650+
else:
3651+
rc = app.exec_()
3652+
sys.exit(rc)
36303653
36313654
if __name__=='__main__':
36323655
main()

0 commit comments

Comments
 (0)