PyQt4 文件选择小部件

发布于 2024-09-08 08:54:43 字数 237 浏览 8 评论 0原文

我想制作一个 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 技术交流群。

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

发布评论

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

评论(3

故乡的云 2024-09-15 08:54:43

据我所知,Qt 设计器没有提供文件对话框。但您可以通过几行代码轻松完成。

假设您有一个名为 PushButton 的简单按钮,并且路径应存储在 lineEdit 中。

from PyQt4.QtGui import QFileDialog

def selectFile():
    lineEdit.setText(QFileDialog.getOpenFileName())

pushButton.clicked.connect(selectFile)

[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.

from PyQt4.QtGui import QFileDialog

def selectFile():
    lineEdit.setText(QFileDialog.getOpenFileName())

pushButton.clicked.connect(selectFile)

[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.

不奢求什么 2024-09-15 08:54:43

QFileDialog 存在于 QtGui 中。至少在我的 4.4 版本中是这样,可能也更早。我认为它不在设计器中的原因是因为它打开自己的窗口,而不是作为放置在另一个窗口上的小部件。

QTDesigner 的文档可能会更好,至少暗示了它的存在。

实例化它并运行 show 命令。它会立即出现并默认为 /

import QtGui
self.fileDialog = QtGui.QFileDialog(self)
self.fileDialog.show()

QFileDialog exists in QtGui. 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 /.

import QtGui
self.fileDialog = QtGui.QFileDialog(self)
self.fileDialog.show()
别在捏我脸啦 2024-09-15 08:54:43

您可以使用QFileDialog类中的getOpenFileName()方法。

QFileDialog.getOpenFileName() 将返回文件路径和选定的文件类型

我得到这个:('C:/Users/Sathsara/Desktop/UI/Test/test. py', 'All Files (*)')

要仅获取文件路径,请使用 QFileDialog.getOpenFileName()[0]


示例代码:

def selectFile():
   print(QFileDialog.getOpenFileName()[0])


dlg.locationBtn.clicked.connect(selectFile)

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:

def selectFile():
   print(QFileDialog.getOpenFileName()[0])


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