QFileDialog 将目录传递给 python 脚本

发布于 2024-07-17 02:03:52 字数 1687 浏览 8 评论 0原文

我正在编写一个小 python 程序,它会遍历 XML 文件并替换一些标签。 它需要三个参数:创建目录树的路径、读取的 XML 文件以及输出到的 xml 文件。 只需传递参数即可在命令行中正常工作。 因为它不仅仅适合我,我想我应该在它上面放一个 Qt 前端。 下面是Qt前端的大部分内容。 MOVtoMXF 是完成所有替换的类。 所以你可以看到,我基本上只是抓住字符串并将它们输入到我已经制作和测试的类中。

class Form(QDialog):

    def ConnectButtons(self):
        self.connect(self.pathBrowseB, SIGNAL("clicked()"), self.pathFileBrowse)
        self.connect(self.xmlFileBrowseB, SIGNAL("clicked()"), self.xmlFileBrowse)
        self.connect(self.outputFileBrowseB, SIGNAL("clicked()"), self.outputFileBrowse)

    def accept(self):
        path = self.pathBox.displayText()
        xmlFile = self.xmlFileBox.displayText()
        outFileName = self.outfileNameBox.displayText()
        print path + "  " + xmlFile + " " + outFileName
        mov1 = MOVtoMXF.MOVtoMXF(path, xmlFile, outFileName)
        mov1.ScanFile()
        self.done()

    def pathFileBrowse(self):
        file = str(QFileDialog.getExistingDirectory(self, "Select Directory"))
        self.pathBox.setText(file)

    def xmlFileBrowse(self):
        file = str(QFileDialog.getOpenFileName(self, "Save File"))
        self.xmlFileBox.setText(file)

    def outputFileBrowse(self):
        file = str(QFileDialog.getSaveFileName(self, "Save File"))
        self.outfileNameBox.setText(file)

问题是,当我输入路径时,它现在返回错误,要么目录不存在,要么文件

“/System/Library/Frameworks/Python.framework/Versions” 末尾有一个尾部斜杠/2.5/lib/python2.5/posixpath.py”,第 62 行,在 join 中 elif path == '' 或 path.endswith('/'):

我认为 QFileDialog、其传回的 QString 和我的 python 期望的字符串之间可能存在一些不匹配。 但我不知道如何修复它。

我在 Max OS X 10.5.6 上运行 pyQt 4.4.4 QT 4.4.0

感谢您提供的任何帮助。

标记

Im writing a little python program that goes through an XML file and does some replacement of tags. It takes three arguments, a path from whcih it creates a directory tree, the XML file its reading and the xml file its outputting to. It works fine from the command line just passing in arguments. As its not just for me, i thought id put a Qt front on it. Below is the majority of the Qt front. MOVtoMXF is the class that does all the replacement. So you can see that im basically just grabbing strings and feeding them into the class that ive already made and tested.

class Form(QDialog):

    def ConnectButtons(self):
        self.connect(self.pathBrowseB, SIGNAL("clicked()"), self.pathFileBrowse)
        self.connect(self.xmlFileBrowseB, SIGNAL("clicked()"), self.xmlFileBrowse)
        self.connect(self.outputFileBrowseB, SIGNAL("clicked()"), self.outputFileBrowse)

    def accept(self):
        path = self.pathBox.displayText()
        xmlFile = self.xmlFileBox.displayText()
        outFileName = self.outfileNameBox.displayText()
        print path + "  " + xmlFile + " " + outFileName
        mov1 = MOVtoMXF.MOVtoMXF(path, xmlFile, outFileName)
        mov1.ScanFile()
        self.done()

    def pathFileBrowse(self):
        file = str(QFileDialog.getExistingDirectory(self, "Select Directory"))
        self.pathBox.setText(file)

    def xmlFileBrowse(self):
        file = str(QFileDialog.getOpenFileName(self, "Save File"))
        self.xmlFileBox.setText(file)

    def outputFileBrowse(self):
        file = str(QFileDialog.getSaveFileName(self, "Save File"))
        self.outfileNameBox.setText(file)

the probelm is that when i feed in a Path, it now comes back with an error, either the directory doesnt exist, or if I have a trailing slash on the end that

File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/posixpath.py", line 62, in join
elif path == '' or path.endswith('/'):

I think its probably some mismatch between the QFileDialog, the QString its passing back and the string the my python expects. but im not sure how to go about fixing it.

Im running on Max OS X 10.5.6
pyQt 4.4.4
QT 4.4.0

thanks for any help you can give.

Mark

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绝情姑娘 2024-07-24 02:03:53

两个潜在的解决方案。

方法 1:

如果必须使用 displayText() 方法,我建议您使用显式字符串转换包装对 displayText() 的调用:

path = str(self.pathBox.displayText()) 
xmlFile = str(self.xmlFileBox.displayText()) 
outFileName = str(self.outfileNameBox.displayText())

原因是 displayText() 返回我认为是 C++ 级别的常量内存引用,这意味着您不会返回 QString 的副本,而是实际上返回内存引用中可用的任何 QString。

当您调用 displayText() 函数时,它是您期望的字符串,但最终当内存引用的内容发生更改时,它会是其他内容。 我注意到不同控件上的几种方法的这种特殊性,最明显的是 QDateEdit/QDateTimeEdit/QTimeEdit 控件,在这些控件中,我通常必须通过将 QDateEdit 的 date() 函数返回的 QDate 包装在一个显式副本中,将其包装在QDate 构造函数。

方法 2:

否则,请使用 text() 方法。 返回的 QString 是一个常量值,而不是常量内存引用。 请参阅此文档:

http://doc.trolltech.com/4.4/qlineedit。 html#text-prop

displayText : const QString 
text : QString

更新:

看来 Riverbank 将在 PyQt 的未来版本中解决此问题,以防有人仍然遇到此问题:

PyQt4 路线图

隐式复制 const&

在当前快照中实现。

当 PyQt 包装 const& 时 价值
由它包装的 C++ 函数返回
值本身的地址。 还,
它不强制执行 const
属性。 这可能会导致意外的
行为(和程序崩溃)
潜在价值消失
或值出乎意料
已修改。

处理这个问题的正确方法是
显式复制该值
使用其类型的复制构造函数。
然而,这不是Pythonic,而且
知道需要做
需要了解 C++ API。

PyQt 将被更改,以便它
自动调用副本
构造函数并将包装副本。

Two potential solutions.

Method 1:

If you must use the displayText() method, I suggest you wrap the call to displayText() with an explicit string cast:

path = str(self.pathBox.displayText()) 
xmlFile = str(self.xmlFileBox.displayText()) 
outFileName = str(self.outfileNameBox.displayText())

The reason is that displayText() returns what I believe is a constant memory reference at the C++ level, meaning that you are not being returned a copy of the QString, but actually whatever QString is available at the memory reference.

When you call the displayText() function, it is the string you expected, but eventually it is something else when the contents at the memory reference are changed. I have noticed this peculiarity with several methods on different controls, most notably QDateEdit/QDateTimeEdit/QTimeEdit controls, where I typically have to make an explicit copy of, say, the QDate returned by the date() function of QDateEdit by wrapping it in a QDate constructor.

Method 2:

Otherwise, use the text() method instead. The QString returned is a constant value, instead of a constant memory reference. See this doc:

http://doc.trolltech.com/4.4/qlineedit.html#text-prop

displayText : const QString 
text : QString

Update:

It looks like Riverbank will be addressing this problem in future versions of PyQt in case anybody is still having this problem:

PyQt4 Roadmap

Implicit Copying of const&

Implemented in current snapshots.

When PyQt wraps a const& value
returned by a C++ function it wraps
the address of the value itself. Also,
it does not enforce the const
attribute. This can cause unexpected
behavour (and program crashes) either
by the underlying value disappearing
or the value being unexpectedly
modified.

The correct way to handle this is to
explicitly make a copy of the value
using its type's copy constructor.
However, that is not Pythonic and
knowing that it needs to be done
requires knowledge of the C++ API.

PyQt will be changed so that it will
automatically invoke the copy
constructor and will wrap the copy.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文