PyQt5 QFileDialog 在单击文件名时关闭 [英] PyQt5 QFileDialog closes when filename clicked

查看:199
本文介绍了PyQt5 QFileDialog 在单击文件名时关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 PyQt5 QFileDialog.getOpenFileName.我希望盒子在打开"之前保持打开状态.按钮被点击.但是,当我在 Linux 系统上运行代码时,单击文件名时对话框会立即关闭.在 Windows 系统上,该框按预期运行并保持打开状态,直到单击打开"按钮.无论是否设置 QFileDialog.DontUseNativeDialog 选项,结果都相同.

I am using PyQt5 QFileDialog.getOpenFileName. I am expecting the box to remain open until the "open" button is clicked. However, when I run the code on my Linux system, the dialog box closes immediately when the file name is clicked. On a Windows system, the box behaves as expected and remains open until the 'Open' button is clicked. The results are the same with or without the QFileDialog.DontUseNativeDialog option set.

from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QFileDialog

import sys


class Main(QMainWindow):
    def __init__(self):
        super().__init__()

        self.setWindowTitle("QFileDialog Test")

        button = QPushButton("Click to open file")
        button.setCheckable(True)
        button.clicked.connect(self.open_file)

        # Set the central widget of the Window.
        self.setCentralWidget(button)

    def open_file(self):
        options = QFileDialog.Options()
        options |= QFileDialog.DontUseNativeDialog
        file_name, _ = QFileDialog.getOpenFileName(None, "Open File",
            "", "Python Files (*.py);;Text Files (*.txt)",options=options)


app = QApplication(sys.argv)
window = Main()
window.show()
app.exec_()

我注销了 KDE 并启动了一个 Openbox 会话,然后运行了上面的代码.QFileDialog 的行为符合我的预期,并等待我单击打开"按钮.这验证了 KDE/KWin 存在问题,并且在其他窗口管理器下运行的代码可能会正常工作.

I logged out of KDE and started an Openbox session instead, then ran the above code. QFileDialog behaved as I was expecting, and waited for me to click the Open button. This verifies the problem exists with KDE / KWin, and that the code, run under other window managers, will likely work fine.

仍然不是一个真正的解决方案,但我现在比以前更了解.

Still isn't a real solution, but I am more informed now than I was earlier.

第二次我发现如果我改变工作区行为 ->一般行为 ->单击行为从单击到双击,我的 QFileDialog 问题消失了.不过,如何解决这个问题将是一个不同的话题.

2nd I found that if I change Workspace Behavior -> General Behavior -> Click Behavior from Single click, to Double click, my QFileDialog issue goes away. How to get around this would be a different topic though.

第三次在示例代码中添加了QFileDialog.DontUseNativeDialog"选项.

3rd Added 'QFileDialog.DontUseNativeDialog' option to sample code.

推荐答案

似乎 Qt 试图尊重操作系统在其文件管理器中打开文件和文件夹的方式,即使在使用本机对话框时也是如此.这取决于 SH_ItemView_ActivateItemOnSingleClick 样式提示,而绕过它的唯一方法是应用代理样式.

It seems like Qt tries to respect the way the OS opens files and folders in its file manager, even when using the native dialog. That depends on the SH_ItemView_ActivateItemOnSingleClick style hint, and the only way to bypass it is to apply a proxy style.

虽然您可以在其 __init__ 中将样式应用于 QFileDialog 的视图(只要您使用本机对话框),但您使用的是静态方法,因此您只能通过将样式设置为整个 QApplication.

While you could apply the style to the QFileDialog's view within its __init__ (as long as you're using the native dialog), you're using static methods, so you can only do this by setting the style to the whole QApplication.

请注意,与样式表、调色板和字体不同,样式不会传播到子小部件,并且它们总是使用 QApplication 样式(或为它们手动设置的样式).

Note that, unlike stylesheets, palette and font, styles are not propagated to children widgets, and they always use the QApplication style (or the style manually set for them).

class SingleClickWorkaroundProxy(QProxyStyle):
    def styleHint(self, hint, option, widget, data):
        if hint == self.SH_ItemView_ActivateItemOnSingleClick:
            return False
        return super().styleHint(hint, option, widget, data)

# ...
app = QApplication(sys.argv)
app.setStyle(SingleClickWorkaroundProxy())
window = Main()
window.show()
app.exec_()

这篇关于PyQt5 QFileDialog 在单击文件名时关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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