PyQt4 文件选择小部件
我想制作一个 QT4(使用 QT 设计器)对话框,其中包含必须选择文件的部分。
现在,我知道 QFileDialog 存在,并且我可以编写一些程序来执行我想要的操作。 但我也可以在QT设计器中完成吗?
有没有办法在 QT 设计器中获取“文件选择”小部件? 或者,我记得这些按钮,将选定的文件作为标题和一个小箭头,允许用户通过 QFileDialog 选择另一个文件?
那么有现成的解决方案吗,还是我必须自己编程?
I want to make a QT4 (using QT designer) dialog, that contains a part where a file has to be selected.
Now, I know QFileDialog exists, and I can program something that does what I want.
But can I also just do it in QT designer?
Is there some way to get a "file select" widget in QT designer?
Or, I remember these buttons, having the selected file as a title and a little arrow allowing the user to select another file by the QFileDialog?
So is there a ready made solution, or do I have to program it myself?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
据我所知,Qt 设计器没有提供文件对话框。但您可以通过几行代码轻松完成。
假设您有一个名为 PushButton 的简单按钮,并且路径应存储在 lineEdit 中。
[edit]只是想知道,您是否使用过 KDE?如果是这样,您可以使用 KUrlRequester 来实现此目的。它可以轻松配置为支持从文件到 URL 到目录的任何内容。
There is no file dialog available from the Qt designer as far as I know. But you can easily do it with a few lines of code.
Assuming you have a simple button called pushButton and the path should be stored in lineEdit.
[edit]Just wondering though, are you using KDE by any chance? If so, than you can use the KUrlRequester for this. It can easily be configured to support anything from files to urls to directories.
QFileDialog
存在于QtGui
中。至少在我的 4.4 版本中是这样,可能也更早。我认为它不在设计器中的原因是因为它打开自己的窗口,而不是作为放置在另一个窗口上的小部件。QTDesigner 的文档可能会更好,至少暗示了它的存在。
实例化它并运行
show
命令。它会立即出现并默认为/
。QFileDialog
exists inQtGui
. At least in my version 4.4 and probably much earlier too. I think the reason it is not in Designer is because it opens its own window instead of being a widget to place on another window.The documentation from QTDesigner could be better and at least hint of its existence.
Instantiate it and run the
show
command. It comes right up and defaults to/
.您可以使用QFileDialog类中的getOpenFileName()方法。
QFileDialog.getOpenFileName() 将返回文件路径和选定的文件类型
我得到这个:('C:/Users/Sathsara/Desktop/UI/Test/test. py', 'All Files (*)')
要仅获取文件路径,请使用 QFileDialog.getOpenFileName()[0]
示例代码:
You can use method getOpenFileName() in QFileDialog Class.
QFileDialog.getOpenFileName() will return the file path and the selected file type
I got this : ('C:/Users/Sathsara/Desktop/UI/Test/test.py', 'All Files (*)')
To get only the file path use QFileDialog.getOpenFileName()[0]
Sample code: