简体   繁体   English

如何使用QFileDialog打开.mid后缀的文件

[英]How to use QFileDialog to open file with .mid suffix

I have created a subclass for an option to Open File.我为打开文件的选项创建了一个子类。 Alongside PYQT5, I have imported the python library Mido & py-midi in order to read the MIDI files.除了 PYQT5,我还导入了 python 库 Mido & py-midi 以读取 MIDI 文件。 If my logic is correct.如果我的逻辑是正确的。 I will use PYQT5's FileDialog in order to retrieve a file, assign it to a variable and then use Mido to read that said MIDI file when I Will then use py-midi to edit these files我将使用 PYQT5 的 FileDialog 来检索文件,将其分配给变量,然后使用 Mido 读取该 MIDI 文件,然后我将使用 py-midi 编辑这些文件

class OpenDialog(QFileDialog):

    def __init__(self, *args, **kwargs):
        super(OpenDialog, self).__init__(*args, **kwargs)

        self.setWindowTitle("Open")
        self.setFixedSize(1000, 450)

        buttons = QDialogButtonBox.Open | QDialogButtonBox.Cancel

        self.buttonBox = QDialogButtonBox(buttons)
        self.buttonBox.accepted.connect(self.accept)
        self.buttonBox.rejected.connect(self.reject)

        self.layout = QVBoxLayout()
        self.layout.addWidget(self.buttonBox)
        self.setLayout(self.layout)

        # OpenedFile =

I have commented out OpenedFile becuase i plan to use this as a variable to link to the opened files.我已经注释掉了 OpenedFile,因为我计划将其用作链接到打开文件的变量。 However, I am unsure how this can be done in PYQ5.但是,我不确定如何在 PYQ5 中完成此操作。 Furthermore, how to do this with a specific file extension.此外,如何使用特定的文件扩展名执行此操作。

I believe you're a bit confused on how QFileDialog works.我相信您对 QFileDialog 的工作方式有些困惑。

First of all, by default Qt tries to use the native file dialog the system provides, so generally you should not try to create your own by subclassing, unless you need very special behavior.首先,默认情况下 Qt 尝试使用系统提供的本机文件对话框,因此通常您不应该尝试通过子类化来创建自己的文件,除非您需要非常特殊的行为。

Then, QFileDialog is a QDialog that already has its own (private) layout and widgets, including the area in which files and folders are shown, a text field for the path, and standard Open/Cancel buttons.然后,QFileDialog 是一个 QDialog,它已经有自己的(私有)布局和小部件,包括显示文件和文件夹的区域、路径的文本字段和标准的打开/取消按钮。

Since you only need to open a specific file type, there's absolutely no need for subclassing, as explained at the very beginning of the documentation :由于您只需要打开特定的文件类型,因此绝对不需要子类化,如文档开头所述:

The easiest way to create a QFileDialog is to use the static functions.创建 QFileDialog 的最简单方法是使用 static 函数。

Those are listed in the static members and you are probably interested in getOpenFileName() ;这些列在static 成员中,您可能对getOpenFileName()感兴趣; do note that the static functions for files (not those for directories) always return a tuple including the path(s) and selected file type filter:请注意,文件(不是目录)的 static 函数总是返回一个元组,包括路径和选定的文件类型过滤器:

    def showOpenFileDialog(self):
        fileName, filter = QFileDialog.getOpenFileName(self, 'Open file', 
            'some/default/path/', 'MIDI files (*.mid)')
        if fileName:
            self.openMidiFile(fileName)

Try the following script:尝试以下脚本:

class FileDialog(QtWidgets.QFileDialog):

    def __init__(self, *args, **kwargs):
        super(FileDialog, self).__init__(*args, **kwargs)
        self.setOption(QFileDialog.DontUseNativeDialog, True)
        self.setNameFilters(["Excel File (*.xlsx *.xls)","CSV File (*.csv)","Log File (*.log)"])
        self.setFileMode(QFileDialog.ExistingFiles)

    def accept(self):
        super(FileDialog, self).accept()

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

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