'How to filter executables using QFileDialog? (Cross-platform solution)
The documentation for QFileDialog.getOpenFileName does not provide any clue on how to filter only executables using a const QString &filter = QString(). Here's the code for my action using PyQt5:
from PyQt5.QtWidgets import QAction, QFileDialog
from PyQt5.QtCore import QDir
from os import path
class OpenSourcePortAction(QAction):
def __init__(self, widget, setSourcePort, config, saveSourcePortPath):
super().__init__('&Open Source Port', widget)
self.widget = widget
self.setShortcut('Ctrl+O')
self.setStatusTip('Select a source port')
self.triggered.connect(self._open)
self.setSourcePort = setSourcePort
self.config = config
self.saveSourcePortPath = saveSourcePortPath
def _open(self):
options = QFileDialog.Options()
options |= QFileDialog.DontUseNativeDialog
fileName, _ = QFileDialog.getOpenFileName(
self.widget, "Select a source port", self.config.get("sourcePortDir"), "Source Ports (gzdoom zandronum)", options=options)
if fileName:
self.saveSourcePortPath(fileName)
self.setSourcePort(fileName)
On linux, naturally, I have no file extensions for executables, but I need to filter .exe extensions on windows (Which I intend to provide a version for). Also, there's no overloaded method that allows QDir::Executable. How can I use QFileDialog.getOpenFileName while filtering only executables, no matter which platform it's running on?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
