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. 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
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. However, I am unsure how this can be done in PYQ5. Furthermore, how to do this with a specific file extension.
I believe you're a bit confused on how QFileDialog works.
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.
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.
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:
Those are listed in the static members and you are probably interested in
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: