aboutsummaryrefslogtreecommitdiffstats
path: root/examples/widgets/tutorials/addressbook/part7.py
diff options
context:
space:
mode:
Diffstat (limited to 'examples/widgets/tutorials/addressbook/part7.py')
-rw-r--r--examples/widgets/tutorials/addressbook/part7.py115
1 files changed, 59 insertions, 56 deletions
diff --git a/examples/widgets/tutorials/addressbook/part7.py b/examples/widgets/tutorials/addressbook/part7.py
index f494837a6..8378030ce 100644
--- a/examples/widgets/tutorials/addressbook/part7.py
+++ b/examples/widgets/tutorials/addressbook/part7.py
@@ -2,7 +2,7 @@
#############################################################################
##
## Copyright (C) 2013 Riverbank Computing Limited.
-## Copyright (C) 2016 The Qt Company Ltd.
+## Copyright (C) 2021 The Qt Company Ltd.
## Contact: http://www.qt.io/licensing/
##
## This file is part of the Qt for Python examples of the Qt Toolkit.
@@ -41,8 +41,13 @@
#############################################################################
import pickle
+import sys
-from PySide6 import QtCore, QtWidgets
+from PySide6.QtCore import QFile, QIODevice, QTextStream, Qt, Signal, Slot
+from PySide6.QtWidgets import (QApplication, QDialog, QFileDialog,
+ QGridLayout, QHBoxLayout, QLabel, QLineEdit,
+ QMessageBox, QPushButton, QTextEdit,
+ QVBoxLayout, QWidget)
class SortedDict(dict):
@@ -74,7 +79,7 @@ class SortedDict(dict):
iterkeys = __iter__
-class AddressBook(QtWidgets.QWidget):
+class AddressBook(QWidget):
NavigationMode, AddingMode, EditingMode = range(3)
def __init__(self, parent=None):
@@ -85,38 +90,38 @@ class AddressBook(QtWidgets.QWidget):
self._old_address = ''
self._current_mode = self.NavigationMode
- name_label = QtWidgets.QLabel("Name:")
- self._name_line = QtWidgets.QLineEdit()
+ name_label = QLabel("Name:")
+ self._name_line = QLineEdit()
self._name_line.setReadOnly(True)
- address_label = QtWidgets.QLabel("Address:")
- self._address_text = QtWidgets.QTextEdit()
+ address_label = QLabel("Address:")
+ self._address_text = QTextEdit()
self._address_text.setReadOnly(True)
- self._add_button = QtWidgets.QPushButton("&Add")
- self._edit_button = QtWidgets.QPushButton("&Edit")
+ self._add_button = QPushButton("&Add")
+ self._edit_button = QPushButton("&Edit")
self._edit_button.setEnabled(False)
- self._remove_button = QtWidgets.QPushButton("&Remove")
+ self._remove_button = QPushButton("&Remove")
self._remove_button.setEnabled(False)
- self._find_button = QtWidgets.QPushButton("&Find")
+ self._find_button = QPushButton("&Find")
self._find_button.setEnabled(False)
- self._submit_button = QtWidgets.QPushButton("&Submit")
+ self._submit_button = QPushButton("&Submit")
self._submit_button.hide()
- self._cancel_button = QtWidgets.QPushButton("&Cancel")
+ self._cancel_button = QPushButton("&Cancel")
self._cancel_button.hide()
- self._next_button = QtWidgets.QPushButton("&Next")
+ self._next_button = QPushButton("&Next")
self._next_button.setEnabled(False)
- self._previous_button = QtWidgets.QPushButton("&Previous")
+ self._previous_button = QPushButton("&Previous")
self._previous_button.setEnabled(False)
- self._load_button = QtWidgets.QPushButton("&Load...")
+ self._load_button = QPushButton("&Load...")
self._load_button.setToolTip("Load contacts from a file")
- self._save_button = QtWidgets.QPushButton("Sa&ve...")
+ self._save_button = QPushButton("Sa&ve...")
self._save_button.setToolTip("Save contacts to a file")
self._save_button.setEnabled(False)
- self._export_button = QtWidgets.QPushButton("Ex&port")
+ self._export_button = QPushButton("Ex&port")
self._export_button.setToolTip("Export as vCard")
self._export_button.setEnabled(False)
@@ -134,7 +139,7 @@ class AddressBook(QtWidgets.QWidget):
self._save_button.clicked.connect(self.save_to_file)
self._export_button.clicked.connect(self.export_as_vcard)
- button_layout_1 = QtWidgets.QVBoxLayout()
+ button_layout_1 = QVBoxLayout()
button_layout_1.addWidget(self._add_button)
button_layout_1.addWidget(self._edit_button)
button_layout_1.addWidget(self._remove_button)
@@ -146,14 +151,14 @@ class AddressBook(QtWidgets.QWidget):
button_layout_1.addWidget(self._export_button)
button_layout_1.addStretch()
- button_layout_2 = QtWidgets.QHBoxLayout()
+ button_layout_2 = QHBoxLayout()
button_layout_2.addWidget(self._previous_button)
button_layout_2.addWidget(self._next_button)
- main_layout = QtWidgets.QGridLayout()
+ main_layout = QGridLayout()
main_layout.addWidget(name_label, 0, 0)
main_layout.addWidget(self._name_line, 0, 1)
- main_layout.addWidget(address_label, 1, 0, QtCore.Qt.AlignTop)
+ main_layout.addWidget(address_label, 1, 0, Qt.AlignTop)
main_layout.addWidget(self._address_text, 1, 1)
main_layout.addLayout(button_layout_1, 1, 2)
main_layout.addLayout(button_layout_2, 2, 1)
@@ -181,33 +186,33 @@ class AddressBook(QtWidgets.QWidget):
address = self._address_text.toPlainText()
if name == "" or address == "":
- QtWidgets.QMessageBox.information(self, "Empty Field",
+ QMessageBox.information(self, "Empty Field",
"Please enter a name and address.")
return
if self._current_mode == self.AddingMode:
if name not in self.contacts:
self.contacts[name] = address
- QtWidgets.QMessageBox.information(self, "Add Successful",
+ QMessageBox.information(self, "Add Successful",
f'"{name}" has been added to your address book.')
else:
- QtWidgets.QMessageBox.information(self, "Add Unsuccessful",
+ QMessageBox.information(self, "Add Unsuccessful",
f'Sorry, "{name}" is already in your address book.')
return
elif self._current_mode == self.EditingMode:
if self._old_name != name:
if name not in self.contacts:
- QtWidgets.QMessageBox.information(self, "Edit Successful",
+ QMessageBox.information(self, "Edit Successful",
f'"{self.oldName}" has been edited in your address book.')
del self.contacts[self._old_name]
self.contacts[name] = address
else:
- QtWidgets.QMessageBox.information(self, "Edit Unsuccessful",
+ QMessageBox.information(self, "Edit Unsuccessful",
f'Sorry, "{name}" is already in your address book.')
return
elif self._old_address != address:
- QtWidgets.QMessageBox.information(self, "Edit Successful",
+ QMessageBox.information(self, "Edit Successful",
f'"{name}" has been edited in your address book.')
self.contacts[name] = address
@@ -223,15 +228,15 @@ class AddressBook(QtWidgets.QWidget):
address = self._address_text.toPlainText()
if name in self.contacts:
- button = QtWidgets.QMessageBox.question(self, "Confirm Remove",
+ button = QMessageBox.question(self, "Confirm Remove",
f'Are you sure you want to remove "{name}"?',
- QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No)
+ QMessageBox.Yes | QMessageBox.No)
- if button == QtWidgets.QMessageBox.Yes:
+ if button == QMessageBox.Yes:
self.previous()
del self.contacts[name]
- QtWidgets.QMessageBox.information(self, "Remove Successful",
+ QMessageBox.information(self, "Remove Successful",
f'"{name}" has been removed from your address book.')
self.update_interface(self.NavigationMode)
@@ -278,15 +283,15 @@ class AddressBook(QtWidgets.QWidget):
def find_contact(self):
self.dialog.show()
- if self.dialog.exec_() == QtWidgets.QDialog.Accepted:
+ if self.dialog.exec_() == QDialog.Accepted:
contact_name = self.dialog.get_find_text()
if contact_name in self.contacts:
self._name_line.setText(contact_name)
self._address_text.setText(self.contacts[contact_name])
else:
- QtWidgets.QMessageBox.information(self, "Contact Not Found",
- f'Sorry, "{ccontact_nameontactName}" is not in your address book.')
+ QMessageBox.information(self, "Contact Not Found",
+ f'Sorry, "{contact_name}" is not in your address book.')
return
self.update_interface(self.NavigationMode)
@@ -296,7 +301,7 @@ class AddressBook(QtWidgets.QWidget):
if self._current_mode in (self.AddingMode, self.EditingMode):
self._name_line.setReadOnly(False)
- self._name_line.setFocus(QtCore.Qt.OtherFocusReason)
+ self._name_line.setFocus(Qt.OtherFocusReason)
self._address_text.setReadOnly(False)
self._add_button.setEnabled(False)
@@ -338,7 +343,7 @@ class AddressBook(QtWidgets.QWidget):
self._save_button.setEnabled(number >= 1)
def save_to_file(self):
- fileName,_ = QtWidgets.QFileDialog.getSaveFileName(self,
+ fileName,_ = QFileDialog.getSaveFileName(self,
"Save Address Book", '',
"Address Book (*.abk);;All Files (*)")
@@ -348,7 +353,7 @@ class AddressBook(QtWidgets.QWidget):
try:
out_file = open(str(fileName), 'wb')
except IOError:
- QtWidgets.QMessageBox.information(self, "Unable to open file",
+ QMessageBox.information(self, "Unable to open file",
f'There was an error opening "{fileName}"')
return
@@ -356,7 +361,7 @@ class AddressBook(QtWidgets.QWidget):
out_file.close()
def load_from_file(self):
- fileName,_ = QtWidgets.QFileDialog.getOpenFileName(self,
+ fileName,_ = QFileDialog.getOpenFileName(self,
"Open Address Book", '',
"Address Book (*.abk);;All Files (*)")
@@ -366,7 +371,7 @@ class AddressBook(QtWidgets.QWidget):
try:
in_file = open(str(fileName), 'rb')
except IOError:
- QtWidgets.QMessageBox.information(self, "Unable to open file",
+ QMessageBox.information(self, "Unable to open file",
f'There was an error opening "{fileName}"')
return
@@ -374,7 +379,7 @@ class AddressBook(QtWidgets.QWidget):
in_file.close()
if len(self.contacts) == 0:
- QtWidgets.QMessageBox.information(self, "No contacts in file",
+ QMessageBox.information(self, "No contacts in file",
"The file you are attempting to open contains no "
"contacts.")
else:
@@ -397,20 +402,20 @@ class AddressBook(QtWidgets.QWidget):
first_name = name
last_name = ''
- file_name = QtWidgets.QFileDialog.getSaveFileName(self, "Export Contact",
+ file_name = QFileDialog.getSaveFileName(self, "Export Contact",
'', "vCard Files (*.vcf);;All Files (*)")[0]
if not file_name:
return
- out_file = QtCore.QFile(file_name)
+ out_file = QFile(file_name)
- if not out_file.open(QtCore.QIODevice.WriteOnly):
- QtWidgets.QMessageBox.information(self, "Unable to open file",
+ if not out_file.open(QIODevice.WriteOnly):
+ QMessageBox.information(self, "Unable to open file",
out_file.errorString())
return
- out_s = QtCore.QTextStream(out_file)
+ out_s = QTextStream(out_file)
out_s << 'BEGIN:VCARD' << '\n'
out_s << 'VERSION:2.1' << '\n'
@@ -424,21 +429,21 @@ class AddressBook(QtWidgets.QWidget):
out_s << 'ADR;HOME:;' << address << '\n'
out_s << 'END:VCARD' << '\n'
- QtWidgets.QMessageBox.information(self, "Export Successful",
+ QMessageBox.information(self, "Export Successful",
f'"{name}" has been exported as a vCard.')
-class FindDialog(QtWidgets.QDialog):
+class FindDialog(QDialog):
def __init__(self, parent=None):
super(FindDialog, self).__init__(parent)
- find_label = QtWidgets.QLabel("Enter the name of a contact:")
- self._line_edit = QtWidgets.QLineEdit()
+ find_label = QLabel("Enter the name of a contact:")
+ self._line_edit = QLineEdit()
- self._find_button = QtWidgets.QPushButton("&Find")
+ self._find_button = QPushButton("&Find")
self._find_text = ''
- layout = QtWidgets.QHBoxLayout()
+ layout = QHBoxLayout()
layout.addWidget(find_label)
layout.addWidget(self._line_edit)
layout.addWidget(self._find_button)
@@ -453,7 +458,7 @@ class FindDialog(QtWidgets.QDialog):
text = self._line_edit.text()
if not text:
- QtWidgets.QMessageBox.information(self, "Empty Field",
+ QMessageBox.information(self, "Empty Field",
"Please enter a name.")
return
@@ -466,9 +471,7 @@ class FindDialog(QtWidgets.QDialog):
if __name__ == '__main__':
- import sys
-
- app = QtWidgets.QApplication(sys.argv)
+ app = QApplication(sys.argv)
address_book = AddressBook()
address_book.show()