PyQt5 QFileDialog Closes When Filename Clicked
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 dialo
Solution 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_()
Post a Comment for "PyQt5 QFileDialog Closes When Filename Clicked"