PyQt5 QFileDialog closes when filename clicked

冷暖自知 提交于 2021-02-11 14:24:02

问题


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

Edit: 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.

2nd Edit: 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.

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


回答1:


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.

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.

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


来源:https://stackoverflow.com/questions/63177758/pyqt5-qfiledialog-closes-when-filename-clicked

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!