QFileDialog窗口中没有文件可见 [英] No files visible in the QFileDialog window

查看:400
本文介绍了QFileDialog窗口中没有文件可见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用pyqt编写一个简单的代码

在代码中,我调用了QFileDialog,但是当我使用静态函数调用它时,一切都很好,但是使用常规方法,即使用dialog.exec_(),,我在文件对话框窗口中看不到任何文件./p>

仅在键入文件的完整路径后,我才能在文件对话框窗口中看到该文件. 请注意,仅当我调用FileDialoghandler函数时才会出现此问题,如果我不这样做,无论我如何调用QFileDialog,一切都将正常进行. 而且这个问题仅在Linux上,在Windows7上一切正常. 我想知道这是PyQt错误还是我在这里遗漏了什么?

代码如下:

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

解决方案

不久前,我在使用getOpenFilename时遇到了类似的问题.对我而言,解决方案是将后端从本机更改为Qt自己的对话框实现.这可以通过如下扩展调用语法来实现:

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

更改为这种调用语法后,我再也没有遇到任何问题.

I am writing a simple code using 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 i.e. using dialog.exec_(), I do not see any files in the file dialog window.

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. And also this issue is only on Linux, on Windows7 everything works ok. I am wondering whether this is a PyQt bug or am I missing something here?

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. For me the solution was to change the backend from native to Qt's own implementation of the dialog. 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.

这篇关于QFileDialog窗口中没有文件可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆