I designed a window class with a button allowing to select a folder making use of the Qt5 QFileDialog.
#!/usr/bin/env python3
from PyQt5 import QtWidgets, QtGui, QtCore
class VerificationWindow(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("Test")
layout = QtWidgets.QGridLayout()
self.label2 = QtWidgets.QLabel("PATH")
self.line2 = QtWidgets.QLineEdit()
self.btn2 = QtWidgets.QPushButton("Browse")
self.btn2.clicked.connect(self.getPATH)
layout.addWidget(self.label2, 0, 0)
layout.addWidget(self.line2, 0, 1)
layout.addWidget(self.btn2, 0, 2)
self.closebtn = QtWidgets.QPushButton("Close")
self.closebtn.clicked.connect(self.close_clicked)
layout.addWidget(self.closebtn, 2, 0)
self.setLayout(layout)
def getPATH(self):
dlg = QtWidgets.QFileDialog(self)
dlg.setWindowTitle("Test Directory Selection")
dlg.setFileMode(dlg.FileMode.Directory)
options = dlg.Options()
options |= dlg.DontUseNativeDialog
dlg.setDirectory("/home/stefan/ARCHIVE/SPECTRA")
if dlg.exec():
fn = dlg.selectedFiles()
print(fn)
self.line2.setText(' '.join(fn))
def close_clicked(self):
self.close()
def main():
app = QtWidgets.QApplication(sys.argv)
window = VerificationWindow()
window.show()
sys.exit(app.exec_())
return
if __name__ == "__main__":
main()
I expect that when running this code, the main window opens and when clicking the Browse button, the dialog starts showing me the content of the specified directory. This is not happening, I see the list of folders in the home directory, independent of which directory I specify in the setDirectory parameter.
What is going wrong in my code?