I think there is a bug in PyQt5 when handling QDialogButtonBox.Open in QFileDialog. If I change the button text, when I select a file in the list it recovers the original text.
With QDialogButtonBox.Cancel there is no problem.
Here is my code:
import sys
import os
from PyQt5.QtWidgets import (QFileDialog, QDialogButtonBox, QApplication, QListView)
from PyQt5.QtCore import (QCoreApplication, QMetaObject)
class OpenFileDialog(QFileDialog):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# Path
self.path = os.getcwd()
options = self.Options()
options = self.DontUseNativeDialog
self.setOptions(options)
# Ok and Cancel buttons
d_button = self.findChild(QDialogButtonBox, "buttonBox")
d_button.setFixedWidth(110)
self.but_ok = d_button.button(QDialogButtonBox.Open)
self.but_ok.setText("Seleccionar")
self.but_cancel = d_button.button(QDialogButtonBox.Cancel)
self.but_cancel.setText("Cancelar")
self.but_ok.clicked.connect(self.click_ok_button)
self.but_cancel.clicked.connect(self.click_cancel_button)
# My solution
fil_lst = self.findChild(QListView, "listView")
fil_lst.clicked.connect((self.selectitem))
# Call translate
self.retranslate(self)
def selectitem(self):
self.but_ok.setText("Seleccionar")
def click_ok_button(self):
print("Select file")
def click_cancel_button(self):
self.close()
def closeEvent(self, event):
print("Select None")
def retranslate(self, open_file_dialog):
_translate = QCoreApplication.translate
self.but_ok.setText(_translate("Open", "Seleccionar"))
self.but_cancel.setText(_translate("Cancel", "Cancelar"))
if __name__ == '__main__':
app = QApplication(sys.argv)
dialog = OpenFileDialog()
dialog.show()
sys.exit(app.exec())
I tried changing the text with Qt translator and the problem happens too.
I solved it by changing the text every time the list is clicked, but it is not a pretty solution.
Does anyone know a more pythonic solution?
1
Have you tried using setLabelText()?
class OpenFileDialog(QFileDialog):
def __init__(self, *args, **kwargs):
...
self.setLabelText(QFileDialog.Accept, "Seleccionar")
self.setLabelText(QFileDialog.Reject, "Cancelar")
...