python - pyqt5 - finding documentation -


i have been working way through summerfields book on rapid gui programming python , qt... pyqt preciser, book 2007 uses version 4.something , trying going current version 5.4.2..

there changes trying figure out , love assistance on how find stuff. here example file save dialog - book:

    fname = qfiledialog.getsavefilename(self,             "image changer - save image", fname,             "image files ({})".format(" ".join(formats))) 

this not work, perhaps because in pyqt5 qfiledialog returns tuple rather string. way can figure out trial , error. pyqt5 documentation refers qt not understand.

i got following 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, works! slogging through progress. tried running python interpreter , typed:

from pyqt5.qtwidgets import  qfiledialog  help(qfiledialog) 

this (sort of) helpful, syntax of not make lot of sense me, , not see getsavefilename supposed return. tedious-@$$ stuff.

what missing?

some of static functions of qfiledialog have odd history in pyqt. if don't know history, it's hard make sense of differences between various versions of pyqt.

the underlying issue quite simple. in python, if function needs return more 1 value, common solution return tuple. in c++, not possible, usual solution instead provide arguments can modified.

the c++ signature of qfiledialog.getsavefilename this:

getsavefilename(     qwidget * parent = 0, const qstring & caption = string(),     const qstring & dir = qstring(), const qstring & filter = qstring(),     qstring * selectedfilter = 0, options options = 0) 

as can see, 4 qstring arguments aren't same. first 3 const, , won't modifed function, selectedfilter argument takes pointer qstring, means can be.

originally, main use of pyqt c++ proto-typing (rather developing python applications), , apis more faithful qt apis. meant that, until pyqt-4.6, only way selected filter qfiledialog, c++ way, this:

>>> s = qstring() # string modified >>> f = qfiledialog.getsavefilename(none, 'save', '', 'img(*.png *.jpg)', s) >>> print s img(*.png *.jpg) 

and in fact, still works in current versions of pyqt4 (providing has qstring enabled, of course).

pyqt4 steadily introduced lot of changes have gradually made more , more python-friendly on years - above example shows, done without breaking backwards-compatibility. @ time, changing signature of getsavefilename return tuple have caused far breakage, , functions getsavefilenameandfilter added temporary compromise instead.

pyqt5 not have such restrictions (it doesn't need provide qstring anymore). has become possible right thing (from python point of view) , return tuple getsavefilename. , principle applies in general: if you're using pyqt5, , see function in qt docs modifies arguments, can expect tuple returned instead.


(ps: users of pyside - younger pyqt - have never had deal these issues. them, static qfiledialog functions have done right thing).


Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -