From b7d4e231a95605ddcae75f32fdc33c473a5b5b90 Mon Sep 17 00:00:00 2001 From: Friedemann Kleint Date: Fri, 23 Apr 2021 09:07:15 +0200 Subject: Remaining examples: Use per-class imports Task-number: PYSIDE-1112 Change-Id: I8534e911959d6eed2ed6d3f7741e99929ff0125e Reviewed-by: Christian Tismer --- .../animation/animatedtiles/animatedtiles.py | 123 ++++++----- .../widgets/animation/appchooser/appchooser.py | 68 +++--- examples/widgets/animation/states/states.py | 245 +++++++++++---------- examples/widgets/dialogs/extension/extension.py | 54 ++--- examples/widgets/dialogs/findfiles/findfiles.py | 77 ++++--- .../widgets/dialogs/trivialwizard/trivialwizard.py | 45 ++-- examples/widgets/effects/lighting.py | 63 +++--- .../widgets/layouts/basiclayouts/basiclayouts.py | 54 ++--- examples/widgets/richtext/orderform/orderform.py | 94 ++++---- examples/widgets/tutorials/addressbook/part1.py | 25 ++- examples/widgets/tutorials/addressbook/part2.py | 46 ++-- examples/widgets/tutorials/addressbook/part3.py | 50 +++-- examples/widgets/tutorials/addressbook/part4.py | 68 +++--- examples/widgets/tutorials/addressbook/part5.py | 89 ++++---- examples/widgets/tutorials/addressbook/part6.py | 101 ++++----- examples/widgets/tutorials/addressbook/part7.py | 115 +++++----- 16 files changed, 688 insertions(+), 629 deletions(-) (limited to 'examples/widgets') diff --git a/examples/widgets/animation/animatedtiles/animatedtiles.py b/examples/widgets/animation/animatedtiles/animatedtiles.py index 85ca55aa1..75458c002 100644 --- a/examples/widgets/animation/animatedtiles/animatedtiles.py +++ b/examples/widgets/animation/animatedtiles/animatedtiles.py @@ -40,19 +40,30 @@ ## ############################################################################# -from PySide6 import QtCore, QtGui, QtStateMachine, QtWidgets +import sys +import math + +from PySide6.QtCore import (QEasingCurve, QObject, QParallelAnimationGroup, + QPointF, QPropertyAnimation, QRandomGenerator, + QRectF, QTimer, Qt, Property, Signal) +from PySide6.QtGui import (QBrush, QColor, QLinearGradient, QPainter, + QPainterPath, QPixmap, QTransform) +from PySide6.QtWidgets import (QApplication, QGraphicsItem, QGraphicsPixmapItem, + QGraphicsRectItem, QGraphicsScene, QGraphicsView, + QGraphicsWidget, QStyle, QWidget) +from PySide6.QtStateMachine import QState, QStateMachine import animatedtiles_rc # Deriving from more than one wrapped class is not supported, so we use # composition and delegate the property. -class Pixmap(QtCore.QObject): +class Pixmap(QObject): def __init__(self, pix): super(Pixmap, self).__init__() - self.pixmap_item = QtWidgets.QGraphicsPixmapItem(pix) - self.pixmap_item.setCacheMode(QtWidgets.QGraphicsItem.DeviceCoordinateCache) + self.pixmap_item = QGraphicsPixmapItem(pix) + self.pixmap_item.setCacheMode(QGraphicsItem.DeviceCoordinateCache) def set_pos(self, pos): self.pixmap_item.setPos(pos) @@ -60,11 +71,11 @@ class Pixmap(QtCore.QObject): def get_pos(self): return self.pixmap_item.pos() - pos = QtCore.Property(QtCore.QPointF, get_pos, set_pos) + pos = Property(QPointF, get_pos, set_pos) -class Button(QtWidgets.QGraphicsWidget): - pressed = QtCore.Signal() +class Button(QGraphicsWidget): + pressed = Signal() def __init__(self, pixmap, parent=None): super(Button, self).__init__(parent) @@ -72,28 +83,28 @@ class Button(QtWidgets.QGraphicsWidget): self._pix = pixmap self.setAcceptHoverEvents(True) - self.setCacheMode(QtWidgets.QGraphicsItem.DeviceCoordinateCache) + self.setCacheMode(QGraphicsItem.DeviceCoordinateCache) def boundingRect(self): - return QtCore.QRectF(-65, -65, 130, 130) + return QRectF(-65, -65, 130, 130) def shape(self): - path = QtGui.QPainterPath() + path = QPainterPath() path.addEllipse(self.boundingRect()) return path def paint(self, painter, option, widget): - down = option.state & QtWidgets.QStyle.State_Sunken + down = option.state & QStyle.State_Sunken r = self.boundingRect() - grad = QtGui.QLinearGradient(r.topLeft(), r.bottomRight()) - if option.state & QtWidgets.QStyle.State_MouseOver: - color_0 = QtCore.Qt.white + grad = QLinearGradient(r.topLeft(), r.bottomRight()) + if option.state & QStyle.State_MouseOver: + color_0 = Qt.white else: - color_0 = QtCore.Qt.lightGray + color_0 = Qt.lightGray - color_1 = QtCore.Qt.darkGray + color_1 = Qt.darkGray if down: color_0, color_1 = color_1, color_0 @@ -101,12 +112,12 @@ class Button(QtWidgets.QGraphicsWidget): grad.setColorAt(0, color_0) grad.setColorAt(1, color_1) - painter.setPen(QtCore.Qt.darkGray) + painter.setPen(Qt.darkGray) painter.setBrush(grad) painter.drawEllipse(r) - color_0 = QtCore.Qt.darkGray - color_1 = QtCore.Qt.lightGray + color_0 = Qt.darkGray + color_1 = Qt.lightGray if down: color_0, color_1 = color_1, color_0 @@ -114,7 +125,7 @@ class Button(QtWidgets.QGraphicsWidget): grad.setColorAt(0, color_0) grad.setColorAt(1, color_1) - painter.setPen(QtCore.Qt.NoPen) + painter.setPen(Qt.NoPen) painter.setBrush(grad) if down: @@ -132,23 +143,19 @@ class Button(QtWidgets.QGraphicsWidget): self.update() -class View(QtWidgets.QGraphicsView): +class View(QGraphicsView): def resizeEvent(self, event): super(View, self).resizeEvent(event) - self.fitInView(self.sceneRect(), QtCore.Qt.KeepAspectRatio) + self.fitInView(self.sceneRect(), Qt.KeepAspectRatio) if __name__ == '__main__': + app = QApplication(sys.argv) - import sys - import math + kinetic_pix = QPixmap(':/images/kinetic.png') + bg_pix = QPixmap(':/images/Time-For-Lunch-2.jpg') - app = QtWidgets.QApplication(sys.argv) - - kinetic_pix = QtGui.QPixmap(':/images/kinetic.png') - bg_pix = QtGui.QPixmap(':/images/Time-For-Lunch-2.jpg') - - scene = QtWidgets.QGraphicsScene(-350, -350, 700, 700) + scene = QGraphicsScene(-350, -350, 700, 700) items = [] for i in range(64): @@ -160,12 +167,12 @@ if __name__ == '__main__': scene.addItem(item.pixmap_item) # Buttons. - button_parent = QtWidgets.QGraphicsRectItem() - ellipse_button = Button(QtGui.QPixmap(':/images/ellipse.png'), button_parent) - figure_8button = Button(QtGui.QPixmap(':/images/figure8.png'), button_parent) - random_button = Button(QtGui.QPixmap(':/images/random.png'), button_parent) - tiled_button = Button(QtGui.QPixmap(':/images/tile.png'), button_parent) - centered_button = Button(QtGui.QPixmap(':/images/centered.png'), button_parent) + button_parent = QGraphicsRectItem() + ellipse_button = Button(QPixmap(':/images/ellipse.png'), button_parent) + figure_8button = Button(QPixmap(':/images/figure8.png'), button_parent) + random_button = Button(QPixmap(':/images/random.png'), button_parent) + tiled_button = Button(QPixmap(':/images/tile.png'), button_parent) + centered_button = Button(QPixmap(':/images/centered.png'), button_parent) ellipse_button.setPos(-100, -100) figure_8button.setPos(100, -100) @@ -174,65 +181,65 @@ if __name__ == '__main__': centered_button.setPos(100, 100) scene.addItem(button_parent) - button_parent.setTransform(QtGui.QTransform().scale(0.75, 0.75)) + button_parent.setTransform(QTransform().scale(0.75, 0.75)) button_parent.setPos(200, 200) button_parent.setZValue(65) # States. - root_state = QtStateMachine.QState() - ellipse_state = QtStateMachine.QState(root_state) - figure_8state = QtStateMachine.QState(root_state) - random_state = QtStateMachine.QState(root_state) - tiled_state = QtStateMachine.QState(root_state) - centered_state = QtStateMachine.QState(root_state) + root_state = QState() + ellipse_state = QState(root_state) + figure_8state = QState(root_state) + random_state = QState(root_state) + tiled_state = QState(root_state) + centered_state = QState(root_state) # Values. - generator = QtCore.QRandomGenerator.global_() + generator = QRandomGenerator.global_() for i, item in enumerate(items): # Ellipse. ellipse_state.assignProperty(item, 'pos', - QtCore.QPointF(math.cos((i / 63.0) * 6.28) * 250, + QPointF(math.cos((i / 63.0) * 6.28) * 250, math.sin((i / 63.0) * 6.28) * 250)) # Figure 8. figure_8state.assignProperty(item, 'pos', - QtCore.QPointF(math.sin((i / 63.0) * 6.28) * 250, + QPointF(math.sin((i / 63.0) * 6.28) * 250, math.sin(((i * 2)/63.0) * 6.28) * 250)) # Random. random_state.assignProperty(item, 'pos', - QtCore.QPointF(-250 + generator.bounded(0, 500), + QPointF(-250 + generator.bounded(0, 500), -250 + generator.bounded(0, 500))) # Tiled. tiled_state.assignProperty(item, 'pos', - QtCore.QPointF(((i % 8) - 4) * kinetic_pix.width() + kinetic_pix.width() / 2, + QPointF(((i % 8) - 4) * kinetic_pix.width() + kinetic_pix.width() / 2, ((i // 8) - 4) * kinetic_pix.height() + kinetic_pix.height() / 2)) # Centered. - centered_state.assignProperty(item, 'pos', QtCore.QPointF()) + centered_state.assignProperty(item, 'pos', QPointF()) # Ui. view = View(scene) view.setWindowTitle("Animated Tiles") - view.setViewportUpdateMode(QtWidgets.QGraphicsView.BoundingRectViewportUpdate) - view.setBackgroundBrush(QtGui.QBrush(bg_pix)) - view.setCacheMode(QtWidgets.QGraphicsView.CacheBackground) + view.setViewportUpdateMode(QGraphicsView.BoundingRectViewportUpdate) + view.setBackgroundBrush(QBrush(bg_pix)) + view.setCacheMode(QGraphicsView.CacheBackground) view.setRenderHints( - QtGui.QPainter.Antialiasing | QtGui.QPainter.SmoothPixmapTransform) + QPainter.Antialiasing | QPainter.SmoothPixmapTransform) view.show() - states = QtStateMachine.QStateMachine() + states = QStateMachine() states.addState(root_state) states.setInitialState(root_state) root_state.setInitialState(centered_state) - group = QtCore.QParallelAnimationGroup() + group = QParallelAnimationGroup() for i, item in enumerate(items): - anim = QtCore.QPropertyAnimation(item, b'pos') + anim = QPropertyAnimation(item, b'pos') anim.setDuration(750 + i * 25) - anim.setEasingCurve(QtCore.QEasingCurve.InOutBack) + anim.setEasingCurve(QEasingCurve.InOutBack) group.addAnimation(anim) trans = root_state.addTransition(ellipse_button.pressed, ellipse_state) @@ -250,7 +257,7 @@ if __name__ == '__main__': trans = root_state.addTransition(centered_button.pressed, centered_state) trans.addAnimation(group) - timer = QtCore.QTimer() + timer = QTimer() timer.start(125) timer.setSingleShot(True) trans = root_state.addTransition(timer.timeout, ellipse_state) diff --git a/examples/widgets/animation/appchooser/appchooser.py b/examples/widgets/animation/appchooser/appchooser.py index 752edccf4..66903b6c5 100644 --- a/examples/widgets/animation/appchooser/appchooser.py +++ b/examples/widgets/animation/appchooser/appchooser.py @@ -40,22 +40,29 @@ ## ############################################################################# -from PySide6 import QtCore, QtGui, QtStateMachine, QtWidgets +import sys + +from PySide6.QtCore import (QPointF, QPropertyAnimation, QRect, QRectF, Qt, + Signal) +from PySide6.QtGui import QPixmap +from PySide6.QtWidgets import (QApplication, QGraphicsScene, QGraphicsView, + QGraphicsWidget, QWidget) +from PySide6.QtStateMachine import QState, QStateMachine import appchooser_rc -class Pixmap(QtWidgets.QGraphicsWidget): - clicked = QtCore.Signal() +class Pixmap(QGraphicsWidget): + clicked = Signal() def __init__(self, pix, parent=None): super(Pixmap, self).__init__(parent) - self.orig = QtGui.QPixmap(pix) - self.p = QtGui.QPixmap(pix) + self.orig = QPixmap(pix) + self.p = QPixmap(pix) def paint(self, painter, option, widget): - painter.drawPixmap(QtCore.QPointF(), self.p) + painter.drawPixmap(QPointF(), self.p) def mousePressEvent(self, ev): self.clicked.emit() @@ -66,58 +73,55 @@ class Pixmap(QtWidgets.QGraphicsWidget): if rect.size().width() > self.orig.size().width(): self.p = self.orig.scaled(rect.size().toSize()) else: - self.p = QtGui.QPixmap(self.orig) + self.p = QPixmap(self.orig) def create_states(objects, selectedRect, parent): for obj in objects: - state = QtStateMachine.QState(parent) + state = QState(parent) state.assignProperty(obj, 'geometry', selectedRect) parent.addTransition(obj.clicked, state) def create_animations(objects, machine): for obj in objects: - animation = QtCore.QPropertyAnimation(obj, b'geometry', obj) + animation = QPropertyAnimation(obj, b'geometry', obj) machine.addDefaultAnimation(animation) if __name__ == '__main__': + app = QApplication(sys.argv) - import sys - - app = QtWidgets.QApplication(sys.argv) - - p1 = Pixmap(QtGui.QPixmap(':/digikam.png')) - p2 = Pixmap(QtGui.QPixmap(':/akregator.png')) - p3 = Pixmap(QtGui.QPixmap(':/accessories-dictionary.png')) - p4 = Pixmap(QtGui.QPixmap(':/k3b.png')) + p1 = Pixmap(QPixmap(':/digikam.png')) + p2 = Pixmap(QPixmap(':/akregator.png')) + p3 = Pixmap(QPixmap(':/accessories-dictionary.png')) + p4 = Pixmap(QPixmap(':/k3b.png')) - p1.setGeometry(QtCore.QRectF(0.0, 0.0, 64.0, 64.0)) - p2.setGeometry(QtCore.QRectF(236.0, 0.0, 64.0, 64.0)) - p3.setGeometry(QtCore.QRectF(236.0, 236.0, 64.0, 64.0)) - p4.setGeometry(QtCore.QRectF(0.0, 236.0, 64.0, 64.0)) + p1.setGeometry(QRectF(0.0, 0.0, 64.0, 64.0)) + p2.setGeometry(QRectF(236.0, 0.0, 64.0, 64.0)) + p3.setGeometry(QRectF(236.0, 236.0, 64.0, 64.0)) + p4.setGeometry(QRectF(0.0, 236.0, 64.0, 64.0)) - scene = QtWidgets.QGraphicsScene(0, 0, 300, 300) - scene.setBackgroundBrush(QtCore.Qt.white) + scene = QGraphicsScene(0, 0, 300, 300) + scene.setBackgroundBrush(Qt.white) scene.addItem(p1) scene.addItem(p2) scene.addItem(p3) scene.addItem(p4) - window = QtWidgets.QGraphicsView(scene) + window = QGraphicsView(scene) window.setFrameStyle(0) - window.setAlignment(QtCore.Qt.AlignLeft | QtCore.Qt.AlignTop) - window.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff) - window.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff) + window.setAlignment(Qt.AlignLeft | Qt.AlignTop) + window.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff) + window.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff) - machine = QtStateMachine.QStateMachine() - machine.setGlobalRestorePolicy(QtStateMachine.QStateMachine.RestoreProperties) + machine = QStateMachine() + machine.setGlobalRestorePolicy(QStateMachine.RestoreProperties) - group = QtStateMachine.QState(machine) - selected_rect = QtCore.QRect(86, 86, 128, 128) + group = QState(machine) + selected_rect = QRect(86, 86, 128, 128) - idle_state = QtStateMachine.QState(group) + idle_state = QState(group) group.setInitialState(idle_state) objects = [p1, p2, p3, p4] diff --git a/examples/widgets/animation/states/states.py b/examples/widgets/animation/states/states.py index f6ae7b571..2150e1f81 100644 --- a/examples/widgets/animation/states/states.py +++ b/examples/widgets/animation/states/states.py @@ -40,72 +40,81 @@ ## ############################################################################# -from PySide6 import QtCore, QtGui, QtStateMachine, QtWidgets +import sys + +from PySide6.QtCore import (QPointF, QPropertyAnimation, + QSequentialAnimationGroup, QRect, QRectF, QSizeF, + Qt) +from PySide6.QtGui import QPixmap +from PySide6.QtWidgets import (QApplication, QGraphicsLinearLayout, + QGraphicsObject, QGraphicsProxyWidget, + QGraphicsWidget, QGraphicsScene, QGraphicsView, + QGroupBox, QPushButton, QRadioButton, + QTextEdit, QVBoxLayout, QWidget) + +from PySide6.QtStateMachine import QState, QStateMachine import states_rc -class Pixmap(QtWidgets.QGraphicsObject): +class Pixmap(QGraphicsObject): def __init__(self, pix): super(Pixmap, self).__init__() - self.p = QtGui.QPixmap(pix) + self.p = QPixmap(pix) def paint(self, painter, option, widget): - painter.drawPixmap(QtCore.QPointF(), self.p) + painter.drawPixmap(QPointF(), self.p) def boundingRect(self): - return QtCore.QRectF(QtCore.QPointF(0, 0), QtCore.QSizeF(self.p.size())) + return QRectF(QPointF(0, 0), QSizeF(self.p.size())) if __name__ == '__main__': - - import sys - - app = QtWidgets.QApplication(sys.argv) + app = QApplication(sys.argv) # Text edit and button. - edit = QtWidgets.QTextEdit() + edit = QTextEdit() edit.setText("asdf lkjha yuoiqwe asd iuaysd u iasyd uiy " "asdf lkjha yuoiqwe asd iuaysd u iasyd uiy " "asdf lkjha yuoiqwe asd iuaysd u iasyd uiy " "asdf lkjha yuoiqwe asd iuaysd u iasyd uiy!") - button = QtWidgets.QPushButton() - button_proxy = QtWidgets.QGraphicsProxyWidget() + button = QPushButton() + button_proxy = QGraphicsProxyWidget() button_proxy.setWidget(button) - edit_proxy = QtWidgets.QGraphicsProxyWidget() + edit_proxy = QGraphicsProxyWidget() edit_proxy.setWidget(edit) - box = QtWidgets.QGroupBox() + box = QGroupBox() box.setFlat(True) box.setTitle("Options") - layout2 = QtWidgets.QVBoxLayout() + layout2 = QVBoxLayout() box.setLayout(layout2) - layout2.addWidget(QtWidgets.QRadioButton("Herring")) - layout2.addWidget(QtWidgets.QRadioButton("Blue Parrot")) - layout2.addWidget(QtWidgets.QRadioButton("Petunias")) + layout2.addWidget(QRadioButton("Herring")) + layout2.addWidget(QRadioButton("Blue Parrot")) + layout2.addWidget(QRadioButton("Petunias")) layout2.addStretch() - box_proxy = QtWidgets.QGraphicsProxyWidget() + box_proxy = QGraphicsProxyWidget() box_proxy.setWidget(box) # Parent widget. - widget = QtWidgets.QGraphicsWidget() - layout = QtWidgets.QGraphicsLinearLayout(QtCore.Qt.Vertical, widget) + widget = QGraphicsWidget() + layout = QGraphicsLinearLayout(Qt.Vertical, widget) layout.addItem(edit_proxy) layout.addItem(button_proxy) widget.setLayout(layout) - p1 = Pixmap(QtGui.QPixmap(':/digikam.png')) - p2 = Pixmap(QtGui.QPixmap(':/akregator.png')) - p3 = Pixmap(QtGui.QPixmap(':/accessories-dictionary.png')) - p4 = Pixmap(QtGui.QPixmap(':/k3b.png')) - p5 = Pixmap(QtGui.QPixmap(':/help-browser.png')) - p6 = Pixmap(QtGui.QPixmap(':/kchart.png')) + p1 = Pixmap(QPixmap(':/digikam.png')) + p2 = Pixmap(QPixmap(':/akregator.png')) + p3 = Pixmap(QPixmap(':/accessories-dictionary.png')) + p4 = Pixmap(QPixmap(':/k3b.png')) + p5 = Pixmap(QPixmap(':/help-browser.png')) + p6 = Pixmap(QPixmap(':/kchart.png')) - scene = QtWidgets.QGraphicsScene(0, 0, 400, 300) + scene = QGraphicsScene(0, 0, 400, 300) scene.setBackgroundBrush(scene.palette().window()) scene.addItem(widget) scene.addItem(box_proxy) @@ -116,22 +125,22 @@ if __name__ == '__main__': scene.addItem(p5) scene.addItem(p6) - machine = QtStateMachine.QStateMachine() - state1 = QtStateMachine.QState(machine) - state2 = QtStateMachine.QState(machine) - state3 = QtStateMachine.QState(machine) + machine = QStateMachine() + state1 = QState(machine) + state2 = QState(machine) + state3 = QState(machine) machine.setInitialState(state1) # State 1. state1.assignProperty(button, 'text', "Switch to state 2") - state1.assignProperty(widget, 'geometry', QtCore.QRectF(0, 0, 400, 150)) - state1.assignProperty(box, 'geometry', QtCore.QRect(-200, 150, 200, 150)) - state1.assignProperty(p1, 'pos', QtCore.QPointF(68, 185)) - state1.assignProperty(p2, 'pos', QtCore.QPointF(168, 185)) - state1.assignProperty(p3, 'pos', QtCore.QPointF(268, 185)) - state1.assignProperty(p4, 'pos', QtCore.QPointF(68 - 150, 48 - 150)) - state1.assignProperty(p5, 'pos', QtCore.QPointF(168, 48 - 150)) - state1.assignProperty(p6, 'pos', QtCore.QPointF(268 + 150, 48 - 150)) + state1.assignProperty(widget, 'geometry', QRectF(0, 0, 400, 150)) + state1.assignProperty(box, 'geometry', QRect(-200, 150, 200, 150)) + state1.assignProperty(p1, 'pos', QPointF(68, 185)) + state1.assignProperty(p2, 'pos', QPointF(168, 185)) + state1.assignProperty(p3, 'pos', QPointF(268, 185)) + state1.assignProperty(p4, 'pos', QPointF(68 - 150, 48 - 150)) + state1.assignProperty(p5, 'pos', QPointF(168, 48 - 150)) + state1.assignProperty(p6, 'pos', QPointF(268 + 150, 48 - 150)) state1.assignProperty(p1, 'rotation', 0.0) state1.assignProperty(p2, 'rotation', 0.0) state1.assignProperty(p3, 'rotation', 0.0) @@ -148,14 +157,14 @@ if __name__ == '__main__': # State 2. state2.assignProperty(button, 'text', "Switch to state 3") - state2.assignProperty(widget, 'geometry', QtCore.QRectF(200, 150, 200, 150)) - state2.assignProperty(box, 'geometry', QtCore.QRect(9, 150, 190, 150)) - state2.assignProperty(p1, 'pos', QtCore.QPointF(68 - 150, 185 + 150)) - state2.assignProperty(p2, 'pos', QtCore.QPointF(168, 185 + 150)) - state2.assignProperty(p3, 'pos', QtCore.QPointF(268 + 150, 185 + 150)) - state2.assignProperty(p4, 'pos', QtCore.QPointF(64, 48)) - state2.assignProperty(p5, 'pos', QtCore.QPointF(168, 48)) - state2.assignProperty(p6, 'pos', QtCore.QPointF(268, 48)) + state2.assignProperty(widget, 'geometry', QRectF(200, 150, 200, 150)) + state2.assignProperty(box, 'geometry', QRect(9, 150, 190, 150)) + state2.assignProperty(p1, 'pos', QPointF(68 - 150, 185 + 150)) + state2.assignProperty(p2, 'pos', QPointF(168, 185 + 150)) + state2.assignProperty(p3, 'pos', QPointF(268 + 150, 185 + 150)) + state2.assignProperty(p4, 'pos', QPointF(64, 48)) + state2.assignProperty(p5, 'pos', QPointF(168, 48)) + state2.assignProperty(p6, 'pos', QPointF(268, 48)) state2.assignProperty(p1, 'rotation', -270.0) state2.assignProperty(p2, 'rotation', 90.0) state2.assignProperty(p3, 'rotation', 270.0) @@ -172,14 +181,14 @@ if __name__ == '__main__': # State 3. state3.assignProperty(button, 'text', "Switch to state 1") - state3.assignProperty(p1, 'pos', QtCore.QPointF(0, 5)) - state3.assignProperty(p2, 'pos', QtCore.QPointF(0, 5 + 64 + 5)) - state3.assignProperty(p3, 'pos', QtCore.QPointF(5, 5 + (64 + 5) + 64)) - state3.assignProperty(p4, 'pos', QtCore.QPointF(5 + 64 + 5, 5)) - state3.assignProperty(p5, 'pos', QtCore.QPointF(5 + 64 + 5, 5 + 64 + 5)) - state3.assignProperty(p6, 'pos', QtCore.QPointF(5 + 64 + 5, 5 + (64 + 5) + 64)) - state3.assignProperty(widget, 'geometry', QtCore.QRectF(138, 5, 400 - 138, 200)) - state3.assignProperty(box, 'geometry', QtCore.QRect(5, 205, 400, 90)) + state3.assignProperty(p1, 'pos', QPointF(0, 5)) + state3.assignProperty(p2, 'pos', QPointF(0, 5 + 64 + 5)) + state3.assignProperty(p3, 'pos', QPointF(5, 5 + (64 + 5) + 64)) + state3.assignProperty(p4, 'pos', QPointF(5 + 64 + 5, 5)) + state3.assignProperty(p5, 'pos', QPointF(5 + 64 + 5, 5 + 64 + 5)) + state3.assignProperty(p6, 'pos', QPointF(5 + 64 + 5, 5 + (64 + 5) + 64)) + state3.assignProperty(widget, 'geometry', QRectF(138, 5, 400 - 138, 200)) + state3.assignProperty(box, 'geometry', QRect(5, 205, 400, 90)) state3.assignProperty(p1, 'opacity', 1.0) state3.assignProperty(p2, 'opacity', 1.0) state3.assignProperty(p3, 'opacity', 1.0) @@ -188,77 +197,77 @@ if __name__ == '__main__': state3.assignProperty(p6, 'opacity', 1.0) t1 = state1.addTransition(button.clicked, state2) - animation_1sub_group = QtCore.QSequentialAnimationGroup() + animation_1sub_group = QSequentialAnimationGroup() animation_1sub_group.addPause(250) - animation_1sub_group.addAnimation(QtCore.QPropertyAnimation(box, b'geometry', state1)) + animation_1sub_group.addAnimation(QPropertyAnimation(box, b'geometry', state1)) t1.addAnimation(animation_1sub_group) - t1.addAnimation(QtCore.QPropertyAnimation(widget, b'geometry', state1)) - t1.addAnimation(QtCore.QPropertyAnimation(p1, b'pos', state1)) - t1.addAnimation(QtCore.QPropertyAnimation(p2, b'pos', state1)) - t1.addAnimation(QtCore.QPropertyAnimation(p3, b'pos', state1)) - t1.addAnimation(QtCore.QPropertyAnimation(p4, b'pos', state1)) - t1.addAnimation(QtCore.QPropertyAnimation(p5, b'pos', state1)) - t1.addAnimation(QtCore.QPropertyAnimation(p6, b'pos', state1)) - t1.addAnimation(QtCore.QPropertyAnimation(p1, b'rotation', state1)) - t1.addAnimation(QtCore.QPropertyAnimation(p2, b'rotation', state1)) - t1.addAnimation(QtCore.QPropertyAnimation(p3, b'rotation', state1)) - t1.addAnimation(QtCore.QPropertyAnimation(p4, b'rotation', state1)) - t1.addAnimation(QtCore.QPropertyAnimation(p5, b'rotation', state1)) - t1.addAnimation(QtCore.QPropertyAnimation(p6, b'rotation', state1)) - t1.addAnimation(QtCore.QPropertyAnimation(p1, b'opacity', state1)) - t1.addAnimation(QtCore.QPropertyAnimation(p2, b'opacity', state1)) - t1.addAnimation(QtCore.QPropertyAnimation(p3, b'opacity', state1)) - t1.addAnimation(QtCore.QPropertyAnimation(p4, b'opacity', state1)) - t1.addAnimation(QtCore.QPropertyAnimation(p5, b'opacity', state1)) - t1.addAnimation(QtCore.QPropertyAnimation(p6, b'opacity', state1)) + t1.addAnimation(QPropertyAnimation(widget, b'geometry', state1)) + t1.addAnimation(QPropertyAnimation(p1, b'pos', state1)) + t1.addAnimation(QPropertyAnimation(p2, b'pos', state1)) + t1.addAnimation(QPropertyAnimation(p3, b'pos', state1)) + t1.addAnimation(QPropertyAnimation(p4, b'pos', state1)) + t1.addAnimation(QPropertyAnimation(p5, b'pos', state1)) + t1.addAnimation(QPropertyAnimation(p6, b'pos', state1)) + t1.addAnimation(QPropertyAnimation(p1, b'rotation', state1)) + t1.addAnimation(QPropertyAnimation(p2, b'rotation', state1)) + t1.addAnimation(QPropertyAnimation(p3, b'rotation', state1)) + t1.addAnimation(QPropertyAnimation(p4, b'rotation', state1)) + t1.addAnimation(QPropertyAnimation(p5, b'rotation', state1)) + t1.addAnimation(QPropertyAnimation(p6, b'rotation', state1)) + t1.addAnimation(QPropertyAnimation(p1, b'opacity', state1)) + t1.addAnimation(QPropertyAnimation(p2, b'opacity', state1)) + t1.addAnimation(QPropertyAnimation(p3, b'opacity', state1)) + t1.addAnimation(QPropertyAnimation(p4, b'opacity', state1)) + t1.addAnimation(QPropertyAnimation(p5, b'opacity', state1)) + t1.addAnimation(QPropertyAnimation(p6, b'opacity', state1)) t2 = state2.addTransition(button.clicked, state3) - t2.addAnimation(QtCore.QPropertyAnimation(box, b'geometry', state2)) - t2.addAnimation(QtCore.QPropertyAnimation(widget, b'geometry', state2)) - t2.addAnimation(QtCore.QPropertyAnimation(p1, b'pos', state2)) - t2.addAnimation(QtCore.QPropertyAnimation(p2, b'pos', state2)) - t2.addAnimation(QtCore.QPropertyAnimation(p3, b'pos', state2)) - t2.addAnimation(QtCore.QPropertyAnimation(p4, b'pos', state2)) - t2.addAnimation(QtCore.QPropertyAnimation(p5, b'pos', state2)) - t2.addAnimation(QtCore.QPropertyAnimation(p6, b'pos', state2)) - t2.addAnimation(QtCore.QPropertyAnimation(p1, b'rotation', state2)) - t2.addAnimation(QtCore.QPropertyAnimation(p2, b'rotation', state2)) - t2.addAnimation(QtCore.QPropertyAnimation(p3, b'rotation', state2)) - t2.addAnimation(QtCore.QPropertyAnimation(p4, b'rotation', state2)) - t2.addAnimation(QtCore.QPropertyAnimation(p5, b'rotation', state2)) - t2.addAnimation(QtCore.QPropertyAnimation(p6, b'rotation', state2)) - t2.addAnimation(QtCore.QPropertyAnimation(p1, b'opacity', state2)) - t2.addAnimation(QtCore.QPropertyAnimation(p2, b'opacity', state2)) - t2.addAnimation(QtCore.QPropertyAnimation(p3, b'opacity', state2)) - t2.addAnimation(QtCore.QPropertyAnimation(p4, b'opacity', state2)) - t2.addAnimation(QtCore.QPropertyAnimation(p5, b'opacity', state2)) - t2.addAnimation(QtCore.QPropertyAnimation(p6, b'opacity', state2)) + t2.addAnimation(QPropertyAnimation(box, b'geometry', state2)) + t2.addAnimation(QPropertyAnimation(widget, b'geometry', state2)) + t2.addAnimation(QPropertyAnimation(p1, b'pos', state2)) + t2.addAnimation(QPropertyAnimation(p2, b'pos', state2)) + t2.addAnimation(QPropertyAnimation(p3, b'pos', state2)) + t2.addAnimation(QPropertyAnimation(p4, b'pos', state2)) + t2.addAnimation(QPropertyAnimation(p5, b'pos', state2)) + t2.addAnimation(QPropertyAnimation(p6, b'pos', state2)) + t2.addAnimation(QPropertyAnimation(p1, b'rotation', state2)) + t2.addAnimation(QPropertyAnimation(p2, b'rotation', state2)) + t2.addAnimation(QPropertyAnimation(p3, b'rotation', state2)) + t2.addAnimation(QPropertyAnimation(p4, b'rotation', state2)) + t2.addAnimation(QPropertyAnimation(p5, b'rotation', state2)) + t2.addAnimation(QPropertyAnimation(p6, b'rotation', state2)) + t2.addAnimation(QPropertyAnimation(p1, b'opacity', state2)) + t2.addAnimation(QPropertyAnimation(p2, b'opacity', state2)) + t2.addAnimation(QPropertyAnimation(p3, b'opacity', state2)) + t2.addAnimation(QPropertyAnimation(p4, b'opacity', state2)) + t2.addAnimation(QPropertyAnimation(p5, b'opacity', state2)) + t2.addAnimation(QPropertyAnimation(p6, b'opacity', state2)) t3 = state3.addTransition(button.clicked, state1) - t3.addAnimation(QtCore.QPropertyAnimation(box, b'geometry', state3)) - t3.addAnimation(QtCore.QPropertyAnimation(widget, b'geometry', state3)) - t3.addAnimation(QtCore.QPropertyAnimation(p1, b'pos', state3)) - t3.addAnimation(QtCore.QPropertyAnimation(p2, b'pos', state3)) - t3.addAnimation(QtCore.QPropertyAnimation(p3, b'pos', state3)) - t3.addAnimation(QtCore.QPropertyAnimation(p4, b'pos', state3)) - t3.addAnimation(QtCore.QPropertyAnimation(p5, b'pos', state3)) - t3.addAnimation(QtCore.QPropertyAnimation(p6, b'pos', state3)) - t3.addAnimation(QtCore.QPropertyAnimation(p1, b'rotation', state3)) - t3.addAnimation(QtCore.QPropertyAnimation(p2, b'rotation', state3)) - t3.addAnimation(QtCore.QPropertyAnimation(p3, b'rotation', state3)) - t3.addAnimation(QtCore.QPropertyAnimation(p4, b'rotation', state3)) - t3.addAnimation(QtCore.QPropertyAnimation(p5, b'rotation', state3)) - t3.addAnimation(QtCore.QPropertyAnimation(p6, b'rotation', state3)) - t3.addAnimation(QtCore.QPropertyAnimation(p1, b'opacity', state3)) - t3.addAnimation(QtCore.QPropertyAnimation(p2, b'opacity', state3)) - t3.addAnimation(QtCore.QPropertyAnimation(p3, b'opacity', state3)) - t3.addAnimation(QtCore.QPropertyAnimation(p4, b'opacity', state3)) - t3.addAnimation(QtCore.QPropertyAnimation(p5, b'opacity', state3)) - t3.addAnimation(QtCore.QPropertyAnimation(p6, b'opacity', state3)) + t3.addAnimation(QPropertyAnimation(box, b'geometry', state3)) + t3.addAnimation(QPropertyAnimation(widget, b'geometry', state3)) + t3.addAnimation(QPropertyAnimation(p1, b'pos', state3)) + t3.addAnimation(QPropertyAnimation(p2, b'pos', state3)) + t3.addAnimation(QPropertyAnimation(p3, b'pos', state3)) + t3.addAnimation(QPropertyAnimation(p4, b'pos', state3)) + t3.addAnimation(QPropertyAnimation(p5, b'pos', state3)) + t3.addAnimation(QPropertyAnimation(p6, b'pos', state3)) + t3.addAnimation(QPropertyAnimation(p1, b'rotation', state3)) + t3.addAnimation(QPropertyAnimation(p2, b'rotation', state3)) + t3.addAnimation(QPropertyAnimation(p3, b'rotation', state3)) + t3.addAnimation(QPropertyAnimation(p4, b'rotation', state3)) + t3.addAnimation(QPropertyAnimation(p5, b'rotation', state3)) + t3.addAnimation(QPropertyAnimation(p6, b'rotation', state3)) + t3.addAnimation(QPropertyAnimation(p1, b'opacity', state3)) + t3.addAnimation(QPropertyAnimation(p2, b'opacity', state3)) + t3.addAnimation(QPropertyAnimation(p3, b'opacity', state3)) + t3.addAnimation(QPropertyAnimation(p4, b'opacity', state3)) + t3.addAnimation(QPropertyAnimation(p5, b'opacity', state3)) + t3.addAnimation(QPropertyAnimation(p6, b'opacity', state3)) machine.start() - view = QtWidgets.QGraphicsView(scene) + view = QGraphicsView(scene) view.show() sys.exit(app.exec_()) diff --git a/examples/widgets/dialogs/extension/extension.py b/examples/widgets/dialogs/extension/extension.py index 678a38168..00222a6d2 100644 --- a/examples/widgets/dialogs/extension/extension.py +++ b/examples/widgets/dialogs/extension/extension.py @@ -2,7 +2,7 @@ ############################################################################# ## ## Copyright (C) 2013 Riverbank Computing Limited. -## Copyright (C) 2016 The Qt Company Ltd. +## Copyright (C) 2021 The Qt Company Ltd. ## Contact: http://www.qt.io/licensing/ ## ## This file is part of the Qt for Python examples of the Qt Toolkit. @@ -42,72 +42,74 @@ """PySide6 port of the widgets/dialogs/extension example from Qt v5.x""" -from PySide6 import QtCore, QtWidgets +import sys +from PySide6.QtCore import Qt +from PySide6.QtWidgets import (QApplication, QCheckBox, QDialog, + QDialogButtonBox, QGridLayout, QHBoxLayout, + QLabel, QLayout, QLineEdit, QPushButton, + QVBoxLayout, QWidget) -class FindDialog(QtWidgets.QDialog): + +class FindDialog(QDialog): def __init__(self, parent=None): super(FindDialog, self).__init__(parent) - label = QtWidgets.QLabel("Find &what:") - line_edit = QtWidgets.QLineEdit() + label = QLabel("Find &what:") + line_edit = QLineEdit() label.setBuddy(line_edit) - case_check_box = QtWidgets.QCheckBox("Match &case") - from_start_check_box = QtWidgets.QCheckBox("Search from &start") + case_check_box = QCheckBox("Match &case") + from_start_check_box = QCheckBox("Search from &start") from_start_check_box.setChecked(True) - find_button = QtWidgets.QPushButton("&Find") + find_button = QPushButton("&Find") find_button.setDefault(True) - more_button = QtWidgets.QPushButton("&More") + more_button = QPushButton("&More") more_button.setCheckable(True) more_button.setAutoDefault(False) - button_box = QtWidgets.QDialogButtonBox(QtCore.Qt.Vertical) - button_box.addButton(find_button, QtWidgets.QDialogButtonBox.ActionRole) - button_box.addButton(more_button, QtWidgets.QDialogButtonBox.ActionRole) + button_box = QDialogButtonBox(Qt.Vertical) + button_box.addButton(find_button, QDialogButtonBox.ActionRole) + button_box.addButton(more_button, QDialogButtonBox.ActionRole) - extension = QtWidgets.QWidget() + extension = QWidget() - whole_words_check_box = QtWidgets.QCheckBox("&Whole words") - backward_check_box = QtWidgets.QCheckBox("Search &backward") - search_selection_check_box = QtWidgets.QCheckBox("Search se&lection") + whole_words_check_box = QCheckBox("&Whole words") + backward_check_box = QCheckBox("Search &backward") + search_selection_check_box = QCheckBox("Search se&lection") more_button.toggled.connect(extension.setVisible) - extension_layout = QtWidgets.QVBoxLayout() + extension_layout = QVBoxLayout() extension_layout.setContentsMargins(0, 0, 0, 0) extension_layout.addWidget(whole_words_check_box) extension_layout.addWidget(backward_check_box) extension_layout.addWidget(search_selection_check_box) extension.setLayout(extension_layout) - top_left_layout = QtWidgets.QHBoxLayout() + top_left_layout = QHBoxLayout() top_left_layout.addWidget(label) top_left_layout.addWidget(line_edit) - left_layout = QtWidgets.QVBoxLayout() + left_layout = QVBoxLayout() left_layout.addLayout(top_left_layout) left_layout.addWidget(case_check_box) left_layout.addWidget(from_start_check_box) left_layout.addStretch(1) - main_layout = QtWidgets.QGridLayout() - main_layout.setSizeConstraint(QtWidgets.QLayout.SetFixedSize) + main_layout = QGridLayout(self) + main_layout.setSizeConstraint(QLayout.SetFixedSize) main_layout.addLayout(left_layout, 0, 0) main_layout.addWidget(button_box, 0, 1) main_layout.addWidget(extension, 1, 0, 1, 2) - self.setLayout(main_layout) self.setWindowTitle("Extension") extension.hide() if __name__ == '__main__': - - import sys - - app = QtWidgets.QApplication(sys.argv) + app = QApplication(sys.argv) dialog = FindDialog() sys.exit(dialog.exec_()) diff --git a/examples/widgets/dialogs/findfiles/findfiles.py b/examples/widgets/dialogs/findfiles/findfiles.py index 365d75377..7ddb6907c 100644 --- a/examples/widgets/dialogs/findfiles/findfiles.py +++ b/examples/widgets/dialogs/findfiles/findfiles.py @@ -42,10 +42,19 @@ """PySide6 port of the widgets/dialogs/findfiles example from Qt v5.x""" -from PySide6 import QtCore, QtGui, QtWidgets +import sys +from PySide6.QtCore import (QCoreApplication, QDir, QFile, QFileInfo, + QIODevice, QTextStream, QUrl, Qt) +from PySide6.QtGui import QDesktopServices +from PySide6.QtWidgets import (QAbstractItemView, QApplication, QComboBox, + QDialog, QFileDialog, QGridLayout, QHBoxLayout, + QHeaderView, QLabel, QLineEdit, QProgressDialog, + QPushButton, QSizePolicy, QTableWidget, + QTableWidgetItem, QVBoxLayout, QWidget) -class Window(QtWidgets.QDialog): + +class Window(QDialog): def __init__(self, parent=None): super(Window, self).__init__(parent) @@ -54,20 +63,20 @@ class Window(QtWidgets.QDialog): self._file_combo_box = self.create_combo_box("*") self._text_combo_box = self.create_combo_box() - self._directory_combo_box = self.create_combo_box(QtCore.QDir.currentPath()) + self._directory_combo_box = self.create_combo_box(QDir.currentPath()) - file_label = QtWidgets.QLabel("Named:") - text_label = QtWidgets.QLabel("Containing text:") - directory_label = QtWidgets.QLabel("In directory:") - self._files_found_label = QtWidgets.QLabel() + file_label = QLabel("Named:") + text_label = QLabel("Containing text:") + directory_label = QLabel("In directory:") + self._files_found_label = QLabel() self.create_files_table() - buttons_layout = QtWidgets.QHBoxLayout() + buttons_layout = QHBoxLayout() buttons_layout.addStretch() buttons_layout.addWidget(self._find_button) - main_layout = QtWidgets.QGridLayout() + main_layout = QGridLayout() main_layout.addWidget(file_label, 0, 0) main_layout.addWidget(self._file_combo_box, 0, 1, 1, 2) main_layout.addWidget(text_label, 1, 0) @@ -84,8 +93,8 @@ class Window(QtWidgets.QDialog): self.resize(500, 300) def browse(self): - directory = QtWidgets.QFileDialog.getExistingDirectory(self, "Find Files", - QtCore.QDir.currentPath()) + directory = QFileDialog.getExistingDirectory(self, "Find Files", + QDir.currentPath()) if directory: if self._directory_combo_box.findText(directory) == -1: @@ -109,18 +118,18 @@ class Window(QtWidgets.QDialog): self.update_combo_box(self._text_combo_box) self.update_combo_box(self._directory_combo_box) - self._current_dir = QtCore.QDir(path) + self._current_dir = QDir(path) if not file_name: file_name = "*" files = self._current_dir.entryList([file_name], - QtCore.QDir.Files | QtCore.QDir.NoSymLinks) + QDir.Files | QDir.NoSymLinks) if text: files = self.find_files(files, text) self.show_files(files) def find_files(self, files, text): - progress_dialog = QtWidgets.QProgressDialog(self) + progress_dialog = QProgressDialog(self) progress_dialog.setCancelButtonText("&Cancel") progress_dialog.setRange(0, len(files)) @@ -132,15 +141,15 @@ class Window(QtWidgets.QDialog): progress_dialog.setValue(i) n = len(files) progress_dialog.setLabelText(f"Searching file number {i} of {n}...") - QtCore.QCoreApplication.processEvents() + QCoreApplication.processEvents() if progress_dialog.wasCanceled(): break - in_file = QtCore.QFile(self._current_dir.absoluteFilePath(files[i])) + in_file = QFile(self._current_dir.absoluteFilePath(files[i])) - if in_file.open(QtCore.QIODevice.ReadOnly): - stream = QtCore.QTextStream(in_file) + if in_file.open(QIODevice.ReadOnly): + stream = QTextStream(in_file) while not stream.atEnd(): if progress_dialog.wasCanceled(): break @@ -155,15 +164,15 @@ class Window(QtWidgets.QDialog): def show_files(self, files): for fn in files: - file = QtCore.QFile(self._current_dir.absoluteFilePath(fn)) - size = QtCore.QFileInfo(file).size() + file = QFile(self._current_dir.absoluteFilePath(fn)) + size = QFileInfo(file).size() - file_name_item = QtWidgets.QTableWidgetItem(fn) - file_name_item.setFlags(file_name_item.flags() ^ QtCore.Qt.ItemIsEditable) + file_name_item = QTableWidgetItem(fn) + file_name_item.setFlags(file_name_item.flags() ^ Qt.ItemIsEditable) size_kb = int((size + 1023) / 1024) - size_item = QtWidgets.QTableWidgetItem(f"{size_kb} KB") - size_item.setTextAlignment(QtCore.Qt.AlignVCenter | QtCore.Qt.AlignRight) - size_item.setFlags(size_item.flags() ^ QtCore.Qt.ItemIsEditable) + size_item = QTableWidgetItem(f"{size_kb} KB") + size_item.setTextAlignment(Qt.AlignVCenter | Qt.AlignRight) + size_item.setFlags(size_item.flags() ^ Qt.ItemIsEditable) row = self._files_table.rowCount() self._files_table.insertRow(row) @@ -174,24 +183,24 @@ class Window(QtWidgets.QDialog): self._files_found_label.setText(f"{n} file(s) found (Double click on a file to open it)") def create_button(self, text, member): - button = QtWidgets.QPushButton(text) + button = QPushButton(text) button.clicked.connect(member) return button def create_combo_box(self, text=""): - combo_box = QtWidgets.QComboBox() + combo_box = QComboBox() combo_box.setEditable(True) combo_box.addItem(text) - combo_box.setSizePolicy(QtWidgets.QSizePolicy.Expanding, - QtWidgets.QSizePolicy.Preferred) + combo_box.setSizePolicy(QSizePolicy.Expanding, + QSizePolicy.Preferred) return combo_box def create_files_table(self): - self._files_table = QtWidgets.QTableWidget(0, 2) - self._files_table.setSelectionBehavior(QtWidgets.QAbstractItemView.SelectRows) + self._files_table = QTableWidget(0, 2) + self._files_table.setSelectionBehavior(QAbstractItemView.SelectRows) self._files_table.setHorizontalHeaderLabels(("File Name", "Size")) - self._files_table.horizontalHeader().setSectionResizeMode(0, QtWidgets.QHeaderView.Stretch) + self._files_table.horizontalHeader().setSectionResizeMode(0, QHeaderView.Stretch) self._files_table.verticalHeader().hide() self._files_table.setShowGrid(False) @@ -200,14 +209,14 @@ class Window(QtWidgets.QDialog): def open_file_of_item(self, row, column): item = self._files_table.item(row, 0) - QtGui.QDesktopServices.openUrl(QtCore.QUrl(self._current_dir.absoluteFilePath(item.text()))) + QDesktopServices.openUrl(QUrl(self._current_dir.absoluteFilePath(item.text()))) if __name__ == '__main__': import sys - app = QtWidgets.QApplication(sys.argv) + app = QApplication(sys.argv) window = Window() window.show() sys.exit(app.exec_()) diff --git a/examples/widgets/dialogs/trivialwizard/trivialwizard.py b/examples/widgets/dialogs/trivialwizard/trivialwizard.py index 30b7f59b0..32cc1aabe 100644 --- a/examples/widgets/dialogs/trivialwizard/trivialwizard.py +++ b/examples/widgets/dialogs/trivialwizard/trivialwizard.py @@ -2,7 +2,7 @@ ############################################################################# ## ## Copyright (C) 2013 Riverbank Computing Limited. -## Copyright (C) 2016 The Qt Company Ltd. +## Copyright (C) 2021 The Qt Company Ltd. ## Contact: http://www.qt.io/licensing/ ## ## This file is part of the Qt for Python examples of the Qt Toolkit. @@ -42,66 +42,55 @@ """PySide6 port of the widgets/dialogs/trivialwizard example from Qt v5.x""" -from PySide6 import QtWidgets +import sys + +from PySide6.QtWidgets import (QApplication, QFormLayout, QLabel, QLineEdit, + QVBoxLayout, QWidget, QWizardPage, QWizard) def create_intro_page(): - page = QtWidgets.QWizardPage() + page = QWizardPage() page.setTitle("Introduction") - label = QtWidgets.QLabel("This wizard will help you register your copy of " + label = QLabel("This wizard will help you register your copy of " "Super Product Two.") label.setWordWrap(True) - layout = QtWidgets.QVBoxLayout() + layout = QVBoxLayout(page) layout.addWidget(label) - page.setLayout(layout) return page def create_registration_page(): - page = QtWidgets.QWizardPage() + page = QWizardPage() page.setTitle("Registration") page.setSubTitle("Please fill both fields.") - name_label = QtWidgets.QLabel("Name:") - name_line_edit = QtWidgets.QLineEdit() - - email_label = QtWidgets.QLabel("Email address:") - email_line_edit = QtWidgets.QLineEdit() - - layout = QtWidgets.QGridLayout() - layout.addWidget(name_label, 0, 0) - layout.addWidget(name_line_edit, 0, 1) - layout.addWidget(email_label, 1, 0) - layout.addWidget(email_line_edit, 1, 1) - page.setLayout(layout) + layout = QFormLayout(page) + layout.addRow("Name:", QLineEdit()) + layout.addRow("Email address:", QLineEdit()) return page def create_conclusion_page(): - page = QtWidgets.QWizardPage() + page = QWizardPage() page.setTitle("Conclusion") - label = QtWidgets.QLabel("You are now successfully registered. Have a nice day!") + label = QLabel("You are now successfully registered. Have a nice day!") label.setWordWrap(True) - layout = QtWidgets.QVBoxLayout() + layout = QVBoxLayout(page) layout.addWidget(label) - page.setLayout(layout) return page if __name__ == '__main__': + app = QApplication(sys.argv) - import sys - - app = QtWidgets.QApplication(sys.argv) - - wizard = QtWidgets.QWizard() + wizard = QWizard() wizard.addPage(create_intro_page()) wizard.addPage(create_registration_page()) wizard.addPage(create_conclusion_page()) diff --git a/examples/widgets/effects/lighting.py b/examples/widgets/effects/lighting.py index cbaffb325..e319b5f66 100644 --- a/examples/widgets/effects/lighting.py +++ b/examples/widgets/effects/lighting.py @@ -2,7 +2,7 @@ ############################################################################# ## ## Copyright (C) 2013 Riverbank Computing Limited. -## Copyright (C) 2016 The Qt Company Ltd. +## Copyright (C) 2021 The Qt Company Ltd. ## Contact: http://www.qt.io/licensing/ ## ## This file is part of the Qt for Python examples of the Qt Toolkit. @@ -41,16 +41,22 @@ ############################################################################# import math +import sys -from PySide6 import QtCore, QtGui, QtWidgets +from PySide6.QtCore import QPointF, QTimer, Qt +from PySide6.QtGui import (QBrush, QColor, QLinearGradient, QPainter, QPen, + QPixmap, QRadialGradient) +from PySide6.QtWidgets import (QApplication, QFrame, QGraphicsDropShadowEffect, + QGraphicsEllipseItem, QGraphicsRectItem, + QGraphicsScene, QGraphicsView, QWidget) -class Lighting(QtWidgets.QGraphicsView): +class Lighting(QGraphicsView): def __init__(self, parent=None): super(Lighting, self).__init__(parent) self.angle = 0.0 - self.m_scene = QtWidgets.QGraphicsScene() + self.m_scene = QGraphicsScene() self.m_lightSource = None self.m_items = [] @@ -58,33 +64,33 @@ class Lighting(QtWidgets.QGraphicsView): self.setup_scene() - timer = QtCore.QTimer(self) + timer = QTimer(self) timer.timeout.connect(self.animate) timer.setInterval(30) timer.start() - self.setRenderHint(QtGui.QPainter.Antialiasing) - self.setFrameStyle(QtWidgets.QFrame.NoFrame) + self.setRenderHint(QPainter.Antialiasing) + self.setFrameStyle(QFrame.NoFrame) def setup_scene(self): self.m_scene.setSceneRect(-300, -200, 600, 460) - linear_grad = QtGui.QLinearGradient(QtCore.QPointF(-100, -100), - QtCore.QPointF(100, 100)) - linear_grad.setColorAt(0, QtGui.QColor(255, 255, 255)) - linear_grad.setColorAt(1, QtGui.QColor(192, 192, 255)) + linear_grad = QLinearGradient(QPointF(-100, -100), + QPointF(100, 100)) + linear_grad.setColorAt(0, QColor(255, 255, 255)) + linear_grad.setColorAt(1, QColor(192, 192, 255)) self.setBackgroundBrush(linear_grad) - radial_grad = QtGui.QRadialGradient(30, 30, 30) - radial_grad.setColorAt(0, QtCore.Qt.yellow) - radial_grad.setColorAt(0.2, QtCore.Qt.yellow) - radial_grad.setColorAt(1, QtCore.Qt.transparent) + radial_grad = QRadialGradient(30, 30, 30) + radial_grad.setColorAt(0, Qt.yellow) + radial_grad.setColorAt(0.2, Qt.yellow) + radial_grad.setColorAt(1, Qt.transparent) - pixmap = QtGui.QPixmap(60, 60) - pixmap.fill(QtCore.Qt.transparent) + pixmap = QPixmap(60, 60) + pixmap.fill(Qt.transparent) - painter = QtGui.QPainter(pixmap) - painter.setPen(QtCore.Qt.NoPen) + painter = QPainter(pixmap) + painter.setPen(Qt.NoPen) painter.setBrush(radial_grad) painter.drawEllipse(0, 0, 60, 60) painter.end() @@ -95,14 +101,14 @@ class Lighting(QtWidgets.QGraphicsView): for i in range(-2, 3): for j in range(-2, 3): if (i + j) & 1: - item = QtWidgets.QGraphicsEllipseItem(0, 0, 50, 50) + item = QGraphicsEllipseItem(0, 0, 50, 50) else: - item = QtWidgets.QGraphicsRectItem(0, 0, 50, 50) + item = QGraphicsRectItem(0, 0, 50, 50) - item.setPen(QtGui.QPen(QtCore.Qt.black, 1)) - item.setBrush(QtGui.QBrush(QtCore.Qt.white)) + item.setPen(QPen(Qt.black, 1)) + item.setBrush(QBrush(Qt.white)) - effect = QtWidgets.QGraphicsDropShadowEffect(self) + effect = QGraphicsDropShadowEffect(self) effect.setBlurRadius(8) item.setGraphicsEffect(effect) item.setZValue(1) @@ -119,8 +125,8 @@ class Lighting(QtWidgets.QGraphicsView): for item in self.m_items: effect = item.graphicsEffect() - delta = QtCore.QPointF(item.x() - xs, item.y() - ys) - effect.setOffset(QtCore.QPointF(delta.toPoint() / 30)) + delta = QPointF(item.x() - xs, item.y() - ys) + effect.setOffset(QPointF(delta.toPoint() / 30)) dd = math.hypot(delta.x(), delta.y()) color = effect.color() @@ -131,10 +137,7 @@ class Lighting(QtWidgets.QGraphicsView): if __name__ == '__main__': - - import sys - - app = QtWidgets.QApplication(sys.argv) + app = QApplication(sys.argv) lighting = Lighting() lighting.setWindowTitle("Lighting and Shadows") diff --git a/examples/widgets/layouts/basiclayouts/basiclayouts.py b/examples/widgets/layouts/basiclayouts/basiclayouts.py index 64d53213e..27ab35fb1 100644 --- a/examples/widgets/layouts/basiclayouts/basiclayouts.py +++ b/examples/widgets/layouts/basiclayouts/basiclayouts.py @@ -2,7 +2,7 @@ ############################################################################ ## ## Copyright (C) 2013 Riverbank Computing Limited. -## Copyright (C) 2016 The Qt Company Ltd. +## Copyright (C) 2021 The Qt Company Ltd. ## Contact: http://www.qt.io/licensing/ ## ## This file is part of the Qt for Python examples of the Qt Toolkit. @@ -42,10 +42,17 @@ """PySide6 port of the widgets/layouts/basiclayout example from Qt v5.x""" -from PySide6 import QtWidgets +import sys +from PySide6.QtCore import Qt +from PySide6.QtWidgets import (QApplication, QComboBox, QDialog, + QDialogButtonBox, QGridLayout, QGroupBox, + QFormLayout, QHBoxLayout, QLabel, QLineEdit, + QMenu, QMenuBar, QPushButton, QSpinBox, + QTextEdit, QVBoxLayout, QWidget) -class Dialog(QtWidgets.QDialog): + +class Dialog(QDialog): num_grid_rows = 3 num_buttons = 4 @@ -57,16 +64,16 @@ class Dialog(QtWidgets.QDialog): self.create_grid_group_box() self.create_form_group_box() - big_editor = QtWidgets.QTextEdit() + big_editor = QTextEdit() big_editor.setPlainText("This widget takes up all the remaining space " "in the top-level layout.") - button_box = QtWidgets.QDialogButtonBox(QtWidgets.QDialogButtonBox.Ok | QtWidgets.QDialogButtonBox.Cancel) + button_box = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel) button_box.accepted.connect(self.accept) button_box.rejected.connect(self.reject) - main_layout = QtWidgets.QVBoxLayout() + main_layout = QVBoxLayout() main_layout.setMenuBar(self._menu_bar) main_layout.addWidget(self._horizontal_group_box) main_layout.addWidget(self._grid_group_box) @@ -78,35 +85,35 @@ class Dialog(QtWidgets.QDialog): self.setWindowTitle("Basic Layouts") def create_menu(self): - self._menu_bar = QtWidgets.QMenuBar() + self._menu_bar = QMenuBar() - self._file_menu = QtWidgets.QMenu("&File", self) + self._file_menu = QMenu("&File", self) self._exit_action = self._file_menu.addAction("E&xit") self._menu_bar.addMenu(self._file_menu) self._exit_action.triggered.connect(self.accept) def create_horizontal_group_box(self): - self._horizontal_group_box = QtWidgets.QGroupBox("Horizontal layout") - layout = QtWidgets.QHBoxLayout() + self._horizontal_group_box = QGroupBox("Horizontal layout") + layout = QHBoxLayout() for i in range(Dialog.num_buttons): - button = QtWidgets.QPushButton(f"Button {i + 1}") + button = QPushButton(f"Button {i + 1}") layout.addWidget(button) self._horizontal_group_box.setLayout(layout) def create_grid_group_box(self): - self._grid_group_box = QtWidgets.QGroupBox("Grid layout") - layout = QtWidgets.QGridLayout() + self._grid_group_box = QGroupBox("Grid layout") + layout = QGridLayout() for i in range(Dialog.num_grid_rows): - label = QtWidgets.QLabel(f"Line {i + 1}:") - line_edit = QtWidgets.QLineEdit() + label = QLabel(f"Line {i + 1}:") + line_edit = QLineEdit() layout.addWidget(label, i + 1, 0) layout.addWidget(line_edit, i + 1, 1) - self._small_editor = QtWidgets.QTextEdit() + self._small_editor = QTextEdit() self._small_editor.setPlainText("This widget takes up about two thirds " "of the grid layout.") @@ -117,18 +124,15 @@ class Dialog(QtWidgets.QDialog): self._grid_group_box.setLayout(layout) def create_form_group_box(self): - self._form_group_box = QtWidgets.QGroupBox("Form layout") - layout = QtWidgets.QFormLayout() - layout.addRow(QtWidgets.QLabel("Line 1:"), QtWidgets.QLineEdit()) - layout.addRow(QtWidgets.QLabel("Line 2, long text:"), QtWidgets.QComboBox()) - layout.addRow(QtWidgets.QLabel("Line 3:"), QtWidgets.QSpinBox()) + self._form_group_box = QGroupBox("Form layout") + layout = QFormLayout() + layout.addRow(QLabel("Line 1:"), QLineEdit()) + layout.addRow(QLabel("Line 2, long text:"), QComboBox()) + layout.addRow(QLabel("Line 3:"), QSpinBox()) self._form_group_box.setLayout(layout) if __name__ == '__main__': - - import sys - - app = QtWidgets.QApplication(sys.argv) + app = QApplication(sys.argv) dialog = Dialog() sys.exit(dialog.exec_()) diff --git a/examples/widgets/richtext/orderform/orderform.py b/examples/widgets/richtext/orderform/orderform.py index 2d69a0aa6..e8be602c2 100644 --- a/examples/widgets/richtext/orderform/orderform.py +++ b/examples/widgets/richtext/orderform/orderform.py @@ -42,14 +42,24 @@ """PySide6 port of the widgets/richtext/orderform example from Qt v5.x""" -from PySide6 import QtCore, QtGui, QtWidgets, QtPrintSupport +import sys +from PySide6.QtCore import QDate, Qt, Signal, Slot +from PySide6.QtGui import (QFont, QTextCharFormat, QTextCursor, + QTextFrameFormat, QTextLength, QTextTableFormat) +from PySide6.QtWidgets import (QApplication, QCheckBox, QDialog, + QDialogButtonBox, QGridLayout, QLabel, + QLineEdit, QMainWindow, QMenu, QMessageBox, + QTableWidget, QTableWidgetItem, QTabWidget, + QTextEdit, QWidget) +from PySide6.QtPrintSupport import QAbstractPrintDialog, QPrintDialog, QPrinter -class MainWindow(QtWidgets.QMainWindow): + +class MainWindow(QMainWindow): def __init__(self): super(MainWindow, self).__init__() - file_menu = QtWidgets.QMenu("&File", self) + file_menu = QMenu("&File", self) new_action = file_menu.addAction("&New...") new_action.setShortcut("Ctrl+N") self._print_action = file_menu.addAction("&Print...", self.print_file) @@ -59,7 +69,7 @@ class MainWindow(QtWidgets.QMainWindow): quit_action.setShortcut("Ctrl+Q") self.menuBar().addMenu(file_menu) - self.letters = QtWidgets.QTabWidget() + self.letters = QTabWidget() new_action.triggered.connect(self.open_dialog) quit_action.triggered.connect(self.close) @@ -68,26 +78,26 @@ class MainWindow(QtWidgets.QMainWindow): self.setWindowTitle("Order Form") def create_letter(self, name, address, orderItems, sendOffers): - editor = QtWidgets.QTextEdit() + editor = QTextEdit() tab_index = self.letters.addTab(editor, name) self.letters.setCurrentIndex(tab_index) cursor = editor.textCursor() - cursor.movePosition(QtGui.QTextCursor.Start) + cursor.movePosition(QTextCursor.Start) top_frame = cursor.currentFrame() top_frame_format = top_frame.frameFormat() top_frame_format.setPadding(16) top_frame.setFrameFormat(top_frame_format) - text_format = QtGui.QTextCharFormat() - bold_format = QtGui.QTextCharFormat() - bold_format.setFontWeight(QtGui.QFont.Bold) + text_format = QTextCharFormat() + bold_format = QTextCharFormat() + bold_format.setFontWeight(QFont.Bold) - reference_frame_format = QtGui.QTextFrameFormat() + reference_frame_format = QTextFrameFormat() reference_frame_format.setBorder(1) reference_frame_format.setPadding(8) - reference_frame_format.setPosition(QtGui.QTextFrameFormat.FloatRight) - reference_frame_format.setWidth(QtGui.QTextLength(QtGui.QTextLength.PercentageLength, 40)) + reference_frame_format.setPosition(QTextFrameFormat.FloatRight) + reference_frame_format.setWidth(QTextLength(QTextLength.PercentageLength, 40)) cursor.insertFrame(reference_frame_format) cursor.insertText("A company", bold_format) @@ -108,13 +118,13 @@ class MainWindow(QtWidgets.QMainWindow): cursor.insertBlock() cursor.insertBlock() - date = QtCore.QDate.currentDate() + date = QDate.currentDate() date_str = date.toString('d MMMM yyyy') cursor.insertText(f"Date: {date_str}", text_format) cursor.insertBlock() - body_frame_format = QtGui.QTextFrameFormat() - body_frame_format.setWidth(QtGui.QTextLength(QtGui.QTextLength.PercentageLength, 100)) + body_frame_format = QTextFrameFormat() + body_frame_format.setWidth(QTextLength(QTextLength.PercentageLength, 100)) cursor.insertFrame(body_frame_format) cursor.insertText("I would like to place an order for the following " @@ -122,8 +132,8 @@ class MainWindow(QtWidgets.QMainWindow): cursor.insertBlock() cursor.insertBlock() - order_table_format = QtGui.QTextTableFormat() - order_table_format.setAlignment(QtCore.Qt.AlignHCenter) + order_table_format = QTextTableFormat() + order_table_format.setAlignment(Qt.AlignHCenter) order_table = cursor.insertTable(1, 2, order_table_format) order_frame_format = cursor.currentFrame().frameFormat() @@ -187,49 +197,49 @@ class MainWindow(QtWidgets.QMainWindow): def open_dialog(self): dialog = DetailsDialog("Enter Customer Details", self) - if dialog.exec_() == QtWidgets.QDialog.Accepted: + if dialog.exec_() == QDialog.Accepted: self.create_letter(dialog.sender_name(), dialog.sender_address(), dialog.order_items(), dialog.send_offers()) def print_file(self): editor = self.letters.currentWidget() - printer = QtPrintSupport.QPrinter() + printer = QPrinter() - dialog = QtPrintSupport.QPrintDialog(printer, self) + dialog = QPrintDialog(printer, self) dialog.setWindowTitle("Print Document") if editor.textCursor().hasSelection(): - dialog.addEnabledOption(QtPrintSupport.QAbstractPrintDialog.PrintSelection) + dialog.addEnabledOption(QAbstractPrintDialog.PrintSelection) - if dialog.exec_() != QtWidgets.QDialog.Accepted: + if dialog.exec_() != QDialog.Accepted: return editor.print_(printer) -class DetailsDialog(QtWidgets.QDialog): +class DetailsDialog(QDialog): def __init__(self, title, parent): super(DetailsDialog, self).__init__(parent) self.items = ("T-shirt", "Badge", "Reference book", "Coffee cup") - name_label = QtWidgets.QLabel("Name:") - address_label = QtWidgets.QLabel("Address:") - address_label.setAlignment(QtCore.Qt.AlignLeft | QtCore.Qt.AlignTop) + name_label = QLabel("Name:") + address_label = QLabel("Address:") + address_label.setAlignment(Qt.AlignLeft | Qt.AlignTop) - self._name_edit = QtWidgets.QLineEdit() - self._address_edit = QtWidgets.QTextEdit() - self._offers_check_box = QtWidgets.QCheckBox("Send information about " + self._name_edit = QLineEdit() + self._address_edit = QTextEdit() + self._offers_check_box = QCheckBox("Send information about " "products and special offers:") self.setup_items_table() - button_box = QtWidgets.QDialogButtonBox(QtWidgets.QDialogButtonBox.Ok | QtWidgets.QDialogButtonBox.Cancel) + button_box = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel) button_box.accepted.connect(self.verify) button_box.rejected.connect(self.reject) - main_layout = QtWidgets.QGridLayout() + main_layout = QGridLayout(self) main_layout.addWidget(name_label, 0, 0) main_layout.addWidget(self._name_edit, 0, 1) main_layout.addWidget(address_label, 1, 0) @@ -237,18 +247,17 @@ class DetailsDialog(QtWidgets.QDialog): main_layout.addWidget(self._items_table, 0, 2, 2, 1) main_layout.addWidget(self._offers_check_box, 2, 1, 1, 2) main_layout.addWidget(button_box, 3, 0, 1, 3) - self.setLayout(main_layout) self.setWindowTitle(title) def setup_items_table(self): - self._items_table = QtWidgets.QTableWidget(len(self.items), 2) + self._items_table = QTableWidget(len(self.items), 2) for row, item in enumerate(self.items): - name = QtWidgets.QTableWidgetItem(item) - name.setFlags(QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable) + name = QTableWidgetItem(item) + name.setFlags(Qt.ItemIsEnabled | Qt.ItemIsSelectable) self._items_table.setItem(row, 0, name) - quantity = QtWidgets.QTableWidgetItem('1') + quantity = QTableWidgetItem('1') self._items_table.setItem(row, 1, quantity) def order_items(self): @@ -256,7 +265,7 @@ class DetailsDialog(QtWidgets.QDialog): for row in range(len(self.items)): text = self._items_table.item(row, 0).text() - quantity = int(self._items_table.item(row, 1).data(QtCore.Qt.DisplayRole)) + quantity = int(self._items_table.item(row, 1).data(Qt.DisplayRole)) order_list.append((text, max(0, quantity))) return order_list @@ -275,20 +284,17 @@ class DetailsDialog(QtWidgets.QDialog): self.accept() return - answer = QtWidgets.QMessageBox.warning(self, "Incomplete Form", + answer = QMessageBox.warning(self, "Incomplete Form", "The form does not contain all the necessary information.\n" "Do you want to discard it?", - QtWidgets.QMessageBox.Yes, QtWidgets.QMessageBox.No) + QMessageBox.Yes, QMessageBox.No) - if answer == QtWidgets.QMessageBox.Yes: + if answer == QMessageBox.Yes: self.reject() if __name__ == '__main__': - - import sys - - app = QtWidgets.QApplication(sys.argv) + app = QApplication(sys.argv) window = MainWindow() window.resize(640, 480) window.show() diff --git a/examples/widgets/tutorials/addressbook/part1.py b/examples/widgets/tutorials/addressbook/part1.py index 3aff8de19..cf8847b2c 100644 --- a/examples/widgets/tutorials/addressbook/part1.py +++ b/examples/widgets/tutorials/addressbook/part1.py @@ -40,23 +40,28 @@ ## ############################################################################# -from PySide6 import QtCore, QtWidgets +import sys +from PySide6.QtCore import Qt +from PySide6.QtWidgets import (QApplication, QGridLayout, + QLabel, QGridLayout, QLineEdit, QTextEdit, + QWidget) -class AddressBook(QtWidgets.QWidget): + +class AddressBook(QWidget): def __init__(self, parent=None): super(AddressBook, self).__init__(parent) - name_label = QtWidgets.QLabel("Name:") - self._name_line = QtWidgets.QLineEdit() + name_label = QLabel("Name:") + self._name_line = QLineEdit() - address_label = QtWidgets.QLabel("Address:") - self._address_text = QtWidgets.QTextEdit() + address_label = QLabel("Address:") + self._address_text = QTextEdit() - main_layout = QtWidgets.QGridLayout() + main_layout = QGridLayout() main_layout.addWidget(name_label, 0, 0) main_layout.addWidget(self._name_line, 0, 1) - main_layout.addWidget(address_label, 1, 0, QtCore.Qt.AlignTop) + main_layout.addWidget(address_label, 1, 0, Qt.AlignTop) main_layout.addWidget(self._address_text, 1, 1) self.setLayout(main_layout) @@ -64,9 +69,7 @@ class AddressBook(QtWidgets.QWidget): if __name__ == '__main__': - import sys - - app = QtWidgets.QApplication(sys.argv) + app = QApplication(sys.argv) address_book = AddressBook() address_book.show() diff --git a/examples/widgets/tutorials/addressbook/part2.py b/examples/widgets/tutorials/addressbook/part2.py index 84838d801..963c84b9a 100644 --- a/examples/widgets/tutorials/addressbook/part2.py +++ b/examples/widgets/tutorials/addressbook/part2.py @@ -2,7 +2,7 @@ ############################################################################# ## ## Copyright (C) 2013 Riverbank Computing Limited. -## Copyright (C) 2016 The Qt Company Ltd. +## Copyright (C) 2021 The Qt Company Ltd. ## Contact: http://www.qt.io/licensing/ ## ## This file is part of the Qt for Python examples of the Qt Toolkit. @@ -40,7 +40,13 @@ ## ############################################################################# -from PySide6 import QtCore, QtWidgets +import sys + +from PySide6.QtCore import Qt, Signal, Slot +from PySide6.QtWidgets import (QApplication, QDialog, QGridLayout, + QHBoxLayout, QLabel, QLineEdit, + QMessageBox, QPushButton, QTextEdit, + QVBoxLayout, QWidget) class SortedDict(dict): @@ -72,7 +78,7 @@ class SortedDict(dict): iterkeys = __iter__ -class AddressBook(QtWidgets.QWidget): +class AddressBook(QWidget): def __init__(self, parent=None): super(AddressBook, self).__init__(parent) @@ -80,34 +86,34 @@ class AddressBook(QtWidgets.QWidget): self._old_name = '' self._old_address = '' - name_label = QtWidgets.QLabel("Name:") - self._name_line = QtWidgets.QLineEdit() + name_label = QLabel("Name:") + self._name_line = QLineEdit() self._name_line.setReadOnly(True) - address_label = QtWidgets.QLabel("Address:") - self._address_text = QtWidgets.QTextEdit() + address_label = QLabel("Address:") + self._address_text = QTextEdit() self._address_text.setReadOnly(True) - self._add_button = QtWidgets.QPushButton("&Add") - self._submit_button = QtWidgets.QPushButton("&Submit") + self._add_button = QPushButton("&Add") + self._submit_button = QPushButton("&Submit") self._submit_button.hide() - self._cancel_button = QtWidgets.QPushButton("&Cancel") + self._cancel_button = QPushButton("&Cancel") self._cancel_button.hide() self._add_button.clicked.connect(self.add_contact) self._submit_button.clicked.connect(self.submit_contact) self._cancel_button.clicked.connect(self.cancel) - button_layout_1 = QtWidgets.QVBoxLayout() - button_layout_1.addWidget(self._add_button, QtCore.Qt.AlignTop) + button_layout_1 = QVBoxLayout() + button_layout_1.addWidget(self._add_button, Qt.AlignTop) button_layout_1.addWidget(self._submit_button) button_layout_1.addWidget(self._cancel_button) button_layout_1.addStretch() - main_layout = QtWidgets.QGridLayout() + main_layout = QGridLayout() main_layout.addWidget(name_label, 0, 0) main_layout.addWidget(self._name_line, 0, 1) - main_layout.addWidget(address_label, 1, 0, QtCore.Qt.AlignTop) + main_layout.addWidget(address_label, 1, 0, Qt.AlignTop) main_layout.addWidget(self._address_text, 1, 1) main_layout.addLayout(button_layout_1, 1, 2) @@ -122,7 +128,7 @@ class AddressBook(QtWidgets.QWidget): self._address_text.clear() self._name_line.setReadOnly(False) - self._name_line.setFocus(QtCore.Qt.OtherFocusReason) + self._name_line.setFocus(Qt.OtherFocusReason) self._address_text.setReadOnly(False) self._add_button.setEnabled(False) @@ -134,16 +140,16 @@ class AddressBook(QtWidgets.QWidget): address = self._address_text.toPlainText() if name == "" or address == "": - QtWidgets.QMessageBox.information(self, "Empty Field", + QMessageBox.information(self, "Empty Field", "Please enter a name and address.") return if name not in self.contacts: self.contacts[name] = address - QtWidgets.QMessageBox.information(self, "Add Successful", + QMessageBox.information(self, "Add Successful", f'"{name}" has been added to your address book.') else: - QtWidgets.QMessageBox.information(self, "Add Unsuccessful", + QMessageBox.information(self, "Add Unsuccessful", f'Sorry, "{name}" is already in your address book.') return @@ -170,9 +176,7 @@ class AddressBook(QtWidgets.QWidget): if __name__ == '__main__': - import sys - - app = QtWidgets.QApplication(sys.argv) + app = QApplication(sys.argv) address_book = AddressBook() address_book.show() diff --git a/examples/widgets/tutorials/addressbook/part3.py b/examples/widgets/tutorials/addressbook/part3.py index b93e2fe09..80e76f46e 100644 --- a/examples/widgets/tutorials/addressbook/part3.py +++ b/examples/widgets/tutorials/addressbook/part3.py @@ -40,7 +40,13 @@ ## ############################################################################# -from PySide6 import QtCore, QtWidgets +import sys + +from PySide6.QtCore import Qt, Signal, Slot +from PySide6.QtWidgets import (QApplication, QDialog, QGridLayout, + QHBoxLayout, QLabel, QLineEdit, + QMessageBox, QPushButton, QTextEdit, + QVBoxLayout, QWidget) class SortedDict(dict): @@ -72,7 +78,7 @@ class SortedDict(dict): iterkeys = __iter__ -class AddressBook(QtWidgets.QWidget): +class AddressBook(QWidget): def __init__(self, parent=None): super(AddressBook, self).__init__(parent) @@ -80,22 +86,22 @@ class AddressBook(QtWidgets.QWidget): self._old_name = '' self._old_address = '' - name_label = QtWidgets.QLabel("Name:") - self._name_line = QtWidgets.QLineEdit() + name_label = QLabel("Name:") + self._name_line = QLineEdit() self._name_line.setReadOnly(True) - address_label = QtWidgets.QLabel("Address:") - self._address_text = QtWidgets.QTextEdit() + address_label = QLabel("Address:") + self._address_text = QTextEdit() self._address_text.setReadOnly(True) - self._add_button = QtWidgets.QPushButton("&Add") - self._submit_button = QtWidgets.QPushButton("&Submit") + self._add_button = QPushButton("&Add") + self._submit_button = QPushButton("&Submit") self._submit_button.hide() - self._cancel_button = QtWidgets.QPushButton("&Cancel") + self._cancel_button = QPushButton("&Cancel") self._cancel_button.hide() - self._next_button = QtWidgets.QPushButton("&Next") + self._next_button = QPushButton("&Next") self._next_button.setEnabled(False) - self._previous_button = QtWidgets.QPushButton("&Previous") + self._previous_button = QPushButton("&Previous") self._previous_button.setEnabled(False) self._add_button.clicked.connect(self.add_contact) @@ -104,20 +110,20 @@ class AddressBook(QtWidgets.QWidget): self._next_button.clicked.connect(self.next) self._previous_button.clicked.connect(self.previous) - button_layout_1 = QtWidgets.QVBoxLayout() - button_layout_1.addWidget(self._add_button, QtCore.Qt.AlignTop) + button_layout_1 = QVBoxLayout() + button_layout_1.addWidget(self._add_button, Qt.AlignTop) button_layout_1.addWidget(self._submit_button) button_layout_1.addWidget(self._cancel_button) button_layout_1.addStretch() - button_layout_2 = QtWidgets.QHBoxLayout() + button_layout_2 = QHBoxLayout() button_layout_2.addWidget(self._previous_button) button_layout_2.addWidget(self._next_button) - main_layout = QtWidgets.QGridLayout() + main_layout = QGridLayout() main_layout.addWidget(name_label, 0, 0) main_layout.addWidget(self._name_line, 0, 1) - main_layout.addWidget(address_label, 1, 0, QtCore.Qt.AlignTop) + main_layout.addWidget(address_label, 1, 0, Qt.AlignTop) main_layout.addWidget(self._address_text, 1, 1) main_layout.addLayout(button_layout_1, 1, 2) main_layout.addLayout(button_layout_2, 3, 1) @@ -133,7 +139,7 @@ class AddressBook(QtWidgets.QWidget): self._address_text.clear() self._name_line.setReadOnly(False) - self._name_line.setFocus(QtCore.Qt.OtherFocusReason) + self._name_line.setFocus(Qt.OtherFocusReason) self._address_text.setReadOnly(False) self._add_button.setEnabled(False) @@ -147,16 +153,16 @@ class AddressBook(QtWidgets.QWidget): address = self._address_text.toPlainText() if name == "" or address == "": - QtWidgets.QMessageBox.information(self, "Empty Field", + QMessageBox.information(self, "Empty Field", "Please enter a name and address.") return if name not in self.contacts: self.contacts[name] = address - QtWidgets.QMessageBox.information(self, "Add Successful", + QMessageBox.information(self, "Add Successful", f'"{name}" has been added to your address book.') else: - QtWidgets.QMessageBox.information(self, "Add Unsuccessful", + QMessageBox.information(self, "Add Unsuccessful", f'Sorry, "{name}" is already in your address book.') return @@ -235,9 +241,7 @@ class AddressBook(QtWidgets.QWidget): if __name__ == '__main__': - import sys - - app = QtWidgets.QApplication(sys.argv) + app = QApplication(sys.argv) address_book = AddressBook() address_book.show() diff --git a/examples/widgets/tutorials/addressbook/part4.py b/examples/widgets/tutorials/addressbook/part4.py index 8ae9d6b7b..389b2e47c 100644 --- a/examples/widgets/tutorials/addressbook/part4.py +++ b/examples/widgets/tutorials/addressbook/part4.py @@ -2,7 +2,7 @@ ############################################################################# ## ## Copyright (C) 2013 Riverbank Computing Limited. -## Copyright (C) 2016 The Qt Company Ltd. +## Copyright (C) 2021 The Qt Company Ltd. ## Contact: http://www.qt.io/licensing/ ## ## This file is part of the Qt for Python examples of the Qt Toolkit. @@ -40,7 +40,13 @@ ## ############################################################################# -from PySide6 import QtCore, QtWidgets +import sys + +from PySide6.QtCore import Qt, Signal, Slot +from PySide6.QtWidgets import (QApplication, QDialog, QGridLayout, + QHBoxLayout, QLabel, QLineEdit, + QMessageBox, QPushButton, QTextEdit, + QVBoxLayout, QWidget) class SortedDict(dict): @@ -72,7 +78,7 @@ class SortedDict(dict): iterkeys = __iter__ -class AddressBook(QtWidgets.QWidget): +class AddressBook(QWidget): NavigationMode, AddingMode, EditingMode = range(3) def __init__(self, parent=None): @@ -83,27 +89,27 @@ class AddressBook(QtWidgets.QWidget): self._old_address = '' self._current_mode = self.NavigationMode - name_label = QtWidgets.QLabel("Name:") - self._name_line = QtWidgets.QLineEdit() + name_label = QLabel("Name:") + self._name_line = QLineEdit() self._name_line.setReadOnly(True) - address_label = QtWidgets.QLabel("Address:") - self._address_text = QtWidgets.QTextEdit() + address_label = QLabel("Address:") + self._address_text = QTextEdit() self._address_text.setReadOnly(True) - self._add_button = QtWidgets.QPushButton("&Add") - self._edit_button = QtWidgets.QPushButton("&Edit") + self._add_button = QPushButton("&Add") + self._edit_button = QPushButton("&Edit") self._edit_button.setEnabled(False) - self._remove_button = QtWidgets.QPushButton("&Remove") + self._remove_button = QPushButton("&Remove") self._remove_button.setEnabled(False) - self._submit_button = QtWidgets.QPushButton("&Submit") + self._submit_button = QPushButton("&Submit") self._submit_button.hide() - self._cancel_button = QtWidgets.QPushButton("&Cancel") + self._cancel_button = QPushButton("&Cancel") self._cancel_button.hide() - self._next_button = QtWidgets.QPushButton("&Next") + self._next_button = QPushButton("&Next") self._next_button.setEnabled(False) - self._previous_button = QtWidgets.QPushButton("&Previous") + self._previous_button = QPushButton("&Previous") self._previous_button.setEnabled(False) self._add_button.clicked.connect(self.add_contact) @@ -114,7 +120,7 @@ class AddressBook(QtWidgets.QWidget): self._next_button.clicked.connect(self.next) self._previous_button.clicked.connect(self.previous) - button_layout_1 = QtWidgets.QVBoxLayout() + button_layout_1 = QVBoxLayout() button_layout_1.addWidget(self._add_button) button_layout_1.addWidget(self._edit_button) button_layout_1.addWidget(self._remove_button) @@ -122,14 +128,14 @@ class AddressBook(QtWidgets.QWidget): button_layout_1.addWidget(self._cancel_button) button_layout_1.addStretch() - button_layout_2 = QtWidgets.QHBoxLayout() + button_layout_2 = QHBoxLayout() button_layout_2.addWidget(self._previous_button) button_layout_2.addWidget(self._next_button) - main_layout = QtWidgets.QGridLayout() + main_layout = QGridLayout() main_layout.addWidget(name_label, 0, 0) main_layout.addWidget(self._name_line, 0, 1) - main_layout.addWidget(address_label, 1, 0, QtCore.Qt.AlignTop) + main_layout.addWidget(address_label, 1, 0, Qt.AlignTop) main_layout.addWidget(self._address_text, 1, 1) main_layout.addLayout(button_layout_1, 1, 2) main_layout.addLayout(button_layout_2, 3, 1) @@ -157,33 +163,33 @@ class AddressBook(QtWidgets.QWidget): address = self._address_text.toPlainText() if name == "" or address == "": - QtWidgets.QMessageBox.information(self, "Empty Field", + QMessageBox.information(self, "Empty Field", "Please enter a name and address.") return if self._current_mode == self.AddingMode: if name not in self.contacts: self.contacts[name] = address - QtWidgets.QMessageBox.information(self, "Add Successful", + QMessageBox.information(self, "Add Successful", f'"{name}" has been added to your address book.') else: - QtWidgets.QMessageBox.information(self, "Add Unsuccessful", + QMessageBox.information(self, "Add Unsuccessful", f'Sorry, "{name}" is already in your address book.') return elif self._current_mode == self.EditingMode: if self._old_name != name: if name not in self.contacts: - QtWidgets.QMessageBox.information(self, "Edit Successful", + QMessageBox.information(self, "Edit Successful", f'"{self.oldName}" has been edited in your address book.') del self.contacts[self._old_name] self.contacts[name] = address else: - QtWidgets.QMessageBox.information(self, "Edit Unsuccessful", + QMessageBox.information(self, "Edit Unsuccessful", f'Sorry, "{name}" is already in your address book.') return elif self._old_address != address: - QtWidgets.QMessageBox.information(self, "Edit Successful", + QMessageBox.information(self, "Edit Successful", f'"{name}" has been edited in your address book.') self.contacts[name] = address @@ -199,15 +205,15 @@ class AddressBook(QtWidgets.QWidget): address = self._address_text.toPlainText() if name in self.contacts: - button = QtWidgets.QMessageBox.question(self, "Confirm Remove", + button = QMessageBox.question(self, "Confirm Remove", f'Are you sure you want to remove "{name}"?', - QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No) + QMessageBox.Yes | QMessageBox.No) - if button == QtWidgets.QMessageBox.Yes: + if button == QMessageBox.Yes: self.previous() del self.contacts[name] - QtWidgets.QMessageBox.information(self, "Remove Successful", + QMessageBox.information(self, "Remove Successful", f'"{name}" has been removed from your address book.') self.update_interface(self.NavigationMode) @@ -256,7 +262,7 @@ class AddressBook(QtWidgets.QWidget): if self._current_mode in (self.AddingMode, self.EditingMode): self._name_line.setReadOnly(False) - self._name_line.setFocus(QtCore.Qt.OtherFocusReason) + self._name_line.setFocus(Qt.OtherFocusReason) self._address_text.setReadOnly(False) self._add_button.setEnabled(False) @@ -289,9 +295,7 @@ class AddressBook(QtWidgets.QWidget): if __name__ == '__main__': - import sys - - app = QtWidgets.QApplication(sys.argv) + app = QApplication(sys.argv) address_book = AddressBook() address_book.show() diff --git a/examples/widgets/tutorials/addressbook/part5.py b/examples/widgets/tutorials/addressbook/part5.py index 297cf97d2..c01821417 100644 --- a/examples/widgets/tutorials/addressbook/part5.py +++ b/examples/widgets/tutorials/addressbook/part5.py @@ -2,7 +2,7 @@ ############################################################################# ## ## Copyright (C) 2013 Riverbank Computing Limited. -## Copyright (C) 2016 The Qt Company Ltd. +## Copyright (C) 2021 The Qt Company Ltd. ## Contact: http://www.qt.io/licensing/ ## ## This file is part of the Qt for Python examples of the Qt Toolkit. @@ -40,7 +40,14 @@ ## ############################################################################# -from PySide6 import QtCore, QtWidgets +import pickle +import sys + +from PySide6.QtCore import Qt, Signal, Slot +from PySide6.QtWidgets import (QApplication, QDialog, QGridLayout, + QHBoxLayout, QLabel, QLineEdit, + QMessageBox, QPushButton, QTextEdit, + QVBoxLayout, QWidget) class SortedDict(dict): @@ -72,7 +79,7 @@ class SortedDict(dict): iterkeys = __iter__ -class AddressBook(QtWidgets.QWidget): +class AddressBook(QWidget): NavigationMode, AddingMode, EditingMode = range(3) def __init__(self, parent=None): @@ -83,29 +90,29 @@ class AddressBook(QtWidgets.QWidget): self._old_address = '' self._current_mode = self.NavigationMode - name_label = QtWidgets.QLabel("Name:") - self._name_line = QtWidgets.QLineEdit() + name_label = QLabel("Name:") + self._name_line = QLineEdit() self._name_line.setReadOnly(True) - address_label = QtWidgets.QLabel("Address:") - self._address_text = QtWidgets.QTextEdit() + address_label = QLabel("Address:") + self._address_text = QTextEdit() self._address_text.setReadOnly(True) - self._add_button = QtWidgets.QPushButton("&Add") - self._edit_button = QtWidgets.QPushButton("&Edit") + self._add_button = QPushButton("&Add") + self._edit_button = QPushButton("&Edit") self._edit_button.setEnabled(False) - self._remove_button = QtWidgets.QPushButton("&Remove") + self._remove_button = QPushButton("&Remove") self._remove_button.setEnabled(False) - self._find_button = QtWidgets.QPushButton("&Find") + self._find_button = QPushButton("&Find") self._find_button.setEnabled(False) - self._submit_button = QtWidgets.QPushButton("&Submit") + self._submit_button = QPushButton("&Submit") self._submit_button.hide() - self._cancel_button = QtWidgets.QPushButton("&Cancel") + self._cancel_button = QPushButton("&Cancel") self._cancel_button.hide() - self._next_button = QtWidgets.QPushButton("&Next") + self._next_button = QPushButton("&Next") self._next_button.setEnabled(False) - self._previous_button = QtWidgets.QPushButton("&Previous") + self._previous_button = QPushButton("&Previous") self._previous_button.setEnabled(False) self.dialog = FindDialog() @@ -119,7 +126,7 @@ class AddressBook(QtWidgets.QWidget): self._next_button.clicked.connect(self.next) self._previous_button.clicked.connect(self.previous) - button_layout_1 = QtWidgets.QVBoxLayout() + button_layout_1 = QVBoxLayout() button_layout_1.addWidget(self._add_button) button_layout_1.addWidget(self._edit_button) button_layout_1.addWidget(self._remove_button) @@ -128,14 +135,14 @@ class AddressBook(QtWidgets.QWidget): button_layout_1.addWidget(self._cancel_button) button_layout_1.addStretch() - button_layout_2 = QtWidgets.QHBoxLayout() + button_layout_2 = QHBoxLayout() button_layout_2.addWidget(self._previous_button) button_layout_2.addWidget(self._next_button) - main_layout = QtWidgets.QGridLayout() + main_layout = QGridLayout() main_layout.addWidget(name_label, 0, 0) main_layout.addWidget(self._name_line, 0, 1) - main_layout.addWidget(address_label, 1, 0, QtCore.Qt.AlignTop) + main_layout.addWidget(address_label, 1, 0, Qt.AlignTop) main_layout.addWidget(self._address_text, 1, 1) main_layout.addLayout(button_layout_1, 1, 2) main_layout.addLayout(button_layout_2, 2, 1) @@ -163,33 +170,33 @@ class AddressBook(QtWidgets.QWidget): address = self._address_text.toPlainText() if name == "" or address == "": - QtWidgets.QMessageBox.information(self, "Empty Field", + QMessageBox.information(self, "Empty Field", "Please enter a name and address.") return if self._current_mode == self.AddingMode: if name not in self.contacts: self.contacts[name] = address - QtWidgets.QMessageBox.information(self, "Add Successful", + QMessageBox.information(self, "Add Successful", f'"{name}" has been added to your address book.') else: - QtWidgets.QMessageBox.information(self, "Add Unsuccessful", + QMessageBox.information(self, "Add Unsuccessful", f'Sorry, "{name}" is already in your address book.') return elif self._current_mode == self.EditingMode: if self._old_name != name: if name not in self.contacts: - QtWidgets.QMessageBox.information(self, "Edit Successful", + QMessageBox.information(self, "Edit Successful", f'"{self.oldName}" has been edited in your address book.') del self.contacts[self._old_name] self.contacts[name] = address else: - QtWidgets.QMessageBox.information(self, "Edit Unsuccessful", + QMessageBox.information(self, "Edit Unsuccessful", f'Sorry, "{name}" is already in your address book.') return elif self._old_address != address: - QtWidgets.QMessageBox.information(self, "Edit Successful", + QMessageBox.information(self, "Edit Successful", f'"{name}" has been edited in your address book.') self.contacts[name] = address @@ -205,15 +212,15 @@ class AddressBook(QtWidgets.QWidget): address = self._address_text.toPlainText() if name in self.contacts: - button = QtWidgets.QMessageBox.question(self, "Confirm Remove", + button = QMessageBox.question(self, "Confirm Remove", f'Are you sure you want to remove "{name}"?', - QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No) + QMessageBox.Yes | QMessageBox.No) - if button == QtWidgets.QMessageBox.Yes: + if button == QMessageBox.Yes: self.previous() del self.contacts[name] - QtWidgets.QMessageBox.information(self, "Remove Successful", + QMessageBox.information(self, "Remove Successful", f'"{name}" has been removed from your address book.') self.update_interface(self.NavigationMode) @@ -260,15 +267,15 @@ class AddressBook(QtWidgets.QWidget): def find_contact(self): self.dialog.show() - if self.dialog.exec_() == QtWidgets.QDialog.Accepted: + if self.dialog.exec_() == QDialog.Accepted: contact_name = self.dialog.get_find_text() if contact_name in self.contacts: self._name_line.setText(contact_name) self._address_text.setText(self.contacts[contact_name]) else: - QtWidgets.QMessageBox.information(self, "Contact Not Found", - f'Sorry, "{ccontact_nameontactName}" is not in your address book.') + QMessageBox.information(self, "Contact Not Found", + f'Sorry, "{contact_name}" is not in your address book.') return self.update_interface(self.NavigationMode) @@ -278,7 +285,7 @@ class AddressBook(QtWidgets.QWidget): if self._current_mode in (self.AddingMode, self.EditingMode): self._name_line.setReadOnly(False) - self._name_line.setFocus(QtCore.Qt.OtherFocusReason) + self._name_line.setFocus(Qt.OtherFocusReason) self._address_text.setReadOnly(False) self._add_button.setEnabled(False) @@ -311,17 +318,17 @@ class AddressBook(QtWidgets.QWidget): self._cancel_button.hide() -class FindDialog(QtWidgets.QDialog): +class FindDialog(QDialog): def __init__(self, parent=None): super(FindDialog, self).__init__(parent) - find_label = QtWidgets.QLabel("Enter the name of a contact:") - self._line_edit = QtWidgets.QLineEdit() + find_label = QLabel("Enter the name of a contact:") + self._line_edit = QLineEdit() - self._find_button = QtWidgets.QPushButton("&Find") + self._find_button = QPushButton("&Find") self._find_text = '' - layout = QtWidgets.QHBoxLayout() + layout = QHBoxLayout() layout.addWidget(find_label) layout.addWidget(self._line_edit) layout.addWidget(self._find_button) @@ -336,7 +343,7 @@ class FindDialog(QtWidgets.QDialog): text = self._line_edit.text() if not text: - QtWidgets.QMessageBox.information(self, "Empty Field", + QMessageBox.information(self, "Empty Field", "Please enter a name.") return else: @@ -349,9 +356,7 @@ class FindDialog(QtWidgets.QDialog): if __name__ == '__main__': - import sys - - app = QtWidgets.QApplication(sys.argv) + app = QApplication(sys.argv) address_book = AddressBook() address_book.show() diff --git a/examples/widgets/tutorials/addressbook/part6.py b/examples/widgets/tutorials/addressbook/part6.py index f77bd2136..2bf694672 100644 --- a/examples/widgets/tutorials/addressbook/part6.py +++ b/examples/widgets/tutorials/addressbook/part6.py @@ -2,7 +2,7 @@ ############################################################################# ## ## Copyright (C) 2013 Riverbank Computing Limited. -## Copyright (C) 2016 The Qt Company Ltd. +## Copyright (C) 2021 The Qt Company Ltd. ## Contact: http://www.qt.io/licensing/ ## ## This file is part of the Qt for Python examples of the Qt Toolkit. @@ -41,8 +41,13 @@ ############################################################################# import pickle +import sys -from PySide6 import QtCore, QtWidgets +from PySide6.QtCore import QFile, QIODevice, QTextStream, Qt, Signal, Slot +from PySide6.QtWidgets import (QApplication, QDialog, QFileDialog, + QGridLayout, QHBoxLayout, QLabel, QLineEdit, + QMessageBox, QPushButton, QTextEdit, + QVBoxLayout, QWidget) class SortedDict(dict): @@ -74,7 +79,7 @@ class SortedDict(dict): iterkeys = __iter__ -class AddressBook(QtWidgets.QWidget): +class AddressBook(QWidget): NavigationMode, AddingMode, EditingMode = range(3) def __init__(self, parent=None): @@ -85,34 +90,34 @@ class AddressBook(QtWidgets.QWidget): self._old_address = '' self._current_mode = self.NavigationMode - name_label = QtWidgets.QLabel("Name:") - self._name_line = QtWidgets.QLineEdit() + name_label = QLabel("Name:") + self._name_line = QLineEdit() self._name_line.setReadOnly(True) - address_label = QtWidgets.QLabel("Address:") - self._address_text = QtWidgets.QTextEdit() + address_label = QLabel("Address:") + self._address_text = QTextEdit() self._address_text.setReadOnly(True) - self._add_button = QtWidgets.QPushButton("&Add") - self._edit_button = QtWidgets.QPushButton("&Edit") + self._add_button = QPushButton("&Add") + self._edit_button = QPushButton("&Edit") self._edit_button.setEnabled(False) - self._remove_button = QtWidgets.QPushButton("&Remove") + self._remove_button = QPushButton("&Remove") self._remove_button.setEnabled(False) - self._find_button = QtWidgets.QPushButton("&Find") + self._find_button = QPushButton("&Find") self._find_button.setEnabled(False) - self._submit_button = QtWidgets.QPushButton("&Submit") + self._submit_button = QPushButton("&Submit") self._submit_button.hide() - self._cancel_button = QtWidgets.QPushButton("&Cancel") + self._cancel_button = QPushButton("&Cancel") self._cancel_button.hide() - self._next_button = QtWidgets.QPushButton("&Next") + self._next_button = QPushButton("&Next") self._next_button.setEnabled(False) - self._previous_button = QtWidgets.QPushButton("&Previous") + self._previous_button = QPushButton("&Previous") self._previous_button.setEnabled(False) - self._load_button = QtWidgets.QPushButton("&Load...") + self._load_button = QPushButton("&Load...") self._load_button.setToolTip("Load contacts from a file") - self._save_button = QtWidgets.QPushButton("Sa&ve...") + self._save_button = QPushButton("Sa&ve...") self._save_button.setToolTip("Save contacts to a file") self._save_button.setEnabled(False) @@ -129,7 +134,7 @@ class AddressBook(QtWidgets.QWidget): self._load_button.clicked.connect(self.load_from_file) self._save_button.clicked.connect(self.save_to_file) - button_layout_1 = QtWidgets.QVBoxLayout() + button_layout_1 = QVBoxLayout() button_layout_1.addWidget(self._add_button) button_layout_1.addWidget(self._edit_button) button_layout_1.addWidget(self._remove_button) @@ -140,14 +145,14 @@ class AddressBook(QtWidgets.QWidget): button_layout_1.addWidget(self._save_button) button_layout_1.addStretch() - button_layout_2 = QtWidgets.QHBoxLayout() + button_layout_2 = QHBoxLayout() button_layout_2.addWidget(self._previous_button) button_layout_2.addWidget(self._next_button) - main_layout = QtWidgets.QGridLayout() + main_layout = QGridLayout() main_layout.addWidget(name_label, 0, 0) main_layout.addWidget(self._name_line, 0, 1) - main_layout.addWidget(address_label, 1, 0, QtCore.Qt.AlignTop) + main_layout.addWidget(address_label, 1, 0, Qt.AlignTop) main_layout.addWidget(self._address_text, 1, 1) main_layout.addLayout(button_layout_1, 1, 2) main_layout.addLayout(button_layout_2, 2, 1) @@ -175,33 +180,33 @@ class AddressBook(QtWidgets.QWidget): address = self._address_text.toPlainText() if name == "" or address == "": - QtWidgets.QMessageBox.information(self, "Empty Field", + QMessageBox.information(self, "Empty Field", "Please enter a name and address.") return if self._current_mode == self.AddingMode: if name not in self.contacts: self.contacts[name] = address - QtWidgets.QMessageBox.information(self, "Add Successful", + QMessageBox.information(self, "Add Successful", f'"{name}" has been added to your address book.') else: - QtWidgets.QMessageBox.information(self, "Add Unsuccessful", + QMessageBox.information(self, "Add Unsuccessful", f'Sorry, "{name}" is already in your address book.') return elif self._current_mode == self.EditingMode: if self._old_name != name: if name not in self.contacts: - QtWidgets.QMessageBox.information(self, "Edit Successful", + QMessageBox.information(self, "Edit Successful", f'"{self.oldName}" has been edited in your address book.') del self.contacts[self._old_name] self.contacts[name] = address else: - QtWidgets.QMessageBox.information(self, "Edit Unsuccessful", + QMessageBox.information(self, "Edit Unsuccessful", f'Sorry, "{name}" is already in your address book.') return elif self._old_address != address: - QtWidgets.QMessageBox.information(self, "Edit Successful", + QMessageBox.information(self, "Edit Successful", f'"{name}" has been edited in your address book.') self.contacts[name] = address @@ -217,15 +222,15 @@ class AddressBook(QtWidgets.QWidget): address = self._address_text.toPlainText() if name in self.contacts: - button = QtWidgets.QMessageBox.question(self, "Confirm Remove", + button = QMessageBox.question(self, "Confirm Remove", f'Are you sure you want to remove "{name}"?', - QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No) + QMessageBox.Yes | QMessageBox.No) - if button == QtWidgets.QMessageBox.Yes: + if button == QMessageBox.Yes: self.previous() del self.contacts[name] - QtWidgets.QMessageBox.information(self, "Remove Successful", + QMessageBox.information(self, "Remove Successful", f'"{name}" has been removed from your address book.') self.update_interface(self.NavigationMode) @@ -272,15 +277,15 @@ class AddressBook(QtWidgets.QWidget): def find_contact(self): self.dialog.show() - if self.dialog.exec_() == QtWidgets.QDialog.Accepted: + if self.dialog.exec_() == QDialog.Accepted: contact_name = self.dialog.get_find_text() if contact_name in self.contacts: self._name_line.setText(contact_name) self._address_text.setText(self.contacts[contact_name]) else: - QtWidgets.QMessageBox.information(self, "Contact Not Found", - f'Sorry, "{ccontact_nameontactName}" is not in your address book.') + QMessageBox.information(self, "Contact Not Found", + f'Sorry, "{contact_name}" is not in your address book.') return self.update_interface(self.NavigationMode) @@ -290,7 +295,7 @@ class AddressBook(QtWidgets.QWidget): if self._current_mode in (self.AddingMode, self.EditingMode): self._name_line.setReadOnly(False) - self._name_line.setFocus(QtCore.Qt.OtherFocusReason) + self._name_line.setFocus(Qt.OtherFocusReason) self._address_text.setReadOnly(False) self._add_button.setEnabled(False) @@ -329,7 +334,7 @@ class AddressBook(QtWidgets.QWidget): self._save_button.setEnabled(number >= 1) def save_to_file(self): - fileName,_ = QtWidgets.QFileDialog.getSaveFileName(self, + fileName,_ = QFileDialog.getSaveFileName(self, "Save Address Book", '', "Address Book (*.abk);;All Files (*)") @@ -339,7 +344,7 @@ class AddressBook(QtWidgets.QWidget): try: out_file = open(str(fileName), 'wb') except IOError: - QtWidgets.QMessageBox.information(self, "Unable to open file", + QMessageBox.information(self, "Unable to open file", f'There was an error opening "{fileName}"') return @@ -347,7 +352,7 @@ class AddressBook(QtWidgets.QWidget): out_file.close() def load_from_file(self): - fileName,_ = QtWidgets.QFileDialog.getOpenFileName(self, + fileName,_ = QFileDialog.getOpenFileName(self, "Open Address Book", '', "Address Book (*.abk);;All Files (*)") @@ -357,7 +362,7 @@ class AddressBook(QtWidgets.QWidget): try: in_file = open(str(fileName), 'rb') except IOError: - QtWidgets.QMessageBox.information(self, "Unable to open file", + QMessageBox.information(self, "Unable to open file", f'There was an error opening "{fileName}"') return @@ -365,7 +370,7 @@ class AddressBook(QtWidgets.QWidget): in_file.close() if len(self.contacts) == 0: - QtWidgets.QMessageBox.information(self, "No contacts in file", + QMessageBox.information(self, "No contacts in file", "The file you are attempting to open contains no " "contacts.") else: @@ -376,17 +381,17 @@ class AddressBook(QtWidgets.QWidget): self.update_interface(self.NavigationMode) -class FindDialog(QtWidgets.QDialog): +class FindDialog(QDialog): def __init__(self, parent=None): super(FindDialog, self).__init__(parent) - find_label = QtWidgets.QLabel("Enter the name of a contact:") - self._line_edit = QtWidgets.QLineEdit() + find_label = QLabel("Enter the name of a contact:") + self._line_edit = QLineEdit() - self._find_button = QtWidgets.QPushButton("&Find") + self._find_button = QPushButton("&Find") self._find_text = '' - layout = QtWidgets.QHBoxLayout() + layout = QHBoxLayout() layout.addWidget(find_label) layout.addWidget(self._line_edit) layout.addWidget(self._find_button) @@ -401,7 +406,7 @@ class FindDialog(QtWidgets.QDialog): text = self._line_edit.text() if not text: - QtWidgets.QMessageBox.information(self, "Empty Field", + QMessageBox.information(self, "Empty Field", "Please enter a name.") return @@ -414,9 +419,7 @@ class FindDialog(QtWidgets.QDialog): if __name__ == '__main__': - import sys - - app = QtWidgets.QApplication(sys.argv) + app = QApplication(sys.argv) address_book = AddressBook() address_book.show() diff --git a/examples/widgets/tutorials/addressbook/part7.py b/examples/widgets/tutorials/addressbook/part7.py index f494837a6..8378030ce 100644 --- a/examples/widgets/tutorials/addressbook/part7.py +++ b/examples/widgets/tutorials/addressbook/part7.py @@ -2,7 +2,7 @@ ############################################################################# ## ## Copyright (C) 2013 Riverbank Computing Limited. -## Copyright (C) 2016 The Qt Company Ltd. +## Copyright (C) 2021 The Qt Company Ltd. ## Contact: http://www.qt.io/licensing/ ## ## This file is part of the Qt for Python examples of the Qt Toolkit. @@ -41,8 +41,13 @@ ############################################################################# import pickle +import sys -from PySide6 import QtCore, QtWidgets +from PySide6.QtCore import QFile, QIODevice, QTextStream, Qt, Signal, Slot +from PySide6.QtWidgets import (QApplication, QDialog, QFileDialog, + QGridLayout, QHBoxLayout, QLabel, QLineEdit, + QMessageBox, QPushButton, QTextEdit, + QVBoxLayout, QWidget) class SortedDict(dict): @@ -74,7 +79,7 @@ class SortedDict(dict): iterkeys = __iter__ -class AddressBook(QtWidgets.QWidget): +class AddressBook(QWidget): NavigationMode, AddingMode, EditingMode = range(3) def __init__(self, parent=None): @@ -85,38 +90,38 @@ class AddressBook(QtWidgets.QWidget): self._old_address = '' self._current_mode = self.NavigationMode - name_label = QtWidgets.QLabel("Name:") - self._name_line = QtWidgets.QLineEdit() + name_label = QLabel("Name:") + self._name_line = QLineEdit() self._name_line.setReadOnly(True) - address_label = QtWidgets.QLabel("Address:") - self._address_text = QtWidgets.QTextEdit() + address_label = QLabel("Address:") + self._address_text = QTextEdit() self._address_text.setReadOnly(True) - self._add_button = QtWidgets.QPushButton("&Add") - self._edit_button = QtWidgets.QPushButton("&Edit") + self._add_button = QPushButton("&Add") + self._edit_button = QPushButton("&Edit") self._edit_button.setEnabled(False) - self._remove_button = QtWidgets.QPushButton("&Remove") + self._remove_button = QPushButton("&Remove") self._remove_button.setEnabled(False) - self._find_button = QtWidgets.QPushButton("&Find") + self._find_button = QPushButton("&Find") self._find_button.setEnabled(False) - self._submit_button = QtWidgets.QPushButton("&Submit") + self._submit_button = QPushButton("&Submit") self._submit_button.hide() - self._cancel_button = QtWidgets.QPushButton("&Cancel") + self._cancel_button = QPushButton("&Cancel") self._cancel_button.hide() - self._next_button = QtWidgets.QPushButton("&Next") + self._next_button = QPushButton("&Next") self._next_button.setEnabled(False) - self._previous_button = QtWidgets.QPushButton("&Previous") + self._previous_button = QPushButton("&Previous") self._previous_button.setEnabled(False) - self._load_button = QtWidgets.QPushButton("&Load...") + self._load_button = QPushButton("&Load...") self._load_button.setToolTip("Load contacts from a file") - self._save_button = QtWidgets.QPushButton("Sa&ve...") + self._save_button = QPushButton("Sa&ve...") self._save_button.setToolTip("Save contacts to a file") self._save_button.setEnabled(False) - self._export_button = QtWidgets.QPushButton("Ex&port") + self._export_button = QPushButton("Ex&port") self._export_button.setToolTip("Export as vCard") self._export_button.setEnabled(False) @@ -134,7 +139,7 @@ class AddressBook(QtWidgets.QWidget): self._save_button.clicked.connect(self.save_to_file) self._export_button.clicked.connect(self.export_as_vcard) - button_layout_1 = QtWidgets.QVBoxLayout() + button_layout_1 = QVBoxLayout() button_layout_1.addWidget(self._add_button) button_layout_1.addWidget(self._edit_button) button_layout_1.addWidget(self._remove_button) @@ -146,14 +151,14 @@ class AddressBook(QtWidgets.QWidget): button_layout_1.addWidget(self._export_button) button_layout_1.addStretch() - button_layout_2 = QtWidgets.QHBoxLayout() + button_layout_2 = QHBoxLayout() button_layout_2.addWidget(self._previous_button) button_layout_2.addWidget(self._next_button) - main_layout = QtWidgets.QGridLayout() + main_layout = QGridLayout() main_layout.addWidget(name_label, 0, 0) main_layout.addWidget(self._name_line, 0, 1) - main_layout.addWidget(address_label, 1, 0, QtCore.Qt.AlignTop) + main_layout.addWidget(address_label, 1, 0, Qt.AlignTop) main_layout.addWidget(self._address_text, 1, 1) main_layout.addLayout(button_layout_1, 1, 2) main_layout.addLayout(button_layout_2, 2, 1) @@ -181,33 +186,33 @@ class AddressBook(QtWidgets.QWidget): address = self._address_text.toPlainText() if name == "" or address == "": - QtWidgets.QMessageBox.information(self, "Empty Field", + QMessageBox.information(self, "Empty Field", "Please enter a name and address.") return if self._current_mode == self.AddingMode: if name not in self.contacts: self.contacts[name] = address - QtWidgets.QMessageBox.information(self, "Add Successful", + QMessageBox.information(self, "Add Successful", f'"{name}" has been added to your address book.') else: - QtWidgets.QMessageBox.information(self, "Add Unsuccessful", + QMessageBox.information(self, "Add Unsuccessful", f'Sorry, "{name}" is already in your address book.') return elif self._current_mode == self.EditingMode: if self._old_name != name: if name not in self.contacts: - QtWidgets.QMessageBox.information(self, "Edit Successful", + QMessageBox.information(self, "Edit Successful", f'"{self.oldName}" has been edited in your address book.') del self.contacts[self._old_name] self.contacts[name] = address else: - QtWidgets.QMessageBox.information(self, "Edit Unsuccessful", + QMessageBox.information(self, "Edit Unsuccessful", f'Sorry, "{name}" is already in your address book.') return elif self._old_address != address: - QtWidgets.QMessageBox.information(self, "Edit Successful", + QMessageBox.information(self, "Edit Successful", f'"{name}" has been edited in your address book.') self.contacts[name] = address @@ -223,15 +228,15 @@ class AddressBook(QtWidgets.QWidget): address = self._address_text.toPlainText() if name in self.contacts: - button = QtWidgets.QMessageBox.question(self, "Confirm Remove", + button = QMessageBox.question(self, "Confirm Remove", f'Are you sure you want to remove "{name}"?', - QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No) + QMessageBox.Yes | QMessageBox.No) - if button == QtWidgets.QMessageBox.Yes: + if button == QMessageBox.Yes: self.previous() del self.contacts[name] - QtWidgets.QMessageBox.information(self, "Remove Successful", + QMessageBox.information(self, "Remove Successful", f'"{name}" has been removed from your address book.') self.update_interface(self.NavigationMode) @@ -278,15 +283,15 @@ class AddressBook(QtWidgets.QWidget): def find_contact(self): self.dialog.show() - if self.dialog.exec_() == QtWidgets.QDialog.Accepted: + if self.dialog.exec_() == QDialog.Accepted: contact_name = self.dialog.get_find_text() if contact_name in self.contacts: self._name_line.setText(contact_name) self._address_text.setText(self.contacts[contact_name]) else: - QtWidgets.QMessageBox.information(self, "Contact Not Found", - f'Sorry, "{ccontact_nameontactName}" is not in your address book.') + QMessageBox.information(self, "Contact Not Found", + f'Sorry, "{contact_name}" is not in your address book.') return self.update_interface(self.NavigationMode) @@ -296,7 +301,7 @@ class AddressBook(QtWidgets.QWidget): if self._current_mode in (self.AddingMode, self.EditingMode): self._name_line.setReadOnly(False) - self._name_line.setFocus(QtCore.Qt.OtherFocusReason) + self._name_line.setFocus(Qt.OtherFocusReason) self._address_text.setReadOnly(False) self._add_button.setEnabled(False) @@ -338,7 +343,7 @@ class AddressBook(QtWidgets.QWidget): self._save_button.setEnabled(number >= 1) def save_to_file(self): - fileName,_ = QtWidgets.QFileDialog.getSaveFileName(self, + fileName,_ = QFileDialog.getSaveFileName(self, "Save Address Book", '', "Address Book (*.abk);;All Files (*)") @@ -348,7 +353,7 @@ class AddressBook(QtWidgets.QWidget): try: out_file = open(str(fileName), 'wb') except IOError: - QtWidgets.QMessageBox.information(self, "Unable to open file", + QMessageBox.information(self, "Unable to open file", f'There was an error opening "{fileName}"') return @@ -356,7 +361,7 @@ class AddressBook(QtWidgets.QWidget): out_file.close() def load_from_file(self): - fileName,_ = QtWidgets.QFileDialog.getOpenFileName(self, + fileName,_ = QFileDialog.getOpenFileName(self, "Open Address Book", '', "Address Book (*.abk);;All Files (*)") @@ -366,7 +371,7 @@ class AddressBook(QtWidgets.QWidget): try: in_file = open(str(fileName), 'rb') except IOError: - QtWidgets.QMessageBox.information(self, "Unable to open file", + QMessageBox.information(self, "Unable to open file", f'There was an error opening "{fileName}"') return @@ -374,7 +379,7 @@ class AddressBook(QtWidgets.QWidget): in_file.close() if len(self.contacts) == 0: - QtWidgets.QMessageBox.information(self, "No contacts in file", + QMessageBox.information(self, "No contacts in file", "The file you are attempting to open contains no " "contacts.") else: @@ -397,20 +402,20 @@ class AddressBook(QtWidgets.QWidget): first_name = name last_name = '' - file_name = QtWidgets.QFileDialog.getSaveFileName(self, "Export Contact", + file_name = QFileDialog.getSaveFileName(self, "Export Contact", '', "vCard Files (*.vcf);;All Files (*)")[0] if not file_name: return - out_file = QtCore.QFile(file_name) + out_file = QFile(file_name) - if not out_file.open(QtCore.QIODevice.WriteOnly): - QtWidgets.QMessageBox.information(self, "Unable to open file", + if not out_file.open(QIODevice.WriteOnly): + QMessageBox.information(self, "Unable to open file", out_file.errorString()) return - out_s = QtCore.QTextStream(out_file) + out_s = QTextStream(out_file) out_s << 'BEGIN:VCARD' << '\n' out_s << 'VERSION:2.1' << '\n' @@ -424,21 +429,21 @@ class AddressBook(QtWidgets.QWidget): out_s << 'ADR;HOME:;' << address << '\n' out_s << 'END:VCARD' << '\n' - QtWidgets.QMessageBox.information(self, "Export Successful", + QMessageBox.information(self, "Export Successful", f'"{name}" has been exported as a vCard.') -class FindDialog(QtWidgets.QDialog): +class FindDialog(QDialog): def __init__(self, parent=None): super(FindDialog, self).__init__(parent) - find_label = QtWidgets.QLabel("Enter the name of a contact:") - self._line_edit = QtWidgets.QLineEdit() + find_label = QLabel("Enter the name of a contact:") + self._line_edit = QLineEdit() - self._find_button = QtWidgets.QPushButton("&Find") + self._find_button = QPushButton("&Find") self._find_text = '' - layout = QtWidgets.QHBoxLayout() + layout = QHBoxLayout() layout.addWidget(find_label) layout.addWidget(self._line_edit) layout.addWidget(self._find_button) @@ -453,7 +458,7 @@ class FindDialog(QtWidgets.QDialog): text = self._line_edit.text() if not text: - QtWidgets.QMessageBox.information(self, "Empty Field", + QMessageBox.information(self, "Empty Field", "Please enter a name.") return @@ -466,9 +471,7 @@ class FindDialog(QtWidgets.QDialog): if __name__ == '__main__': - import sys - - app = QtWidgets.QApplication(sys.argv) + app = QApplication(sys.argv) address_book = AddressBook() address_book.show() -- cgit v1.2.3