The RESOURCES.QRC needs to be converted to a PY file via PYRCC5 and introduced in the main program.
Pyrcc5-o resources_rc.py RESOURCES.QRC
the 6th instance of the fast programming of the Python Qt GUI with python3+pyqt5 the main window original January 11, 2017 10:14:12 Tags: python/qt/gui/pyqt
This article is an example of the 6th chapter of the fast programming of the Python Qt GUI MainWindow rewritten as a PYTHON3+PYQT5 encoding. Changes, I found a lot of pits to change, the following is about to list some of the key needs to be changed, and with the revised Code, the code in the python3.5+pyqt5.7 environment to run and test normal. Please refer to the study.
This instance has the following programs:
/home/yrd/eric_workspace/charter6/ui_newimagedlg.py
/home/yrd/eric_workspace/charter6/ newimagedlg.py
/home/yrd/eric_workspace/charter6/helpform.py
/home/yrd/eric_workspace/charter6/ resizedlg.py
/home/yrd/eric_workspace/charter6/mainwindow.py
Resource file:
/home/yrd/eric_workspace/charter6/ RESOURCES.QRC
/home/yrd/eric_workspace/charter6/images
/home/yrd/eric_workspace/charter6/help
You can find the code resources for the author's book on the Internet to get the resource files.
Key content:
1, because Settings.value ("RecentFiles") is itself a list type,
Self.recentfiles = Settings.value ("RecentFiles"). Tostringlist ()
changed to
Self.recentfiles=settings.value ("RecentFiles")
2, type conversion problem
Self.restoregeometry (
settings.value ("Mainwindow/geometry"). Tobytearray ())
self.restorestate ( Settings.value ("Mainwindow/state"). Tobytearray ())
Changed to
Self.restoregeometry (
Qbytearray (Settings.value ("Mainwindow/geometry"))
Self.restorestate (QByteArray ( Settings.value ("Mainwindow/state")))
3, the problem of introduction to the library, there are many, only one example:
From Pyqt5.qtprintsupport import Qprinter,qprintdialog
4, the pits in the instance, Qfiledialog.getsavefilename and qfiledialog.getopenfilename return the result is a tupple. The first is the filename and the second is the file type.
Fname,tpye = Qfiledialog.getsavefilename (self,
"image changer-save image", fname,
"image files ({0})". Format ( ". Join (formats)))
Fname,tpye = Qfiledialog.getopenfilename (self,
" image changer-choose image ", dir,
" Image files ({0}) ". Format (" ". Join (formats)))
The qstring type should be replaced with the Python string type in 5,PYQT5 development. Self.recentfiles is a list type, so use append or insert rather than prepend.
def addrecentfile (self, fname):
If fname are None:
return
if not Self.recentFiles.contains (fname):
Self.recentFiles.prepend (QString (fname)) while
Self.recentFiles.count () > 9:
self.recentFiles.takeLast ()
change
to Def addrecentfile (self, fname):
If fname is None:
return
if fname not in self.recentfiles:< C12/>self.recentfiles.insert (0,fname) while
len (self.recentfiles) > 9:
self.recentFiles.pop ()
6, the problem of the signal slot, now for example:
Self.connect (Action, SIGNAL ("triggered ()"), Self.loadfile)
Change into
Action.triggered[bool].connect (Self.loadfile)
The trough Self.loadfile also made some changes according to the actual situation.
The RESOURCES.QRC needs to be converted to a PY file via PYRCC5 and introduced in the main program.
Pyrcc5-o resources_rc.py RESOURCES.QRC
/home/yrd/eric_workspace/charter6/resources.qrc
<!DOCTYPE RCC><RCC version="1.0">
<qresource>
<file alias="filenew.png">images/filenew.png</file>
<file alias="fileopen.png">images/fileopen.png</file>
<file alias="filesave.png">images/filesave.png</file>
<file alias="filesaveas.png">images/filesaveas.png</file>
<file alias="fileprint.png">images/fileprint.png</file>
<file alias="filequit.png">images/filequit.png</file>
<file alias="editinvert.png">images/editinvert.png</file>
<file alias="editswap.png">images/editswap.png</file>
<file alias="editzoom.png">images/editzoom.png</file>
<file alias="editmirror.png">images/editmirror.png</file>
<file alias="editunmirror.png">images/editunmirror.png</file>
<file alias="editmirrorhoriz.png">images/editmirrorhoriz.png</file>
<file alias="editmirrorvert.png">images/editmirrorvert.png</file>
<file alias="back.png">images/back.png</file>
<file alias="home.png">images/home.png</file>
<file alias="icon.png">images/icon.png</file>
<file>help/editmenu.html</file>
<file>help/filemenu.html</file>
<file>help/index.html</file>
</qresource>
</RCC>
/home/yrd/eric_workspace/charter6/ui_newimagedlg.py
# -*- coding: utf-8 -*-
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_NewImageDlg(object):
def setupUi(self, NewImageDlg):
NewImageDlg.setObjectName("NewImageDlg")
NewImageDlg.resize(287, 214)
self.gridlayout = QtWidgets.QGridLayout(NewImageDlg)
self.gridlayout.setContentsMargins(9, 9, 9, 9)
self.gridlayout.setSpacing(6)
self.gridlayout.setObjectName("gridlayout")
self.buttonBox = QtWidgets.QDialogButtonBox(NewImageDlg)
self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
self.buttonBox.setStandardButtons(QtWidgets.QDialogButtonBox.Cancel|QtWidgets.QDialogButtonBox.Ok)
self.buttonBox.setObjectName("buttonBox")
self.gridlayout.addWidget(self.buttonBox, 5, 1, 1, 2)
spacerItem = QtWidgets.QSpacerItem(269, 16, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
self.gridlayout.addItem(spacerItem, 4, 0, 1, 3)
self.colorLabel = QtWidgets.QLabel(NewImageDlg)
self.colorLabel.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.colorLabel.setFrameShadow(QtWidgets.QFrame.Raised)
self.colorLabel.setText("")
self.colorLabel.setScaledContents(True)
self.colorLabel.setObjectName("colorLabel")
self.gridlayout.addWidget(self.colorLabel, 3, 1, 1, 1)
self.label_3 = QtWidgets.QLabel(NewImageDlg)
self.label_3.setObjectName("label_3")
self.gridlayout.addWidget(self.label_3, 3, 0, 1, 1)
self.colorButton = QtWidgets.QPushButton(NewImageDlg)
self.colorButton.setObjectName("colorButton")
self.gridlayout.addWidget(self.colorButton, 3, 2, 1, 1)
self.brushComboBox = QtWidgets.QComboBox(NewImageDlg)
self.brushComboBox.setObjectName("brushComboBox")
self.gridlayout.addWidget(self.brushComboBox, 2, 1, 1, 2)
self.label_4 = QtWidgets.QLabel(NewImageDlg)
self.label_4.setObjectName("label_4")
self.gridlayout.addWidget(self.label_4, 2, 0, 1, 1)
self.label = QtWidgets.QLabel(NewImageDlg)
self.label.setObjectName("label")
self.gridlayout.addWidget(self.label, 0, 0, 1, 1)
self.label_2 = QtWidgets.QLabel(NewImageDlg)
self.label_2.setObjectName("label_2")
self.gridlayout.addWidget(self.label_2, 1, 0, 1, 1)
self.heightSpinBox = QtWidgets.QSpinBox(NewImageDlg)
self.heightSpinBox.setAlignment(QtCore.Qt.AlignRight)
self.heightSpinBox.setMinimum(8)
self.heightSpinBox.setMaximum(512)
self.heightSpinBox.setSingleStep(4)
self.heightSpinBox.setProperty("value", 64)
self.heightSpinBox.setObjectName("heightSpinBox")
self.gridlayout.addWidget(self.heightSpinBox, 1, 1, 1, 1)
self.widthSpinBox = QtWidgets.QSpinBox(NewImageDlg)
self.widthSpinBox.setAlignment(QtCore.Qt.AlignRight)
self.widthSpinBox.setMinimum(8)
self.widthSpinBox.setMaximum(512)
self.widthSpinBox.setSingleStep(4)
self.widthSpinBox.setProperty("value", 64)
self.widthSpinBox.setObjectName("widthSpinBox")
self.gridlayout.addWidget(self.widthSpinBox, 0, 1, 1, 1)
self.label_4.setBuddy(self.brushComboBox)
self.label.setBuddy(self.widthSpinBox)
self.label_2.setBuddy(self.heightSpinBox)
self.retranslateUi(NewImageDlg)
self.buttonBox.accepted.connect(NewImageDlg.accept)
self.buttonBox.rejected.connect(NewImageDlg.reject)
QtCore.QMetaObject.connectSlotsByName(NewImageDlg)
NewImageDlg.setTabOrder(self.widthSpinBox, self.heightSpinBox)
NewImageDlg.setTabOrder(self.heightSpinBox, self.brushComboBox)
NewImageDlg.setTabOrder(self.brushComboBox, self.colorButton)
NewImageDlg.setTabOrder(self.colorButton, self.buttonBox)
def retranslateUi(self, NewImageDlg):
_translate = QtCore.QCoreApplication.translate
NewImageDlg.setWindowTitle(_translate("NewImageDlg", "Image Chooser - New Image"))
self.label_3.setText(_translate("NewImageDlg", "Color"))
self.colorButton.setText(_translate("NewImageDlg", "&Color..."))
self.label_4.setText(_translate("NewImageDlg", "&Brush pattern:"))
self.label.setText(_translate("NewImageDlg", "&Width:"))
self.label_2.setText(_translate("NewImageDlg", "&Height:"))
self.heightSpinBox.setSuffix(_translate("NewImageDlg", " px"))
self.widthSpinBox.setSuffix(_translate("NewImageDlg", " px"))
/home/yrd/eric_workspace/charter6/newimagedlg.py
#-*-Coding:utf-8-*-"" "
Module implementing Newimagedlg.
" "" From Pyqt5.qtcore import pyqtslot,qt,qvariant