@@ -3418,9 +3418,10 @@ The worker thread is implemented using Qt's ``QThread`` class rather than the
3418
3418
:mod: `threading ` module, as there are circumstances where one has to use
3419
3419
``QThread ``, which offers better integration with other ``Qt `` components.
3420
3420
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.
3424
3425
3425
3426
.. code-block :: python3
3426
3427
@@ -3430,16 +3431,25 @@ refer to the comments in the code snippet for more detailed information.
3430
3431
import sys
3431
3432
import time
3432
3433
3433
- # Deal with minor differences between PySide2 and PyQt5
3434
+ # Deal with minor differences between different Qt packages
3434
3435
try:
3435
- from PySide2 import QtCore, QtGui, QtWidgets
3436
+ from PySide6 import QtCore, QtGui, QtWidgets
3436
3437
Signal = QtCore.Signal
3437
3438
Slot = QtCore.Slot
3438
3439
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
3443
3453
3444
3454
logger = logging.getLogger(__name__)
3445
3455
@@ -3511,8 +3521,14 @@ refer to the comments in the code snippet for more detailed information.
3511
3521
while not QtCore.QThread.currentThread().isInterruptionRequested():
3512
3522
delay = 0.5 + random.random() * 2
3513
3523
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)
3516
3532
i += 1
3517
3533
3518
3534
#
@@ -3539,7 +3555,10 @@ refer to the comments in the code snippet for more detailed information.
3539
3555
self.textedit = te = QtWidgets.QPlainTextEdit(self)
3540
3556
# Set whatever the default monospace font is for the platform
3541
3557
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
3543
3562
te.setFont(f)
3544
3563
te.setReadOnly(True)
3545
3564
PB = QtWidgets.QPushButton
@@ -3626,7 +3645,11 @@ refer to the comments in the code snippet for more detailed information.
3626
3645
app = QtWidgets.QApplication(sys.argv)
3627
3646
example = Window(app)
3628
3647
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)
3630
3653
3631
3654
if __name__=='__main__':
3632
3655
main()
0 commit comments