简体   繁体   English

QFileDialog 窗口中没有可见的文件

[英]No files visible in the QFileDialog window

I am writing a simple code using pyqt我正在使用 pyqt 编写一个简单的代码

In the code, I invoke a QFileDialog , however when I invoke it using the static functions all works fine, but with the normal method ie using dialog.exec_(), I do not see any files in the file dialog window.在代码中,我调用了QFileDialog ,但是当我使用静态函数调用它时一切正常,但是使用普通方法,即使用dialog.exec_(),我在文件对话框窗口中看不到任何文件。

Only after typing the complete path of the file can I see the file in the file dialog window.只有在输入文件的完整路径后,我才能在文件对话框窗口中看到该文件。 Note that this issue is only when I invoke the FileDialoghandler function, If I don't do that, no matter how I invoke the QFileDialog , everything works fine.请注意,此问题仅在我调用FileDialoghandler函数时出现,如果我不这样做,无论我如何调用QFileDialog ,一切正常。 And also this issue is only on Linux, on Windows7 everything works ok.而且这个问题只在 Linux 上,在 Windows7 上一切正常。 I am wondering whether this is a PyQt bug or am I missing something here?我想知道这是 PyQt 错误还是我在这里遗漏了什么?

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

I encountered a similar problem not long ago with the getOpenFilename. 我不久前用getOpenFilename遇到了类似的问题。 For me the solution was to change the backend from native to Qt's own implementation of the dialog. 对我来说,解决方案是将后端从本机更改为Qt自己的对话框实现。 This can be achieved withan extended calling syntax which looks like: 这可以通过扩展的调用语法实现,如下所示:

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

After I changed to this calling syntax I never had any problems again. 在我改为这个调用语法后,我再也没有遇到任何问题。

Encountered the same problem in Windows 10 running the code from command prompt without any IDE.在没有任何 IDE 的情况下从命令提示符运行代码的 Windows 10 中遇到了同样的问题。 Including options=QtGui.QFileDialog.DontUseNativeDialog did solve the problem.包括 options=QtGui.QFileDialog.DontUseNativeDialog 确实解决了这个问题。 (with python 3.10). (使用python 3.10)。 For Example:例如:

self.path_open, _ = QFileDialog.getOpenFileName(self, "Open file", "", "e-documents (*.docx *.pdf)",options=QFileDialog.DontUseNativeDialog) self.path_open, _ = QFileDialog.getOpenFileName(self, "Open file", "", "e-documents (*.docx *.pdf)",options=QFileDialog.DontUseNativeDialog)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM