diff options
Diffstat (limited to 'examples/widgets/tutorials')
30 files changed, 647 insertions, 1035 deletions
diff --git a/examples/widgets/tutorials/addressbook/part1.py b/examples/widgets/tutorials/addressbook/part1.py index 23d438416..648ddea46 100644 --- a/examples/widgets/tutorials/addressbook/part1.py +++ b/examples/widgets/tutorials/addressbook/part1.py @@ -1,51 +1,12 @@ - -############################################################################# -## -## Copyright (C) 2013 Riverbank Computing Limited. -## Copyright (C) 2016 The Qt Company Ltd. -## Contact: http://www.qt.io/licensing/ -## -## This file is part of the Qt for Python examples of the Qt Toolkit. -## -## $QT_BEGIN_LICENSE:BSD$ -## You may use this file under the terms of the BSD license as follows: -## -## "Redistribution and use in source and binary forms, with or without -## modification, are permitted provided that the following conditions are -## met: -## * Redistributions of source code must retain the above copyright -## notice, this list of conditions and the following disclaimer. -## * Redistributions in binary form must reproduce the above copyright -## notice, this list of conditions and the following disclaimer in -## the documentation and/or other materials provided with the -## distribution. -## * Neither the name of The Qt Company Ltd nor the names of its -## contributors may be used to endorse or promote products derived -## from this software without specific prior written permission. -## -## -## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -## A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -## OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -## -## $QT_END_LICENSE$ -## -############################################################################# +# Copyright (C) 2013 Riverbank Computing Limited. +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause import sys from PySide6.QtCore import Qt -from PySide6.QtWidgets import (QApplication, QGridLayout, - QLabel, QGridLayout, QLineEdit, QTextEdit, - QWidget) +from PySide6.QtWidgets import (QApplication, QGridLayout, QLabel, QLineEdit, + QTextEdit, QWidget) class AddressBook(QWidget): diff --git a/examples/widgets/tutorials/addressbook/part2.py b/examples/widgets/tutorials/addressbook/part2.py index a5f91a5e7..3c0eb451d 100644 --- a/examples/widgets/tutorials/addressbook/part2.py +++ b/examples/widgets/tutorials/addressbook/part2.py @@ -1,50 +1,12 @@ - -############################################################################# -## -## Copyright (C) 2013 Riverbank Computing Limited. -## 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. -## -## $QT_BEGIN_LICENSE:BSD$ -## You may use this file under the terms of the BSD license as follows: -## -## "Redistribution and use in source and binary forms, with or without -## modification, are permitted provided that the following conditions are -## met: -## * Redistributions of source code must retain the above copyright -## notice, this list of conditions and the following disclaimer. -## * Redistributions in binary form must reproduce the above copyright -## notice, this list of conditions and the following disclaimer in -## the documentation and/or other materials provided with the -## distribution. -## * Neither the name of The Qt Company Ltd nor the names of its -## contributors may be used to endorse or promote products derived -## from this software without specific prior written permission. -## -## -## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -## A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -## OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -## -## $QT_END_LICENSE$ -## -############################################################################# +# Copyright (C) 2013 Riverbank Computing Limited. +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause import sys -from PySide6.QtCore import Qt, Signal, Slot -from PySide6.QtWidgets import (QApplication, QDialog, QGridLayout, - QHBoxLayout, QLabel, QLineEdit, +from PySide6.QtCore import Qt +from PySide6.QtWidgets import (QApplication, QGridLayout, + QLabel, QLineEdit, QMessageBox, QPushButton, QTextEdit, QVBoxLayout, QWidget) @@ -140,17 +102,16 @@ class AddressBook(QWidget): address = self._address_text.toPlainText() if name == "" or address == "": - QMessageBox.information(self, "Empty Field", - "Please enter a name and address.") + QMessageBox.information(self, "Empty Field", "Please enter a name and address.") return if name not in self.contacts: self.contacts[name] = address QMessageBox.information(self, "Add Successful", - f'"{name}" has been added to your address book.') + f'"{name}" has been added to your address book.') else: QMessageBox.information(self, "Add Unsuccessful", - f'Sorry, "{name}" is already in your address book.') + f'Sorry, "{name}" is already in your address book.') return if not self.contacts: diff --git a/examples/widgets/tutorials/addressbook/part3.py b/examples/widgets/tutorials/addressbook/part3.py index 3053dd468..611796f5e 100644 --- a/examples/widgets/tutorials/addressbook/part3.py +++ b/examples/widgets/tutorials/addressbook/part3.py @@ -1,49 +1,11 @@ - -############################################################################# -## -## Copyright (C) 2013 Riverbank Computing Limited. -## Copyright (C) 2016 The Qt Company Ltd. -## Contact: http://www.qt.io/licensing/ -## -## This file is part of the Qt for Python examples of the Qt Toolkit. -## -## $QT_BEGIN_LICENSE:BSD$ -## You may use this file under the terms of the BSD license as follows: -## -## "Redistribution and use in source and binary forms, with or without -## modification, are permitted provided that the following conditions are -## met: -## * Redistributions of source code must retain the above copyright -## notice, this list of conditions and the following disclaimer. -## * Redistributions in binary form must reproduce the above copyright -## notice, this list of conditions and the following disclaimer in -## the documentation and/or other materials provided with the -## distribution. -## * Neither the name of The Qt Company Ltd nor the names of its -## contributors may be used to endorse or promote products derived -## from this software without specific prior written permission. -## -## -## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -## A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -## OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -## -## $QT_END_LICENSE$ -## -############################################################################# +# Copyright (C) 2013 Riverbank Computing Limited. +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause import sys -from PySide6.QtCore import Qt, Signal, Slot -from PySide6.QtWidgets import (QApplication, QDialog, QGridLayout, +from PySide6.QtCore import Qt, Slot +from PySide6.QtWidgets import (QApplication, QGridLayout, QHBoxLayout, QLabel, QLineEdit, QMessageBox, QPushButton, QTextEdit, QVBoxLayout, QWidget) @@ -148,22 +110,22 @@ class AddressBook(QWidget): self._submit_button.show() self._cancel_button.show() + @Slot() def submit_contact(self): name = self._name_line.text() address = self._address_text.toPlainText() if name == "" or address == "": - QMessageBox.information(self, "Empty Field", - "Please enter a name and address.") + QMessageBox.information(self, "Empty Field", "Please enter a name and address.") return if name not in self.contacts: self.contacts[name] = address QMessageBox.information(self, "Add Successful", - f'"{name}" has been added to your address book.') + f'"{name}" has been added to your address book.') else: QMessageBox.information(self, "Add Unsuccessful", - f'Sorry, "{name}" is already in your address book.') + f'Sorry, "{name}" is already in your address book.') return if not self.contacts: @@ -181,6 +143,7 @@ class AddressBook(QWidget): self._submit_button.hide() self._cancel_button.hide() + @Slot() def cancel(self): self._name_line.setText(self._old_name) self._address_text.setText(self._old_address) @@ -200,6 +163,7 @@ class AddressBook(QWidget): self._submit_button.hide() self._cancel_button.hide() + @Slot() def next(self): name = self._name_line.text() it = iter(self.contacts) @@ -217,6 +181,7 @@ class AddressBook(QWidget): self._name_line.setText(next_name) self._address_text.setText(next_address) + @Slot() def previous(self): name = self._name_line.text() diff --git a/examples/widgets/tutorials/addressbook/part4.py b/examples/widgets/tutorials/addressbook/part4.py index 87f196436..95f31d46c 100644 --- a/examples/widgets/tutorials/addressbook/part4.py +++ b/examples/widgets/tutorials/addressbook/part4.py @@ -1,49 +1,11 @@ - -############################################################################# -## -## Copyright (C) 2013 Riverbank Computing Limited. -## 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. -## -## $QT_BEGIN_LICENSE:BSD$ -## You may use this file under the terms of the BSD license as follows: -## -## "Redistribution and use in source and binary forms, with or without -## modification, are permitted provided that the following conditions are -## met: -## * Redistributions of source code must retain the above copyright -## notice, this list of conditions and the following disclaimer. -## * Redistributions in binary form must reproduce the above copyright -## notice, this list of conditions and the following disclaimer in -## the documentation and/or other materials provided with the -## distribution. -## * Neither the name of The Qt Company Ltd nor the names of its -## contributors may be used to endorse or promote products derived -## from this software without specific prior written permission. -## -## -## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -## A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -## OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -## -## $QT_END_LICENSE$ -## -############################################################################# +# Copyright (C) 2013 Riverbank Computing Limited. +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause import sys -from PySide6.QtCore import Qt, Signal, Slot -from PySide6.QtWidgets import (QApplication, QDialog, QGridLayout, +from PySide6.QtCore import Qt, Slot +from PySide6.QtWidgets import (QApplication, QGridLayout, QHBoxLayout, QLabel, QLineEdit, QMessageBox, QPushButton, QTextEdit, QVBoxLayout, QWidget) @@ -143,6 +105,7 @@ class AddressBook(QWidget): self.setLayout(main_layout) self.setWindowTitle("Simple Address Book") + @Slot() def add_contact(self): self._old_name = self._name_line.text() self._old_address = self._address_text.toPlainText() @@ -152,72 +115,76 @@ class AddressBook(QWidget): self.update_interface(self.AddingMode) + @Slot() def edit_contact(self): self._old_name = self._name_line.text() self._old_address = self._address_text.toPlainText() self.update_interface(self.EditingMode) + @Slot() def submit_contact(self): name = self._name_line.text() address = self._address_text.toPlainText() if name == "" or address == "": - QMessageBox.information(self, "Empty Field", - "Please enter a name and address.") + 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 QMessageBox.information(self, "Add Successful", - f'"{name}" has been added to your address book.') + f'"{name}" has been added to your address book.') else: QMessageBox.information(self, "Add Unsuccessful", - f'Sorry, "{name}" is already in your address book.') + 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: QMessageBox.information(self, "Edit Successful", - f'"{self.oldName}" has been edited in your address book.') + f'"{self.oldName}" has been edited in your ' + 'address book.') del self.contacts[self._old_name] self.contacts[name] = address else: QMessageBox.information(self, "Edit Unsuccessful", - f'Sorry, "{name}" is already in your address book.') + f'Sorry, "{name}" is already in your address book.') return elif self._old_address != address: QMessageBox.information(self, "Edit Successful", - f'"{name}" has been edited in your address book.') + f'"{name}" has been edited in your address book.') self.contacts[name] = address self.update_interface(self.NavigationMode) + @Slot() def cancel(self): self._name_line.setText(self._old_name) self._address_text.setText(self._old_address) self.update_interface(self.NavigationMode) + @Slot() def remove_contact(self): name = self._name_line.text() - address = self._address_text.toPlainText() if name in self.contacts: button = QMessageBox.question(self, "Confirm Remove", - f'Are you sure you want to remove "{name}"?', - QMessageBox.Yes | QMessageBox.No) + f'Are you sure you want to remove "{name}"?', + QMessageBox.Yes | QMessageBox.No) if button == QMessageBox.Yes: self.previous() del self.contacts[name] QMessageBox.information(self, "Remove Successful", - f'"{name}" has been removed from your address book.') + f'"{name}" has been removed from your address book.') self.update_interface(self.NavigationMode) + @Slot() def next(self): name = self._name_line.text() it = iter(self.contacts) @@ -235,6 +202,7 @@ class AddressBook(QWidget): self._name_line.setText(next_name) self._address_text.setText(next_address) + @Slot() def previous(self): name = self._name_line.text() diff --git a/examples/widgets/tutorials/addressbook/part5.py b/examples/widgets/tutorials/addressbook/part5.py index c8081ddab..1e9c05862 100644 --- a/examples/widgets/tutorials/addressbook/part5.py +++ b/examples/widgets/tutorials/addressbook/part5.py @@ -1,49 +1,10 @@ +# Copyright (C) 2013 Riverbank Computing Limited. +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause -############################################################################# -## -## Copyright (C) 2013 Riverbank Computing Limited. -## 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. -## -## $QT_BEGIN_LICENSE:BSD$ -## You may use this file under the terms of the BSD license as follows: -## -## "Redistribution and use in source and binary forms, with or without -## modification, are permitted provided that the following conditions are -## met: -## * Redistributions of source code must retain the above copyright -## notice, this list of conditions and the following disclaimer. -## * Redistributions in binary form must reproduce the above copyright -## notice, this list of conditions and the following disclaimer in -## the documentation and/or other materials provided with the -## distribution. -## * Neither the name of The Qt Company Ltd nor the names of its -## contributors may be used to endorse or promote products derived -## from this software without specific prior written permission. -## -## -## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -## A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -## OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -## -## $QT_END_LICENSE$ -## -############################################################################# - -import pickle import sys -from PySide6.QtCore import Qt, Signal, Slot +from PySide6.QtCore import Qt, Slot from PySide6.QtWidgets import (QApplication, QDialog, QGridLayout, QHBoxLayout, QLabel, QLineEdit, QMessageBox, QPushButton, QTextEdit, @@ -150,6 +111,7 @@ class AddressBook(QWidget): self.setLayout(main_layout) self.setWindowTitle("Simple Address Book") + @Slot() def add_contact(self): self._old_name = self._name_line.text() self._old_address = self._address_text.toPlainText() @@ -159,72 +121,76 @@ class AddressBook(QWidget): self.update_interface(self.AddingMode) + @Slot() def edit_contact(self): self._old_name = self._name_line.text() self._old_address = self._address_text.toPlainText() self.update_interface(self.EditingMode) + @Slot() def submit_contact(self): name = self._name_line.text() address = self._address_text.toPlainText() if name == "" or address == "": - QMessageBox.information(self, "Empty Field", - "Please enter a name and address.") + 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 QMessageBox.information(self, "Add Successful", - f'"{name}" has been added to your address book.') + f'"{name}" has been added to your address book.') else: QMessageBox.information(self, "Add Unsuccessful", - f'Sorry, "{name}" is already in your address book.') + 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: QMessageBox.information(self, "Edit Successful", - f'"{self.oldName}" has been edited in your address book.') + f'"{self.oldName}" has been edited in your ' + 'address book.') del self.contacts[self._old_name] self.contacts[name] = address else: QMessageBox.information(self, "Edit Unsuccessful", - f'Sorry, "{name}" is already in your address book.') + f'Sorry, "{name}" is already in your address book.') return elif self._old_address != address: QMessageBox.information(self, "Edit Successful", - f'"{name}" has been edited in your address book.') + f'"{name}" has been edited in your address book.') self.contacts[name] = address self.update_interface(self.NavigationMode) + @Slot() def cancel(self): self._name_line.setText(self._old_name) self._address_text.setText(self._old_address) self.update_interface(self.NavigationMode) + @Slot() def remove_contact(self): name = self._name_line.text() - address = self._address_text.toPlainText() if name in self.contacts: button = QMessageBox.question(self, "Confirm Remove", - f'Are you sure you want to remove "{name}"?', - QMessageBox.Yes | QMessageBox.No) + f'Are you sure you want to remove "{name}"?', + QMessageBox.Yes | QMessageBox.No) if button == QMessageBox.Yes: self.previous() del self.contacts[name] QMessageBox.information(self, "Remove Successful", - f'"{name}" has been removed from your address book.') + f'"{name}" has been removed from your address book.') self.update_interface(self.NavigationMode) + @Slot() def next(self): name = self._name_line.text() it = iter(self.contacts) @@ -242,6 +208,7 @@ class AddressBook(QWidget): self._name_line.setText(next_name) self._address_text.setText(next_address) + @Slot() def previous(self): name = self._name_line.text() @@ -275,7 +242,7 @@ class AddressBook(QWidget): self._address_text.setText(self.contacts[contact_name]) else: QMessageBox.information(self, "Contact Not Found", - f'Sorry, "{contact_name}" is not in your address book.') + f'Sorry, "{contact_name}" is not in your address book.') return self.update_interface(self.NavigationMode) @@ -343,8 +310,7 @@ class FindDialog(QDialog): text = self._line_edit.text() if not text: - QMessageBox.information(self, "Empty Field", - "Please enter a name.") + QMessageBox.information(self, "Empty Field", "Please enter a name.") return else: self._find_text = text diff --git a/examples/widgets/tutorials/addressbook/part6.py b/examples/widgets/tutorials/addressbook/part6.py index 1d43805e7..d11298fb9 100644 --- a/examples/widgets/tutorials/addressbook/part6.py +++ b/examples/widgets/tutorials/addressbook/part6.py @@ -1,49 +1,11 @@ - -############################################################################# -## -## Copyright (C) 2013 Riverbank Computing Limited. -## 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. -## -## $QT_BEGIN_LICENSE:BSD$ -## You may use this file under the terms of the BSD license as follows: -## -## "Redistribution and use in source and binary forms, with or without -## modification, are permitted provided that the following conditions are -## met: -## * Redistributions of source code must retain the above copyright -## notice, this list of conditions and the following disclaimer. -## * Redistributions in binary form must reproduce the above copyright -## notice, this list of conditions and the following disclaimer in -## the documentation and/or other materials provided with the -## distribution. -## * Neither the name of The Qt Company Ltd nor the names of its -## contributors may be used to endorse or promote products derived -## from this software without specific prior written permission. -## -## -## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -## A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -## OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -## -## $QT_END_LICENSE$ -## -############################################################################# +# Copyright (C) 2013 Riverbank Computing Limited. +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause import pickle import sys -from PySide6.QtCore import QFile, QIODevice, QTextStream, Qt, Signal, Slot +from PySide6.QtCore import Qt, Slot from PySide6.QtWidgets import (QApplication, QDialog, QFileDialog, QGridLayout, QHBoxLayout, QLabel, QLineEdit, QMessageBox, QPushButton, QTextEdit, @@ -160,6 +122,7 @@ class AddressBook(QWidget): self.setLayout(main_layout) self.setWindowTitle("Simple Address Book") + @Slot() def add_contact(self): self._old_name = self._name_line.text() self._old_address = self._address_text.toPlainText() @@ -169,72 +132,76 @@ class AddressBook(QWidget): self.update_interface(self.AddingMode) + @Slot() def edit_contact(self): self._old_name = self._name_line.text() self._old_address = self._address_text.toPlainText() self.update_interface(self.EditingMode) + @Slot() def submit_contact(self): name = self._name_line.text() address = self._address_text.toPlainText() if name == "" or address == "": - QMessageBox.information(self, "Empty Field", - "Please enter a name and address.") + 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 QMessageBox.information(self, "Add Successful", - f'"{name}" has been added to your address book.') + f'"{name}" has been added to your address book.') else: QMessageBox.information(self, "Add Unsuccessful", - f'Sorry, "{name}" is already in your address book.') + 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: QMessageBox.information(self, "Edit Successful", - f'"{self.oldName}" has been edited in your address book.') + f'"{self.oldName}" has been edited in your ' + 'address book.') del self.contacts[self._old_name] self.contacts[name] = address else: QMessageBox.information(self, "Edit Unsuccessful", - f'Sorry, "{name}" is already in your address book.') + f'Sorry, "{name}" is already in your address book.') return elif self._old_address != address: QMessageBox.information(self, "Edit Successful", - f'"{name}" has been edited in your address book.') + f'"{name}" has been edited in your address book.') self.contacts[name] = address self.update_interface(self.NavigationMode) + @Slot() def cancel(self): self._name_line.setText(self._old_name) self._address_text.setText(self._old_address) self.update_interface(self.NavigationMode) + @Slot() def remove_contact(self): name = self._name_line.text() - address = self._address_text.toPlainText() if name in self.contacts: button = QMessageBox.question(self, "Confirm Remove", - f'Are you sure you want to remove "{name}"?', - QMessageBox.Yes | QMessageBox.No) + f'Are you sure you want to remove "{name}"?', + QMessageBox.Yes | QMessageBox.No) if button == QMessageBox.Yes: self.previous() del self.contacts[name] QMessageBox.information(self, "Remove Successful", - f'"{name}" has been removed from your address book.') + f'"{name}" has been removed from your address book.') self.update_interface(self.NavigationMode) + @Slot() def next(self): name = self._name_line.text() it = iter(self.contacts) @@ -252,6 +219,7 @@ class AddressBook(QWidget): self._name_line.setText(next_name) self._address_text.setText(next_address) + @Slot() def previous(self): name = self._name_line.text() @@ -285,7 +253,7 @@ class AddressBook(QWidget): self._address_text.setText(self.contacts[contact_name]) else: QMessageBox.information(self, "Contact Not Found", - f'Sorry, "{contact_name}" is not in your address book.') + f'Sorry, "{contact_name}" is not in your address book.') return self.update_interface(self.NavigationMode) @@ -335,8 +303,8 @@ class AddressBook(QWidget): def save_to_file(self): fileName, _ = QFileDialog.getSaveFileName(self, - "Save Address Book", '', - "Address Book (*.abk);;All Files (*)") + "Save Address Book", '', + "Address Book (*.abk);;All Files (*)") if not fileName: return @@ -345,7 +313,7 @@ class AddressBook(QWidget): out_file = open(str(fileName), 'wb') except IOError: QMessageBox.information(self, "Unable to open file", - f'There was an error opening "{fileName}"') + f'There was an error opening "{fileName}"') return pickle.dump(self.contacts, out_file) @@ -353,8 +321,8 @@ class AddressBook(QWidget): def load_from_file(self): fileName, _ = QFileDialog.getOpenFileName(self, - "Open Address Book", '', - "Address Book (*.abk);;All Files (*)") + "Open Address Book", '', + "Address Book (*.abk);;All Files (*)") if not fileName: return @@ -363,7 +331,7 @@ class AddressBook(QWidget): in_file = open(str(fileName), 'rb') except IOError: QMessageBox.information(self, "Unable to open file", - f'There was an error opening "{fileName}"') + f'There was an error opening "{fileName}"') return self.contacts = pickle.load(in_file) @@ -371,8 +339,7 @@ class AddressBook(QWidget): if len(self.contacts) == 0: QMessageBox.information(self, "No contacts in file", - "The file you are attempting to open contains no " - "contacts.") + "The file you are attempting to open contains no contacts.") else: for name, address in self.contacts: self._name_line.setText(name) @@ -406,8 +373,7 @@ class FindDialog(QDialog): text = self._line_edit.text() if not text: - QMessageBox.information(self, "Empty Field", - "Please enter a name.") + QMessageBox.information(self, "Empty Field", "Please enter a name.") return self._find_text = text diff --git a/examples/widgets/tutorials/addressbook/part7.py b/examples/widgets/tutorials/addressbook/part7.py index 26688c428..3829c003d 100644 --- a/examples/widgets/tutorials/addressbook/part7.py +++ b/examples/widgets/tutorials/addressbook/part7.py @@ -1,49 +1,11 @@ - -############################################################################# -## -## Copyright (C) 2013 Riverbank Computing Limited. -## 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. -## -## $QT_BEGIN_LICENSE:BSD$ -## You may use this file under the terms of the BSD license as follows: -## -## "Redistribution and use in source and binary forms, with or without -## modification, are permitted provided that the following conditions are -## met: -## * Redistributions of source code must retain the above copyright -## notice, this list of conditions and the following disclaimer. -## * Redistributions in binary form must reproduce the above copyright -## notice, this list of conditions and the following disclaimer in -## the documentation and/or other materials provided with the -## distribution. -## * Neither the name of The Qt Company Ltd nor the names of its -## contributors may be used to endorse or promote products derived -## from this software without specific prior written permission. -## -## -## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -## A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -## OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -## -## $QT_END_LICENSE$ -## -############################################################################# +# Copyright (C) 2013 Riverbank Computing Limited. +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause import pickle import sys -from PySide6.QtCore import QFile, QIODevice, QTextStream, Qt, Signal, Slot +from PySide6.QtCore import QFile, QIODevice, QTextStream, Qt, Slot from PySide6.QtWidgets import (QApplication, QDialog, QFileDialog, QGridLayout, QHBoxLayout, QLabel, QLineEdit, QMessageBox, QPushButton, QTextEdit, @@ -166,6 +128,7 @@ class AddressBook(QWidget): self.setLayout(main_layout) self.setWindowTitle("Simple Address Book") + @Slot() def add_contact(self): self._old_name = self._name_line.text() self._old_address = self._address_text.toPlainText() @@ -175,72 +138,76 @@ class AddressBook(QWidget): self.update_interface(self.AddingMode) + @Slot() def edit_contact(self): self._old_name = self._name_line.text() self._old_address = self._address_text.toPlainText() self.update_interface(self.EditingMode) + @Slot() def submit_contact(self): name = self._name_line.text() address = self._address_text.toPlainText() if name == "" or address == "": - QMessageBox.information(self, "Empty Field", - "Please enter a name and address.") + 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 QMessageBox.information(self, "Add Successful", - f'"{name}" has been added to your address book.') + f'"{name}" has been added to your address book.') else: QMessageBox.information(self, "Add Unsuccessful", - f'Sorry, "{name}" is already in your address book.') + 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: QMessageBox.information(self, "Edit Successful", - f'"{self.oldName}" has been edited in your address book.') + f'"{self.oldName}" has been edited in your ' + 'address book.') del self.contacts[self._old_name] self.contacts[name] = address else: QMessageBox.information(self, "Edit Unsuccessful", - f'Sorry, "{name}" is already in your address book.') + f'Sorry, "{name}" is already in your address book.') return elif self._old_address != address: QMessageBox.information(self, "Edit Successful", - f'"{name}" has been edited in your address book.') + f'"{name}" has been edited in your address book.') self.contacts[name] = address self.update_interface(self.NavigationMode) + @Slot() def cancel(self): self._name_line.setText(self._old_name) self._address_text.setText(self._old_address) self.update_interface(self.NavigationMode) + @Slot() def remove_contact(self): name = self._name_line.text() - address = self._address_text.toPlainText() if name in self.contacts: button = QMessageBox.question(self, "Confirm Remove", - f'Are you sure you want to remove "{name}"?', - QMessageBox.Yes | QMessageBox.No) + f'Are you sure you want to remove "{name}"?', + QMessageBox.Yes | QMessageBox.No) if button == QMessageBox.Yes: self.previous() del self.contacts[name] QMessageBox.information(self, "Remove Successful", - f'"{name}" has been removed from your address book.') + f'"{name}" has been removed from your address book.') self.update_interface(self.NavigationMode) + @Slot() def next(self): name = self._name_line.text() it = iter(self.contacts) @@ -258,6 +225,7 @@ class AddressBook(QWidget): self._name_line.setText(next_name) self._address_text.setText(next_address) + @Slot() def previous(self): name = self._name_line.text() @@ -291,7 +259,7 @@ class AddressBook(QWidget): self._address_text.setText(self.contacts[contact_name]) else: QMessageBox.information(self, "Contact Not Found", - f'Sorry, "{contact_name}" is not in your address book.') + f'Sorry, "{contact_name}" is not in your address book.') return self.update_interface(self.NavigationMode) @@ -344,8 +312,8 @@ class AddressBook(QWidget): def save_to_file(self): fileName, _ = QFileDialog.getSaveFileName(self, - "Save Address Book", '', - "Address Book (*.abk);;All Files (*)") + "Save Address Book", '', + "Address Book (*.abk);;All Files (*)") if not fileName: return @@ -354,7 +322,7 @@ class AddressBook(QWidget): out_file = open(str(fileName), 'wb') except IOError: QMessageBox.information(self, "Unable to open file", - f'There was an error opening "{fileName}"') + f'There was an error opening "{fileName}"') return pickle.dump(self.contacts, out_file) @@ -362,8 +330,8 @@ class AddressBook(QWidget): def load_from_file(self): fileName, _ = QFileDialog.getOpenFileName(self, - "Open Address Book", '', - "Address Book (*.abk);;All Files (*)") + "Open Address Book", '', + "Address Book (*.abk);;All Files (*)") if not fileName: return @@ -372,7 +340,7 @@ class AddressBook(QWidget): in_file = open(str(fileName), 'rb') except IOError: QMessageBox.information(self, "Unable to open file", - f'There was an error opening "{fileName}"') + f'There was an error opening "{fileName}"') return self.contacts = pickle.load(in_file) @@ -380,8 +348,7 @@ class AddressBook(QWidget): if len(self.contacts) == 0: QMessageBox.information(self, "No contacts in file", - "The file you are attempting to open contains no " - "contacts.") + "The file you are attempting to open contains no contacts.") else: for name, address in self.contacts: self._name_line.setText(name) @@ -403,7 +370,7 @@ class AddressBook(QWidget): last_name = '' file_name = QFileDialog.getSaveFileName(self, "Export Contact", - '', "vCard Files (*.vcf);;All Files (*)")[0] + '', "vCard Files (*.vcf);;All Files (*)")[0] if not file_name: return @@ -411,8 +378,7 @@ class AddressBook(QWidget): out_file = QFile(file_name) if not out_file.open(QIODevice.WriteOnly): - QMessageBox.information(self, "Unable to open file", - out_file.errorString()) + QMessageBox.information(self, "Unable to open file", out_file.errorString()) return out_s = QTextStream(out_file) @@ -430,7 +396,7 @@ class AddressBook(QWidget): out_s << 'END:VCARD' << '\n' QMessageBox.information(self, "Export Successful", - f'"{name}" has been exported as a vCard.') + f'"{name}" has been exported as a vCard.') class FindDialog(QDialog): @@ -458,8 +424,7 @@ class FindDialog(QDialog): text = self._line_edit.text() if not text: - QMessageBox.information(self, "Empty Field", - "Please enter a name.") + QMessageBox.information(self, "Empty Field", "Please enter a name.") return self._find_text = text diff --git a/examples/widgets/tutorials/cannon/t1.py b/examples/widgets/tutorials/cannon/t1.py index 2da78ccf1..62411ace2 100644 --- a/examples/widgets/tutorials/cannon/t1.py +++ b/examples/widgets/tutorials/cannon/t1.py @@ -1,43 +1,5 @@ - -############################################################################# -## -## 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. -## -## $QT_BEGIN_LICENSE:BSD$ -## You may use this file under the terms of the BSD license as follows: -## -## "Redistribution and use in source and binary forms, with or without -## modification, are permitted provided that the following conditions are -## met: -## * Redistributions of source code must retain the above copyright -## notice, this list of conditions and the following disclaimer. -## * Redistributions in binary form must reproduce the above copyright -## notice, this list of conditions and the following disclaimer in -## the documentation and/or other materials provided with the -## distribution. -## * Neither the name of The Qt Company Ltd nor the names of its -## contributors may be used to endorse or promote products derived -## from this software without specific prior written permission. -## -## -## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -## A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -## OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -## -## $QT_END_LICENSE$ -## -############################################################################# +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause # PySide6 tutorial 1 diff --git a/examples/widgets/tutorials/cannon/t10.py b/examples/widgets/tutorials/cannon/t10.py index ed3d9fce3..8649bb562 100644 --- a/examples/widgets/tutorials/cannon/t10.py +++ b/examples/widgets/tutorials/cannon/t10.py @@ -1,50 +1,12 @@ - -############################################################################# -## -## 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. -## -## $QT_BEGIN_LICENSE:BSD$ -## You may use this file under the terms of the BSD license as follows: -## -## "Redistribution and use in source and binary forms, with or without -## modification, are permitted provided that the following conditions are -## met: -## * Redistributions of source code must retain the above copyright -## notice, this list of conditions and the following disclaimer. -## * Redistributions in binary form must reproduce the above copyright -## notice, this list of conditions and the following disclaimer in -## the documentation and/or other materials provided with the -## distribution. -## * Neither the name of The Qt Company Ltd nor the names of its -## contributors may be used to endorse or promote products derived -## from this software without specific prior written permission. -## -## -## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -## A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -## OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -## -## $QT_END_LICENSE$ -## -############################################################################# +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause # PySide6 tutorial 10 import sys -from PySide6.QtCore import QRect, Qt, Signal, Slot +from PySide6.QtCore import QRect, Qt, Signal, Slot, qWarning from PySide6.QtGui import QColor, QFont, QPainter, QPalette from PySide6.QtWidgets import (QApplication, QGridLayout, QLCDNumber, QPushButton, QSlider, QVBoxLayout, QWidget) @@ -81,8 +43,8 @@ class LCDRange(QWidget): def set_range(self, minValue, maxValue): if minValue < 0 or maxValue > 99 or minValue > maxValue: qWarning(f"LCDRange::setRange({minValue}, {maxValue})\n" - "\tRange must be 0..99\n" - "\tand minValue must not be greater than maxValue") + "\tRange must be 0..99\n" + "\tand minValue must not be greater than maxValue") return self.slider.setRange(minValue, maxValue) @@ -129,15 +91,14 @@ class CannonField(QWidget): self.force_changed.emit(self._current_force) def paintEvent(self, event): - painter = QPainter(self) - - painter.setPen(Qt.NoPen) - painter.setBrush(Qt.blue) + with QPainter(self) as painter: + painter.setPen(Qt.NoPen) + painter.setBrush(Qt.blue) - painter.translate(0, self.height()) - painter.drawPie(QRect(-35, -35, 70, 70), 0, 90 * 16) - painter.rotate(-self._current_angle) - painter.drawRect(QRect(33, -4, 15, 8)) + painter.translate(0, self.height()) + painter.drawPie(QRect(-35, -35, 70, 70), 0, 90 * 16) + painter.rotate(-self._current_angle) + painter.drawRect(QRect(33, -4, 15, 8)) def cannon_rect(self): result = QRect(0, 0, 50, 50) @@ -152,7 +113,7 @@ class MyWidget(QWidget): quit = QPushButton("&Quit") quit.setFont(QFont("Times", 18, QFont.Bold)) - quit.clicked.connect(qApp.quit) + quit.clicked.connect(qApp.quit) # noqa: F821 angle = LCDRange() angle.set_range(5, 70) diff --git a/examples/widgets/tutorials/cannon/t11.py b/examples/widgets/tutorials/cannon/t11.py index f297a14b4..fbfd2481d 100644 --- a/examples/widgets/tutorials/cannon/t11.py +++ b/examples/widgets/tutorials/cannon/t11.py @@ -1,43 +1,5 @@ - -############################################################################# -## -## 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. -## -## $QT_BEGIN_LICENSE:BSD$ -## You may use this file under the terms of the BSD license as follows: -## -## "Redistribution and use in source and binary forms, with or without -## modification, are permitted provided that the following conditions are -## met: -## * Redistributions of source code must retain the above copyright -## notice, this list of conditions and the following disclaimer. -## * Redistributions in binary form must reproduce the above copyright -## notice, this list of conditions and the following disclaimer in -## the documentation and/or other materials provided with the -## distribution. -## * Neither the name of The Qt Company Ltd nor the names of its -## contributors may be used to endorse or promote products derived -## from this software without specific prior written permission. -## -## -## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -## A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -## OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -## -## $QT_END_LICENSE$ -## -############################################################################# +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause # PySide6 tutorial 11 @@ -45,7 +7,7 @@ import sys import math -from PySide6.QtCore import QPoint, QRect, QTimer, Qt, Signal, Slot +from PySide6.QtCore import QPoint, QRect, QTimer, Qt, Signal, Slot, qWarning from PySide6.QtGui import QColor, QFont, QPainter, QPalette, QRegion from PySide6.QtWidgets import (QApplication, QGridLayout, QHBoxLayout, QLCDNumber, QPushButton, QSlider, @@ -83,8 +45,8 @@ class LCDRange(QWidget): def set_range(self, minValue, maxValue): if minValue < 0 or maxValue > 99 or minValue > maxValue: qWarning(f"LCDRange::setRange({minValue}, {maxValue})\n" - "\tRange must be 0..99\n" - "\tand minValue must not be greater than maxValue") + "\tRange must be 0..99\n" + "\tand minValue must not be greater than maxValue") return self.slider.setRange(minValue, maxValue) @@ -159,11 +121,10 @@ class CannonField(QWidget): self.update(region) def paintEvent(self, event): - painter = QPainter(self) - - self.paint_cannon(painter) - if self._auto_shoot_timer.isActive(): - self.paint_shot(painter) + with QPainter(self) as painter: + self.paint_cannon(painter) + if self._auto_shoot_timer.isActive(): + self.paint_shot(painter) def paint_shot(self, painter): painter.setPen(Qt.NoPen) @@ -214,7 +175,7 @@ class MyWidget(QWidget): quit = QPushButton("&Quit") quit.setFont(QFont("Times", 18, QFont.Bold)) - quit.clicked.connect(qApp.quit) + quit.clicked.connect(qApp.quit) # noqa: F821 angle = LCDRange() angle.set_range(5, 70) diff --git a/examples/widgets/tutorials/cannon/t12.py b/examples/widgets/tutorials/cannon/t12.py index f2c6ea08c..749c24684 100644 --- a/examples/widgets/tutorials/cannon/t12.py +++ b/examples/widgets/tutorials/cannon/t12.py @@ -1,43 +1,5 @@ - -############################################################################# -## -## 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. -## -## $QT_BEGIN_LICENSE:BSD$ -## You may use this file under the terms of the BSD license as follows: -## -## "Redistribution and use in source and binary forms, with or without -## modification, are permitted provided that the following conditions are -## met: -## * Redistributions of source code must retain the above copyright -## notice, this list of conditions and the following disclaimer. -## * Redistributions in binary form must reproduce the above copyright -## notice, this list of conditions and the following disclaimer in -## the documentation and/or other materials provided with the -## distribution. -## * Neither the name of The Qt Company Ltd nor the names of its -## contributors may be used to endorse or promote products derived -## from this software without specific prior written permission. -## -## -## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -## A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -## OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -## -## $QT_END_LICENSE$ -## -############################################################################# +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause # PySide6 tutorial 12 @@ -46,7 +8,7 @@ import sys import math import random -from PySide6.QtCore import QPoint, QRect, QTime, QTimer, Qt, Signal, Slot +from PySide6.QtCore import QPoint, QRect, QTime, QTimer, Qt, Signal, Slot, qWarning from PySide6.QtGui import QColor, QFont, QPainter, QPalette, QRegion from PySide6.QtWidgets import (QApplication, QGridLayout, QHBoxLayout, QLabel, QLCDNumber, QPushButton, QSlider, @@ -100,8 +62,8 @@ class LCDRange(QWidget): def set_range(self, minValue, maxValue): if minValue < 0 or maxValue > 99 or minValue > maxValue: qWarning(f"LCDRange::setRange({minValue}, {maxValue})\n" - "\tRange must be 0..99\n" - "\tand minValue must not be greater than maxValue") + "\tRange must be 0..99\n" + "\tand minValue must not be greater than maxValue") return self.slider.setRange(minValue, maxValue) @@ -198,13 +160,12 @@ class CannonField(QWidget): self.update(region) def paintEvent(self, event): - painter = QPainter(self) - - self.paint_cannon(painter) - if self._auto_shoot_timer.isActive(): - self.paint_shot(painter) + with QPainter(self) as painter: + self.paint_cannon(painter) + if self._auto_shoot_timer.isActive(): + self.paint_shot(painter) - self.paint_target(painter) + self.paint_target(painter) def paint_shot(self, painter): painter.setPen(Qt.NoPen) @@ -265,7 +226,7 @@ class MyWidget(QWidget): quit = QPushButton("&Quit") quit.setFont(QFont("Times", 18, QFont.Bold)) - quit.clicked.connect(qApp.quit) + quit.clicked.connect(qApp.quit) # noqa: F821 angle = LCDRange("ANGLE") angle.set_range(5, 70) diff --git a/examples/widgets/tutorials/cannon/t13.py b/examples/widgets/tutorials/cannon/t13.py index aa00825aa..f9a771d15 100644 --- a/examples/widgets/tutorials/cannon/t13.py +++ b/examples/widgets/tutorials/cannon/t13.py @@ -1,43 +1,5 @@ - -############################################################################# -## -## 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. -## -## $QT_BEGIN_LICENSE:BSD$ -## You may use this file under the terms of the BSD license as follows: -## -## "Redistribution and use in source and binary forms, with or without -## modification, are permitted provided that the following conditions are -## met: -## * Redistributions of source code must retain the above copyright -## notice, this list of conditions and the following disclaimer. -## * Redistributions in binary form must reproduce the above copyright -## notice, this list of conditions and the following disclaimer in -## the documentation and/or other materials provided with the -## distribution. -## * Neither the name of The Qt Company Ltd nor the names of its -## contributors may be used to endorse or promote products derived -## from this software without specific prior written permission. -## -## -## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -## A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -## OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -## -## $QT_END_LICENSE$ -## -############################################################################# +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause # PySide6 tutorial 13 @@ -46,8 +8,8 @@ import sys import math import random -from PySide6.QtCore import (QPoint, QRect, QTime, QTimer, Qt, SIGNAL, SLOT, - Signal, Slot) +from PySide6.QtCore import (QPoint, QRect, QTime, QTimer, Qt, + Signal, Slot, qWarning) from PySide6.QtGui import QColor, QFont, QPainter, QPalette, QRegion from PySide6.QtWidgets import (QApplication, QGridLayout, QHBoxLayout, QLabel, QLCDNumber, QPushButton, QSizePolicy, QSlider, @@ -102,8 +64,8 @@ class LCDRange(QWidget): def set_range(self, minValue, maxValue): if minValue < 0 or maxValue > 99 or minValue > maxValue: qWarning(f"LCDRange::setRange({minValue}, {maxValue})\n" - "\tRange must be 0..99\n" - "\tand minValue must not be greater than maxValue") + "\tRange must be 0..99\n" + "\tand minValue must not be greater than maxValue") return self.slider.setRange(minValue, maxValue) @@ -220,18 +182,17 @@ class CannonField(QWidget): self.update(region) def paintEvent(self, event): - painter = QPainter(self) - - if self._game_ended: - painter.setPen(Qt.black) - painter.setFont(QFont("Courier", 48, QFont.Bold)) - painter.drawText(self.rect(), Qt.AlignCenter, "Game Over") - - self.paint_cannon(painter) - if self.is_shooting(): - self.paint_shot(painter) - if not self._game_ended: - self.paint_target(painter) + with QPainter(self) as painter: + if self._game_ended: + painter.setPen(Qt.black) + painter.setFont(QFont("Courier", 48, QFont.Bold)) + painter.drawText(self.rect(), Qt.AlignCenter, "Game Over") + + self.paint_cannon(painter) + if self.is_shooting(): + self.paint_shot(painter) + if not self._game_ended: + self.paint_target(painter) def paint_shot(self, painter): painter.setPen(Qt.NoPen) @@ -298,7 +259,7 @@ class GameBoard(QWidget): quit = QPushButton("&Quit") quit.setFont(QFont("Times", 18, QFont.Bold)) - quit.clicked.connect(qApp.quit) + quit.clicked.connect(qApp.quit) # noqa: F821 angle = LCDRange("ANGLE") angle.set_range(5, 70) diff --git a/examples/widgets/tutorials/cannon/t14.py b/examples/widgets/tutorials/cannon/t14.py index 0e8318ebe..3c94408f3 100644 --- a/examples/widgets/tutorials/cannon/t14.py +++ b/examples/widgets/tutorials/cannon/t14.py @@ -1,43 +1,5 @@ - -############################################################################# -## -## 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. -## -## $QT_BEGIN_LICENSE:BSD$ -## You may use this file under the terms of the BSD license as follows: -## -## "Redistribution and use in source and binary forms, with or without -## modification, are permitted provided that the following conditions are -## met: -## * Redistributions of source code must retain the above copyright -## notice, this list of conditions and the following disclaimer. -## * Redistributions in binary form must reproduce the above copyright -## notice, this list of conditions and the following disclaimer in -## the documentation and/or other materials provided with the -## distribution. -## * Neither the name of The Qt Company Ltd nor the names of its -## contributors may be used to endorse or promote products derived -## from this software without specific prior written permission. -## -## -## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -## A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -## OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -## -## $QT_END_LICENSE$ -## -############################################################################# +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause # PySide6 tutorial 14 @@ -46,10 +8,10 @@ import sys import math import random -from PySide6.QtCore import (QPoint, QRect, QTime, QTimer, QSize, Qt, SIGNAL, - SLOT, Signal, Slot) +from PySide6.QtCore import (QPoint, QRect, QTime, QTimer, QSize, Qt, + Signal, Slot, qWarning) from PySide6.QtGui import (QColor, QFont, QKeySequence, QPainter, QPalette, - QShortcut, QRegion) + QShortcut, QRegion, QTransform) from PySide6.QtWidgets import (QApplication, QFrame, QGridLayout, QHBoxLayout, QLabel, QLCDNumber, QPushButton, QSizePolicy, QSlider, QVBoxLayout, QWidget) @@ -103,8 +65,8 @@ class LCDRange(QWidget): def set_range(self, minValue, maxValue): if minValue < 0 or maxValue > 99 or minValue > maxValue: qWarning(f"LCDRange::setRange({minValue}, {maxValue})\n" - "\tRange must be 0..99\n" - "\tand minValue must not be greater than maxValue") + "\tRange must be 0..99\n" + "\tand minValue must not be greater than maxValue") return self.slider.setRange(minValue, maxValue) @@ -212,7 +174,8 @@ class CannonField(QWidget): self._auto_shoot_timer.stop() self.hit.emit() self.can_shoot.emit(True) - elif shot_r.x() > self.width() or shot_r.y() > self.height() or shot_r.intersects(self.barrier_rect()): + elif (shot_r.x() > self.width() or shot_r.y() > self.height() + or shot_r.intersects(self.barrier_rect())): self._auto_shoot_timer.stop() self.missed.emit() self.can_shoot.emit(True) @@ -243,19 +206,18 @@ class CannonField(QWidget): self._barrel_pressed = False def paintEvent(self, event): - painter = QPainter(self) - - if self._game_ended: - painter.setPen(Qt.black) - painter.setFont(QFont("Courier", 48, QFont.Bold)) - painter.drawText(self.rect(), Qt.AlignCenter, "Game Over") - - self.paint_cannon(painter) - self.paint_barrier(painter) - if self.is_shooting(): - self.paint_shot(painter) - if not self._game_ended: - self.paint_target(painter) + with QPainter(self) as painter: + if self._game_ended: + painter.setPen(Qt.black) + painter.setFont(QFont("Courier", 48, QFont.Bold)) + painter.drawText(self.rect(), Qt.AlignCenter, "Game Over") + + self.paint_cannon(painter) + self.paint_barrier(painter) + if self.is_shooting(): + self.paint_shot(painter) + if not self._game_ended: + self.paint_target(painter) def paint_shot(self, painter): painter.setPen(Qt.NoPen) @@ -340,7 +302,7 @@ class GameBoard(QWidget): quit = QPushButton("&Quit") quit.setFont(QFont("Times", 18, QFont.Bold)) - quit.clicked.connect(qApp.quit) + quit.clicked.connect(qApp.quit) # noqa: F821 angle = LCDRange("ANGLE") angle.set_range(5, 70) @@ -380,7 +342,7 @@ class GameBoard(QWidget): QShortcut(QKeySequence(Qt.Key_Enter), self, self.fire) QShortcut(QKeySequence(Qt.Key_Return), self, self.fire) - QShortcut(QKeySequence(Qt.CTRL + Qt.Key_Q), self, self.close) + QShortcut(QKeySequence(Qt.CTRL | Qt.Key_Q), self, self.close) top_layout = QHBoxLayout() top_layout.addWidget(shoot) diff --git a/examples/widgets/tutorials/cannon/t2.py b/examples/widgets/tutorials/cannon/t2.py index 1205a1328..d3adba396 100644 --- a/examples/widgets/tutorials/cannon/t2.py +++ b/examples/widgets/tutorials/cannon/t2.py @@ -1,52 +1,13 @@ - -############################################################################# -## -## 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. -## -## $QT_BEGIN_LICENSE:BSD$ -## You may use this file under the terms of the BSD license as follows: -## -## "Redistribution and use in source and binary forms, with or without -## modification, are permitted provided that the following conditions are -## met: -## * Redistributions of source code must retain the above copyright -## notice, this list of conditions and the following disclaimer. -## * Redistributions in binary form must reproduce the above copyright -## notice, this list of conditions and the following disclaimer in -## the documentation and/or other materials provided with the -## distribution. -## * Neither the name of The Qt Company Ltd nor the names of its -## contributors may be used to endorse or promote products derived -## from this software without specific prior written permission. -## -## -## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -## A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -## OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -## -## $QT_END_LICENSE$ -## -############################################################################# +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause # PySide6 tutorial 2 import sys -from PySide6.QtCore import (QPoint, QRect, QTime, QTimer, Qt) from PySide6.QtGui import QFont -from PySide6.QtWidgets import (QApplication, QPushButton) +from PySide6.QtWidgets import QApplication, QPushButton if __name__ == '__main__': diff --git a/examples/widgets/tutorials/cannon/t3.py b/examples/widgets/tutorials/cannon/t3.py index fbbb9b71e..13bd8f736 100644 --- a/examples/widgets/tutorials/cannon/t3.py +++ b/examples/widgets/tutorials/cannon/t3.py @@ -1,43 +1,5 @@ - -############################################################################# -## -## 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. -## -## $QT_BEGIN_LICENSE:BSD$ -## You may use this file under the terms of the BSD license as follows: -## -## "Redistribution and use in source and binary forms, with or without -## modification, are permitted provided that the following conditions are -## met: -## * Redistributions of source code must retain the above copyright -## notice, this list of conditions and the following disclaimer. -## * Redistributions in binary form must reproduce the above copyright -## notice, this list of conditions and the following disclaimer in -## the documentation and/or other materials provided with the -## distribution. -## * Neither the name of The Qt Company Ltd nor the names of its -## contributors may be used to endorse or promote products derived -## from this software without specific prior written permission. -## -## -## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -## A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -## OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -## -## $QT_END_LICENSE$ -## -############################################################################# +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause # PySide6 tutorial 3 diff --git a/examples/widgets/tutorials/cannon/t4.py b/examples/widgets/tutorials/cannon/t4.py index b309c7bff..37a2dc9dd 100644 --- a/examples/widgets/tutorials/cannon/t4.py +++ b/examples/widgets/tutorials/cannon/t4.py @@ -1,43 +1,5 @@ - -############################################################################# -## -## 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. -## -## $QT_BEGIN_LICENSE:BSD$ -## You may use this file under the terms of the BSD license as follows: -## -## "Redistribution and use in source and binary forms, with or without -## modification, are permitted provided that the following conditions are -## met: -## * Redistributions of source code must retain the above copyright -## notice, this list of conditions and the following disclaimer. -## * Redistributions in binary form must reproduce the above copyright -## notice, this list of conditions and the following disclaimer in -## the documentation and/or other materials provided with the -## distribution. -## * Neither the name of The Qt Company Ltd nor the names of its -## contributors may be used to endorse or promote products derived -## from this software without specific prior written permission. -## -## -## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -## A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -## OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -## -## $QT_END_LICENSE$ -## -############################################################################# +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause # PySide6 tutorial 4 @@ -58,7 +20,7 @@ class MyWidget(QWidget): self.quit.setGeometry(62, 40, 75, 30) self.quit.setFont(QFont("Times", 18, QFont.Bold)) - self.quit.clicked.connect(qApp.quit) + self.quit.clicked.connect(qApp.quit) # noqa: F821 if __name__ == '__main__': diff --git a/examples/widgets/tutorials/cannon/t5.py b/examples/widgets/tutorials/cannon/t5.py index a4324a633..ed5d085f8 100644 --- a/examples/widgets/tutorials/cannon/t5.py +++ b/examples/widgets/tutorials/cannon/t5.py @@ -1,43 +1,5 @@ - -############################################################################# -## -## 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. -## -## $QT_BEGIN_LICENSE:BSD$ -## You may use this file under the terms of the BSD license as follows: -## -## "Redistribution and use in source and binary forms, with or without -## modification, are permitted provided that the following conditions are -## met: -## * Redistributions of source code must retain the above copyright -## notice, this list of conditions and the following disclaimer. -## * Redistributions in binary form must reproduce the above copyright -## notice, this list of conditions and the following disclaimer in -## the documentation and/or other materials provided with the -## distribution. -## * Neither the name of The Qt Company Ltd nor the names of its -## contributors may be used to endorse or promote products derived -## from this software without specific prior written permission. -## -## -## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -## A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -## OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -## -## $QT_END_LICENSE$ -## -############################################################################# +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause # PySide6 tutorial 5 @@ -63,7 +25,7 @@ class MyWidget(QWidget): slider.setRange(0, 99) slider.setValue(0) - quit.clicked.connect(qApp.quit) + quit.clicked.connect(qApp.quit) # noqa: F821 slider.valueChanged.connect(lcd.display) layout = QVBoxLayout(self) diff --git a/examples/widgets/tutorials/cannon/t6.py b/examples/widgets/tutorials/cannon/t6.py index c30fbfc6d..ea2e044e6 100644 --- a/examples/widgets/tutorials/cannon/t6.py +++ b/examples/widgets/tutorials/cannon/t6.py @@ -1,43 +1,5 @@ - -############################################################################# -## -## 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. -## -## $QT_BEGIN_LICENSE:BSD$ -## You may use this file under the terms of the BSD license as follows: -## -## "Redistribution and use in source and binary forms, with or without -## modification, are permitted provided that the following conditions are -## met: -## * Redistributions of source code must retain the above copyright -## notice, this list of conditions and the following disclaimer. -## * Redistributions in binary form must reproduce the above copyright -## notice, this list of conditions and the following disclaimer in -## the documentation and/or other materials provided with the -## distribution. -## * Neither the name of The Qt Company Ltd nor the names of its -## contributors may be used to endorse or promote products derived -## from this software without specific prior written permission. -## -## -## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -## A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -## OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -## -## $QT_END_LICENSE$ -## -############################################################################# +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause # PySide6 tutorial 6 @@ -71,7 +33,7 @@ class MyWidget(QWidget): quit = QPushButton("Quit") quit.setFont(QFont("Times", 18, QFont.Bold)) - quit.clicked.connect(qApp.quit) + quit.clicked.connect(qApp.quit) # noqa: F821 layout = QVBoxLayout(self) layout.addWidget(quit) diff --git a/examples/widgets/tutorials/cannon/t7.py b/examples/widgets/tutorials/cannon/t7.py index f1c6798bb..1175107b8 100644 --- a/examples/widgets/tutorials/cannon/t7.py +++ b/examples/widgets/tutorials/cannon/t7.py @@ -1,43 +1,5 @@ - -############################################################################# -## -## 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. -## -## $QT_BEGIN_LICENSE:BSD$ -## You may use this file under the terms of the BSD license as follows: -## -## "Redistribution and use in source and binary forms, with or without -## modification, are permitted provided that the following conditions are -## met: -## * Redistributions of source code must retain the above copyright -## notice, this list of conditions and the following disclaimer. -## * Redistributions in binary form must reproduce the above copyright -## notice, this list of conditions and the following disclaimer in -## the documentation and/or other materials provided with the -## distribution. -## * Neither the name of The Qt Company Ltd nor the names of its -## contributors may be used to endorse or promote products derived -## from this software without specific prior written permission. -## -## -## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -## A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -## OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -## -## $QT_END_LICENSE$ -## -############################################################################# +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause # PySide6 tutorial 7 @@ -84,7 +46,7 @@ class MyWidget(QWidget): quit = QPushButton("Quit") quit.setFont(QFont("Times", 18, QFont.Bold)) - quit.clicked.connect(qApp.quit) + quit.clicked.connect(qApp.quit) # noqa: F821 previous_range = None diff --git a/examples/widgets/tutorials/cannon/t8.py b/examples/widgets/tutorials/cannon/t8.py index cc54194cf..9bb5516b8 100644 --- a/examples/widgets/tutorials/cannon/t8.py +++ b/examples/widgets/tutorials/cannon/t8.py @@ -1,50 +1,12 @@ - -############################################################################# -## -## 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. -## -## $QT_BEGIN_LICENSE:BSD$ -## You may use this file under the terms of the BSD license as follows: -## -## "Redistribution and use in source and binary forms, with or without -## modification, are permitted provided that the following conditions are -## met: -## * Redistributions of source code must retain the above copyright -## notice, this list of conditions and the following disclaimer. -## * Redistributions in binary form must reproduce the above copyright -## notice, this list of conditions and the following disclaimer in -## the documentation and/or other materials provided with the -## distribution. -## * Neither the name of The Qt Company Ltd nor the names of its -## contributors may be used to endorse or promote products derived -## from this software without specific prior written permission. -## -## -## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -## A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -## OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -## -## $QT_END_LICENSE$ -## -############################################################################# +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause # PySide6 tutorial 8 import sys -from PySide6.QtCore import Signal, Slot, Qt +from PySide6.QtCore import Signal, Slot, Qt, qWarning from PySide6.QtGui import QColor, QFont, QPainter, QPalette from PySide6.QtWidgets import (QApplication, QGridLayout, QLCDNumber, QPushButton, QSlider, QVBoxLayout, QWidget) @@ -81,8 +43,8 @@ class LCDRange(QWidget): def set_range(self, minValue, maxValue): if minValue < 0 or maxValue > 99 or minValue > maxValue: qWarning("LCDRange.setRange({minValue}, {maxValue})\n" - "\tRange must be 0..99\n" - "\tand minValue must not be greater than maxValue") + "\tRange must be 0..99\n" + "\tand minValue must not be greater than maxValue") return self.slider.setRange(minValue, maxValue) @@ -115,8 +77,8 @@ class CannonField(QWidget): self.angle_changed.emit(self._current_angle) def paintEvent(self, event): - painter = QPainter(self) - painter.drawText(200, 200, f"Angle = {self._current_angle}") + with QPainter(self) as painter: + painter.drawText(200, 200, f"Angle = {self._current_angle}") class MyWidget(QWidget): @@ -126,7 +88,7 @@ class MyWidget(QWidget): quit = QPushButton("Quit") quit.setFont(QFont("Times", 18, QFont.Bold)) - quit.clicked.connect(qApp.quit) + quit.clicked.connect(qApp.quit) # noqa: F821 angle = LCDRange() angle.set_range(5, 70) diff --git a/examples/widgets/tutorials/cannon/t9.py b/examples/widgets/tutorials/cannon/t9.py index 36dcf8fb1..7cdda4e7b 100644 --- a/examples/widgets/tutorials/cannon/t9.py +++ b/examples/widgets/tutorials/cannon/t9.py @@ -1,50 +1,12 @@ - -############################################################################# -## -## 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. -## -## $QT_BEGIN_LICENSE:BSD$ -## You may use this file under the terms of the BSD license as follows: -## -## "Redistribution and use in source and binary forms, with or without -## modification, are permitted provided that the following conditions are -## met: -## * Redistributions of source code must retain the above copyright -## notice, this list of conditions and the following disclaimer. -## * Redistributions in binary form must reproduce the above copyright -## notice, this list of conditions and the following disclaimer in -## the documentation and/or other materials provided with the -## distribution. -## * Neither the name of The Qt Company Ltd nor the names of its -## contributors may be used to endorse or promote products derived -## from this software without specific prior written permission. -## -## -## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -## A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -## OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -## -## $QT_END_LICENSE$ -## -############################################################################# +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause # PySide6 tutorial 9 import sys -from PySide6.QtCore import QRect, Qt, Signal, Slot +from PySide6.QtCore import QRect, Qt, Signal, Slot, qWarning from PySide6.QtGui import QColor, QFont, QPainter, QPalette from PySide6.QtWidgets import (QApplication, QGridLayout, QLCDNumber, QPushButton, QSlider, QVBoxLayout, QWidget) @@ -81,8 +43,8 @@ class LCDRange(QWidget): def set_range(self, minValue, maxValue): if minValue < 0 or maxValue > 99 or minValue > maxValue: qWarning(f"LCDRange::setRange({minValue}, {maxValue})\n" - "\tRange must be 0..99\n" - "\tand minValue must not be greater than maxValue") + "\tRange must be 0..99\n" + "\tand minValue must not be greater than maxValue") return self.slider.setRange(minValue, maxValue) @@ -115,15 +77,14 @@ class CannonField(QWidget): self.angle_changed.emit(self._current_angle) def paintEvent(self, event): - painter = QPainter(self) - - painter.setPen(Qt.NoPen) - painter.setBrush(Qt.blue) + with QPainter(self) as painter: + painter.setPen(Qt.NoPen) + painter.setBrush(Qt.blue) - painter.translate(0, self.rect().height()) - painter.drawPie(QRect(-35, -35, 70, 70), 0, 90 * 16) - painter.rotate(-self._current_angle) - painter.drawRect(QRect(33, -4, 15, 8)) + painter.translate(0, self.rect().height()) + painter.drawPie(QRect(-35, -35, 70, 70), 0, 90 * 16) + painter.rotate(-self._current_angle) + painter.drawRect(QRect(33, -4, 15, 8)) class MyWidget(QWidget): @@ -133,7 +94,7 @@ class MyWidget(QWidget): quit = QPushButton("Quit") quit.setFont(QFont("Times", 18, QFont.Bold)) - quit.clicked.connect(qApp.quit) + quit.clicked.connect(qApp.quit) # noqa: F821 angle = LCDRange() angle.set_range(5, 70) diff --git a/examples/widgets/tutorials/modelview/1_readonly.py b/examples/widgets/tutorials/modelview/1_readonly.py new file mode 100644 index 000000000..9dc923260 --- /dev/null +++ b/examples/widgets/tutorials/modelview/1_readonly.py @@ -0,0 +1,38 @@ +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +import sys + +from PySide6.QtCore import QAbstractTableModel, Qt +from PySide6.QtWidgets import QApplication, QTableView + +"""PySide6 port of the widgets/tutorials/modelview/1_readonly example from Qt v6.x""" + + +#! [1] +class MyModel(QAbstractTableModel): + def __init__(self, parent=None): + super().__init__(parent) + + def rowCount(self, parent=None): + return 2 + + def columnCount(self, parent=None): + return 3 + + def data(self, index, role=Qt.DisplayRole): + if role == Qt.DisplayRole: + row = index.row() + 1 + column = index.column() + 1 + return f"Row{row}, Column{column}" + return None +#! [1] + + +if __name__ == '__main__': + app = QApplication(sys.argv) + table_view = QTableView() + my_model = MyModel() + table_view.setModel(my_model) + table_view.show() + sys.exit(app.exec()) diff --git a/examples/widgets/tutorials/modelview/2_formatting.py b/examples/widgets/tutorials/modelview/2_formatting.py new file mode 100644 index 000000000..f39ec462c --- /dev/null +++ b/examples/widgets/tutorials/modelview/2_formatting.py @@ -0,0 +1,65 @@ +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +import sys + +from PySide6.QtCore import QAbstractTableModel, Qt +from PySide6.QtGui import QBrush, QFont +from PySide6.QtWidgets import QApplication, QTableView + +"""PySide6 port of the widgets/tutorials/modelview/2_formatting example from Qt v6.x""" + + +class MyModel(QAbstractTableModel): + def __init__(self, parent=None): + super().__init__(parent) + + def rowCount(self, parent=None): + return 2 + + def columnCount(self, parent=None): + return 3 + +#! [1] + def data(self, index, role=Qt.DisplayRole): + row = index.row() + col = index.column() + # generate a log message when this method gets called + print(f"row {row}, col{col}, role {role}") + + if role == Qt.DisplayRole: + if row == 0 and col == 1: + return "<--left" + if row == 1 and col == 1: + return "right-->" + return f"Row{row}, Column{col + 1}" + + elif role == Qt.FontRole: + if row == 0 and col == 0: # change font only for cell(0,0) + bold_font = QFont() + bold_font.setBold(True) + return bold_font + + elif role == Qt.BackgroundRole: + if row == 1 and col == 2: # change background only for cell(1,2) + return QBrush(Qt.red) + + elif role == Qt.TextAlignmentRole: + if row == 1 and col == 1: # change text alignment only for cell(1,1) + return Qt.AlignRight | Qt.AlignVCenter + + elif role == Qt.CheckStateRole: + if row == 1 and col == 0: # add a checkbox to cell(1,0) + return Qt.Checked + + return None +#! [1] + + +if __name__ == '__main__': + app = QApplication(sys.argv) + table_view = QTableView() + my_model = MyModel() + table_view.setModel(my_model) + table_view.show() + sys.exit(app.exec()) diff --git a/examples/widgets/tutorials/modelview/3_changingmodel.py b/examples/widgets/tutorials/modelview/3_changingmodel.py new file mode 100644 index 000000000..2148ec5d3 --- /dev/null +++ b/examples/widgets/tutorials/modelview/3_changingmodel.py @@ -0,0 +1,53 @@ +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +import sys + +from PySide6.QtCore import QAbstractTableModel, QTime, QTimer, Qt, Slot +from PySide6.QtWidgets import QApplication, QTableView + +"""PySide6 port of the widgets/tutorials/modelview/3_changingmodel example from Qt v6.x""" + + +class MyModel(QAbstractTableModel): +#! [1] + def __init__(self, parent=None): + super().__init__(parent) + self._timer = QTimer(self) + self._timer.setInterval(1000) + self._timer.timeout.connect(self.timer_hit) + self._timer.start() +#! [1] + + def rowCount(self, parent=None): + return 2 + + def columnCount(self, parent=None): + return 3 + +#! [2] + def data(self, index, role=Qt.DisplayRole): + row = index.row() + col = index.column() + if role == Qt.DisplayRole and row == 0 and col == 0: + return QTime.currentTime().toString() + return None +#! [2] + +#! [3] + @Slot() + def timer_hit(self): + # we identify the top left cell + top_left = self.createIndex(0, 0) + # emit a signal to make the view reread identified data + self.dataChanged.emit(top_left, top_left, [Qt.DisplayRole]) +#! [3] + + +if __name__ == '__main__': + app = QApplication(sys.argv) + table_view = QTableView() + my_model = MyModel() + table_view.setModel(my_model) + table_view.show() + sys.exit(app.exec()) diff --git a/examples/widgets/tutorials/modelview/4_headers.py b/examples/widgets/tutorials/modelview/4_headers.py new file mode 100644 index 000000000..3feef17bf --- /dev/null +++ b/examples/widgets/tutorials/modelview/4_headers.py @@ -0,0 +1,43 @@ +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +import sys + +from PySide6.QtCore import QAbstractTableModel, Qt +from PySide6.QtWidgets import QApplication, QTableView + +"""PySide6 port of the widgets/tutorials/modelview/4_headers example from Qt v6.x""" + + +class MyModel(QAbstractTableModel): + def __init__(self, parent=None): + super().__init__(parent) + + def rowCount(self, parent=None): + return 2 + + def columnCount(self, parent=None): + return 3 + + def data(self, index, role=Qt.DisplayRole): + if role == Qt.DisplayRole: + row = index.row() + 1 + column = index.column() + 1 + return f"Row{row}, Column{column}" + return None + +#! [1] + def headerData(self, section, orientation, role): + if role == Qt.DisplayRole and orientation == Qt.Horizontal: + return ["first", "second", "third"][section] + return None +#! [1] + + +if __name__ == '__main__': + app = QApplication(sys.argv) + table_view = QTableView() + my_model = MyModel() + table_view.setModel(my_model) + table_view.show() + sys.exit(app.exec()) diff --git a/examples/widgets/tutorials/modelview/5_edit.py b/examples/widgets/tutorials/modelview/5_edit.py new file mode 100644 index 000000000..1a4481fc9 --- /dev/null +++ b/examples/widgets/tutorials/modelview/5_edit.py @@ -0,0 +1,73 @@ +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +import sys +from itertools import chain + +from PySide6.QtCore import QAbstractTableModel, Qt, Signal, Slot +from PySide6.QtWidgets import QApplication, QMainWindow, QTableView + +"""PySide6 port of the widgets/tutorials/modelview/5_edit example from Qt v6.x""" + + +COLS = 3 +ROWS = 2 + + +class MyModel(QAbstractTableModel): + + editCompleted = Signal(str) + + def __init__(self, parent=None): + super().__init__(parent) + self._grid_data = [["" for y in range(COLS)] for x in range(ROWS)] + + def rowCount(self, parent=None): + return ROWS + + def columnCount(self, parent=None): + return COLS + + def data(self, index, role=Qt.DisplayRole): + if role == Qt.DisplayRole and self.checkIndex(index): + return self._grid_data[index.row()][index.column()] + return None + +#! [1] + def setData(self, index, value, role): + if role != Qt.EditRole or not self.checkIndex(index): + return False + # save value from editor to member m_gridData + self._grid_data[index.row()][index.column()] = value + # for presentation purposes only: build and emit a joined string + result = " ".join(chain(*self._grid_data)) + self.editCompleted.emit(result) + return True +#! [1] + +#! [2] + def flags(self, index): + return Qt.ItemIsEditable | super().flags(index) +#! [2] + + +class MainWindow(QMainWindow): + def __init__(self, parent=None): + super().__init__(parent) + self._table_view = QTableView(self) + self.setCentralWidget(self._table_view) + my_model = MyModel(self) + self._table_view.setModel(my_model) + # transfer changes to the model to the window title + my_model.editCompleted.connect(self.show_window_title) + + @Slot(str) + def show_window_title(self, title): + self.setWindowTitle(title) + + +if __name__ == '__main__': + app = QApplication(sys.argv) + w = MainWindow() + w.show() + sys.exit(app.exec()) diff --git a/examples/widgets/tutorials/modelview/6_treeview.py b/examples/widgets/tutorials/modelview/6_treeview.py new file mode 100644 index 000000000..cac3c6d53 --- /dev/null +++ b/examples/widgets/tutorials/modelview/6_treeview.py @@ -0,0 +1,42 @@ +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +import sys + +from PySide6.QtGui import QStandardItem, QStandardItemModel +from PySide6.QtWidgets import QApplication, QMainWindow, QTreeView + +"""PySide6 port of the widgets/tutorials/modelview/6_treeview example from Qt v6.x""" + + +#! [1] +class MainWindow(QMainWindow): + def __init__(self, parent=None): + super().__init__(parent) + self._standard_model = QStandardItemModel(self) + self._tree_view = QTreeView(self) + self.setCentralWidget(self._tree_view) + + prepared_row = self.prepare_row("first", "second", "third") + item = self._standard_model.invisibleRootItem() + # adding a row to the invisible root item produces a root element + item.appendRow(prepared_row) + + second_row = self.prepare_row("111", "222", "333") + # adding a row to an item starts a subtree + prepared_row[0].appendRow(second_row) + + self._tree_view.setModel(self._standard_model) + self._tree_view.expandAll() + + def prepare_row(self, first, second, third): + return [QStandardItem(first), QStandardItem(second), + QStandardItem(third)] +#! [1] + + +if __name__ == '__main__': + app = QApplication(sys.argv) + w = MainWindow() + w.show() + sys.exit(app.exec()) diff --git a/examples/widgets/tutorials/modelview/7_selections.py b/examples/widgets/tutorials/modelview/7_selections.py new file mode 100644 index 000000000..c879d8f67 --- /dev/null +++ b/examples/widgets/tutorials/modelview/7_selections.py @@ -0,0 +1,71 @@ +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +import sys + +from PySide6.QtCore import QItemSelection, Qt, Slot +from PySide6.QtGui import QStandardItem, QStandardItemModel +from PySide6.QtWidgets import QApplication, QMainWindow, QTreeView + +"""PySide6 port of the widgets/tutorials/modelview/7_selections example from Qt v6.x""" + + +#! [1] +class MainWindow(QMainWindow): + def __init__(self, parent=None): + super().__init__(parent) + self._standard_model = QStandardItemModel(self) + self._tree_view = QTreeView(self) + self.setCentralWidget(self._tree_view) + + # defining a couple of items + root_node = self._standard_model.invisibleRootItem() + + america_item = QStandardItem("America") + mexico_item = QStandardItem("Canada") + usa_item = QStandardItem("USA") + boston_item = QStandardItem("Boston") + europe_item = QStandardItem("Europe") + italy_item = QStandardItem("Italy") + rome_item = QStandardItem("Rome") + verona_item = QStandardItem("Verona") + + # building up the hierarchy + root_node.appendRow(america_item) + root_node.appendRow(europe_item) + america_item.appendRow(mexico_item) + america_item.appendRow(usa_item) + usa_item.appendRow(boston_item) + europe_item.appendRow(italy_item) + italy_item.appendRow(rome_item) + italy_item.appendRow(verona_item) + + self._tree_view.setModel(self._standard_model) + self._tree_view.expandAll() + + # selection changes shall trigger a slot + selection_model = self._tree_view.selectionModel() + selection_model.selectionChanged.connect(self.selection_changed_slot) +#! [1] + +#! [2] + @Slot(QItemSelection, QItemSelection) + def selection_changed_slot(self, new_selection, old_selection): + # get the text of the selected item + index = self._tree_view.selectionModel().currentIndex() + selected_text = index.data(Qt.DisplayRole) + # find out the hierarchy level of the selected item + hierarchy_level = 1 + seek_root = index + while seek_root.parent().isValid(): + seek_root = seek_root.parent() + hierarchy_level += 1 + self.setWindowTitle(f"{selected_text}, Level {hierarchy_level}") +#! [2] + + +if __name__ == '__main__': + app = QApplication(sys.argv) + w = MainWindow() + w.show() + sys.exit(app.exec()) diff --git a/examples/widgets/tutorials/modelview/doc/modelview.rst b/examples/widgets/tutorials/modelview/doc/modelview.rst new file mode 100644 index 000000000..017f78de1 --- /dev/null +++ b/examples/widgets/tutorials/modelview/doc/modelview.rst @@ -0,0 +1,4 @@ +Model View Tutorial Examples +============================ + +Example code for the Model View Tutorial. diff --git a/examples/widgets/tutorials/modelview/modelview.pyproject b/examples/widgets/tutorials/modelview/modelview.pyproject new file mode 100644 index 000000000..3bbe9d47a --- /dev/null +++ b/examples/widgets/tutorials/modelview/modelview.pyproject @@ -0,0 +1,9 @@ +{ + "files": ["1_readonly.py", + "2_formatting.py", + "3_changingmodel.py", + "4_headers.py", + "5_edit.py", + "6_treeview.py", + "7_selections.py"] +} |