I have been working my way through Summerfields book on Rapid GUI programming with Python and QT (PyQt to be more precise), but the book from 2007 uses version 4.x and I am trying to get going with the current version (5.4.2).
There are some changes that I am trying to figure out and would love some assistance on how to find stuff. Here is an example for a file save dialog - from the book:
fname = QFileDialog.getSaveFileName(self,
"Image Changer - Save Image", fname,
"Image files ({})".format(" ".join(formats)))
This does not work, perhaps primarily because in PyQt5 the QFileDialog
returns a tuple rather than a string. The only way I can figure this out is just by trial and error. The PyQt5 documentation refers you to QT, which I really do not understand.
I got the following to work:
fname = QFileDialog.getSaveFileName(self, 'some text',
"whatever.png", '*.png')
if "." not in fname[0]:
fname[0] += ".png"
self.addRecentFile(fname[0])
self.filename = fname[0]
return self.fileSave()
Wow, it works! But it is just by slogging through that I get any progress. I tried running python interpreter and typed:
from PyQt5.QtWidgets import QFileDialog
help(QFileDialog)
This is (sort of) helpful, but the syntax of the help does not make a lot of sense to me, and I do not see what getSaveFileName
is supposed to return.
What am I missing?
These methods of
QFileDialog
appear to be a bit special because PyQt have implemented their own methods rather than directly wrapping the Qt methods.Firstly, the PyQt5
QFileDialog.getSaveFileName()
method implements the behaviour of theQFileDialog.getSaveFileNameAndFilter()
method from PyQt4 (source). Secondly, theQFileDialog.getSaveFileNameAndFilter()
method in PyQt4 returns a tuple of(filename, selectedFilter)
(source).For reference, the calling signature of the PyQt4
QFileDialog.getSaveFileNameAndFilter()
method isHopefully this helps resolve any confusion. Most PyQt5 classes/methods will not be this confusing to decode!