QFileDialog window does not display files

I am writing simple code using pyqt

In the code, I call QFileDialog , but when I call it using static functions, everything works fine, but with the usual method, that is, using dialog.exec_(), I do not see any files in the file dialog box.

Only after entering the full path of the file can I see the file in the file dialog box. Note that this problem only occurs when the FileDialoghandler function is called. If I do not, no matter how I call QFileDialog , everything works fine. And also this problem is only in Linux, on Windows7 everything works fine. I am wondering if this is a PyQt bug or am I missing something here?

The code is as follows:

 import sys from PyQt4.QtCore import Qt from PyQt4.QtGui import * from PyQt4.QtCore import QAbstractFileEngine from PyQt4.QtCore import QAbstractFileEngineHandler from PyQt4.QtCore import QFSFileEngine class FileDialogHandler(QAbstractFileEngineHandler): def create(self,filename): if str(filename).startswith(':'): return None # Will be handled by Qt as a resource file print("Create QFSFileEngine for {0}".format(filename)) return QFSFileEngine(filename) class Example(QMainWindow): def __init__(self): super(Example, self).__init__() self.initUI() def initUI(self): self.textEdit = QTextEdit() self.setCentralWidget(self.textEdit) self.statusBar() openFile = QAction(QIcon('open.png'), 'Open', self) openFile.setShortcut('Ctrl+O') openFile.setStatusTip('Open new File') openFile.triggered.connect(self.showDialog) menubar = self.menuBar() fileMenu = menubar.addMenu('&File') fileMenu.addAction(openFile) self.setGeometry(300, 300, 350, 300) self.setWindowTitle('File dialog') self.show() def showDialog(self): handler = FileDialogHandler() #using QFileDialog.getOpenFileName works fine fname = QFileDialog.getOpenFileName(None, 'Open file', '/home','All files (*.*)') #dialog = QFileDialog() #dialog.setOption(QFileDialog.DontUseNativeDialog,False) #if dialog.exec_(): #fname = dialog.selectedFiles() #else: #fname = None f = open(fname, 'r') with f: data = f.read() self.textEdit.setText(data) def main(): app = QApplication(sys.argv) ex = Example() sys.exit(app.exec_()) if __name__ == '__main__': main() 
+6
source share
1 answer

I ran into a similar problem recently with getOpenFilename. For me, the solution was to change the backend from native to Qt's own implementation of the dialog. This can be achieved using the advanced call syntax, which looks like this:

 filename = QtGui.QFileDialog.getOpenFileName(self, 'Open file', '/home', 'All files (*.*)', options=QtGui.QFileDialog.DontUseNativeDialog) 

After I changed this call syntax, I had no more problems.

+6
source

Source: https://habr.com/ru/post/924242/


All Articles