pyqt5-查找文档 [英] pyqt5 - finding documentation

查看:367
本文介绍了pyqt5-查找文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在研究Summerfields关于使用Python和QT进行快速GUI编程的书... pyqt更加精确,但是2007年的书使用的是第4版.我正在尝试使用当前的5.4版.2 ..

I have been working my way through Summerfields book on Rapid GUI programming with Python and QT... pyqt to be preciser, but the book from 2007 uses version 4.something 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)))

这不起作用,可能主要是因为在pyqt5中,QFileDialog返回一个元组而不是字符串.我能弄清楚这一点的唯一方法就是反复试验. pyqt5文档将您引向QT,我真的不明白.

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.

我有以下工作要做:

   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()

哇,行得通!但是,只有通过努力,我才能取得任何进展. 我尝试运行python解释器并输入:

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)

这很有帮助,但是对我来说,帮助的语法没有多大意义,而且我看不到getSaveFileName应该返回什么.这是一些乏味的@@ $$的东西.

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. This is some tedious-@$$ stuff.

我想念什么?

推荐答案

QFileDialog的某些静态函数在PyQt中具有奇怪的历史记录.如果您不了解这段历史,就很难理解PyQt各个版本之间的区别.

Some of the static functions of QFileDialog have an odd history in PyQt. If you don't know this history, it's hard to make sense of the differences between the various versions of PyQt.

根本问题很简单.在Python中,如果一个函数需要返回多个值,则最常见的解决方案是返回一个元组.但是在C ++中,这实际上是不可能的,因此通常的解决方案是改为提供可以修改的参数.

The underlying issue is quite simple. In Python, if a function needs to return more than one value, the most common solution is to return a tuple. But in C++, this is not really possible, so the usual solution is to instead provide arguments that can be modified.

QFileDialog.getSaveFileName 的C ++签名是这样的:

getSaveFileName(
    QWidget * parent = 0, const QString & caption = String(),
    const QString & dir = QString(), const QString & filter = QString(),
    QString * selectedFilter = 0, Options options = 0)

如您所见,四个QString参数并不完全相同.前三个是const,因此不会被该函数修改,但是selectedFilter参数采用指向QString的指针,这意味着可以.

As you can see, the four QString arguments aren't all the same. The first three are const, and so won't be modifed by the function, but the selectedFilter argument takes a pointer to a QString, which means it can be.

最初,PyQt的主要用途是C ++原型设计(而不是开发Python应用程序),因此PyQt的API更忠实于Qt API.这意味着直到PyQt-4.6,从QFileDialog获取所选过滤器的 only 方法,都是以C ++方式进行的,如下所示:

Originally, the main use of PyQt was for C++ proto-typing (rather than developing Python applications), and so its APIs were much more faithful to the Qt APIs. This meant that, up until PyQt-4.6, the only way to get the selected filter from QFileDialog, was to do it the C++ way, like this:

>>> s = QString() # string to be modified
>>> f = QFileDialog.getSaveFileName(None, 'Save', '', 'Img(*.png *.jpg)', s)
>>> print s
Img(*.png *.jpg)

事实上,这仍然适用于当前版本的PyQt4(当然,前提是已启用QString).

And in fact, this still works in current versions of PyQt4 (providing it has QString enabled, of course).

PyQt4稳定地引入了许多更改,这些年来逐渐使它变得越来越对Python友好-但是如上面的示例所示,这一切都做到了 而没有破坏向后兼容性.当时,更改getSaveFileName的签名以返回元组会造成太多损坏,因此,添加了getSaveFileNameAndFilter之类的功能作为暂时的妥协.

PyQt4 steadily introduced a lot of changes that have gradually made it more and more Python-friendly over the years - but as the above example shows, this was all done without breaking backwards-compatibility. At the time, changing the signature of getSaveFileName to return a tuple would have caused far too much breakage, and so functions like getSaveFileNameAndFilter were added as a temporary compromise instead.

PyQt5没有这样的限制(甚至不再需要提供QString).因此,终于有可能做正确的事情(从Python的角度来看),而只需从getSaveFileName返回一个元组.现在,该原则通常适用:如果您使用的是PyQt5,并且在Qt文档中看到一个修改其参数的函数,则始终可以期望返回一个元组.

PyQt5 does not have such restrictions (it doesn't even need to provide QString anymore). So it has finally become possible to do the right thing (from a Python point of view) and just return a tuple from getSaveFileName. And this principle now applies in general: if you're using PyQt5, and you see a function in the Qt docs that modifies its arguments, you can always expect a tuple to be returned instead.

(PS:比PyQt还年轻的PySide用户从来不必处理这些问题.对于他们来说,静态QFileDialog函数总是做正确的事情.)

(PS: users of PySide - which is much younger than PyQt - have never had to deal with these issues. For them, the static QFileDialog functions have always done the right thing).

这篇关于pyqt5-查找文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆