mirror of
https://github.com/python/cpython
synced 2024-09-15 23:36:24 +00:00
[doc]: Update cookbook recipe for Qt6. (GH-116719)
This commit is contained in:
parent
612f1ec988
commit
186af3cf21
|
@ -3418,9 +3418,10 @@ The worker thread is implemented using Qt's ``QThread`` class rather than the
|
||||||
:mod:`threading` module, as there are circumstances where one has to use
|
:mod:`threading` module, as there are circumstances where one has to use
|
||||||
``QThread``, which offers better integration with other ``Qt`` components.
|
``QThread``, which offers better integration with other ``Qt`` components.
|
||||||
|
|
||||||
The code should work with recent releases of either ``PySide2`` or ``PyQt5``.
|
The code should work with recent releases of either ``PySide6``, ``PyQt6``,
|
||||||
You should be able to adapt the approach to earlier versions of Qt. Please
|
``PySide2`` or ``PyQt5``. You should be able to adapt the approach to earlier
|
||||||
refer to the comments in the code snippet for more detailed information.
|
versions of Qt. Please refer to the comments in the code snippet for more
|
||||||
|
detailed information.
|
||||||
|
|
||||||
.. code-block:: python3
|
.. code-block:: python3
|
||||||
|
|
||||||
|
@ -3430,16 +3431,25 @@ refer to the comments in the code snippet for more detailed information.
|
||||||
import sys
|
import sys
|
||||||
import time
|
import time
|
||||||
|
|
||||||
# Deal with minor differences between PySide2 and PyQt5
|
# Deal with minor differences between different Qt packages
|
||||||
try:
|
try:
|
||||||
from PySide2 import QtCore, QtGui, QtWidgets
|
from PySide6 import QtCore, QtGui, QtWidgets
|
||||||
Signal = QtCore.Signal
|
Signal = QtCore.Signal
|
||||||
Slot = QtCore.Slot
|
Slot = QtCore.Slot
|
||||||
except ImportError:
|
except ImportError:
|
||||||
from PyQt5 import QtCore, QtGui, QtWidgets
|
try:
|
||||||
Signal = QtCore.pyqtSignal
|
from PyQt6 import QtCore, QtGui, QtWidgets
|
||||||
Slot = QtCore.pyqtSlot
|
Signal = QtCore.pyqtSignal
|
||||||
|
Slot = QtCore.pyqtSlot
|
||||||
|
except ImportError:
|
||||||
|
try:
|
||||||
|
from PySide2 import QtCore, QtGui, QtWidgets
|
||||||
|
Signal = QtCore.Signal
|
||||||
|
Slot = QtCore.Slot
|
||||||
|
except ImportError:
|
||||||
|
from PyQt5 import QtCore, QtGui, QtWidgets
|
||||||
|
Signal = QtCore.pyqtSignal
|
||||||
|
Slot = QtCore.pyqtSlot
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
@ -3511,8 +3521,14 @@ refer to the comments in the code snippet for more detailed information.
|
||||||
while not QtCore.QThread.currentThread().isInterruptionRequested():
|
while not QtCore.QThread.currentThread().isInterruptionRequested():
|
||||||
delay = 0.5 + random.random() * 2
|
delay = 0.5 + random.random() * 2
|
||||||
time.sleep(delay)
|
time.sleep(delay)
|
||||||
level = random.choice(LEVELS)
|
try:
|
||||||
logger.log(level, 'Message after delay of %3.1f: %d', delay, i, extra=extra)
|
if random.random() < 0.1:
|
||||||
|
raise ValueError('Exception raised: %d' % i)
|
||||||
|
else:
|
||||||
|
level = random.choice(LEVELS)
|
||||||
|
logger.log(level, 'Message after delay of %3.1f: %d', delay, i, extra=extra)
|
||||||
|
except ValueError as e:
|
||||||
|
logger.exception('Failed: %s', e, extra=extra)
|
||||||
i += 1
|
i += 1
|
||||||
|
|
||||||
#
|
#
|
||||||
|
@ -3539,7 +3555,10 @@ refer to the comments in the code snippet for more detailed information.
|
||||||
self.textedit = te = QtWidgets.QPlainTextEdit(self)
|
self.textedit = te = QtWidgets.QPlainTextEdit(self)
|
||||||
# Set whatever the default monospace font is for the platform
|
# Set whatever the default monospace font is for the platform
|
||||||
f = QtGui.QFont('nosuchfont')
|
f = QtGui.QFont('nosuchfont')
|
||||||
f.setStyleHint(f.Monospace)
|
if hasattr(f, 'Monospace'):
|
||||||
|
f.setStyleHint(f.Monospace)
|
||||||
|
else:
|
||||||
|
f.setStyleHint(f.StyleHint.Monospace) # for Qt6
|
||||||
te.setFont(f)
|
te.setFont(f)
|
||||||
te.setReadOnly(True)
|
te.setReadOnly(True)
|
||||||
PB = QtWidgets.QPushButton
|
PB = QtWidgets.QPushButton
|
||||||
|
@ -3626,7 +3645,11 @@ refer to the comments in the code snippet for more detailed information.
|
||||||
app = QtWidgets.QApplication(sys.argv)
|
app = QtWidgets.QApplication(sys.argv)
|
||||||
example = Window(app)
|
example = Window(app)
|
||||||
example.show()
|
example.show()
|
||||||
sys.exit(app.exec_())
|
if hasattr(app, 'exec'):
|
||||||
|
rc = app.exec()
|
||||||
|
else:
|
||||||
|
rc = app.exec_()
|
||||||
|
sys.exit(rc)
|
||||||
|
|
||||||
if __name__=='__main__':
|
if __name__=='__main__':
|
||||||
main()
|
main()
|
||||||
|
|
Loading…
Reference in a new issue