【问题标题】:How to filter executables using QFileDialog? (Cross-platform solution)如何使用 QFileDialog 过滤可执行文件? (跨平台解决方案)
【发布时间】:2021-02-14 21:14:07
【问题描述】:

documentation for QFileDialog.getOpenFileName 没有提供关于如何使用const QString &filter = QString() 仅过滤可执行文件的任何线索。这是我使用 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)

在 linux 上,我自然没有可执行文件的文件扩展名,但我需要过滤 windows 上的 .exe 扩展名(我打算为其提供一个版本)。此外,没有允许QDir::Executable 的重载方法。如何在仅过滤可执行文件时使用QFileDialog.getOpenFileName,无论它在哪个平台上运行?

【问题讨论】:

    标签: python pyqt pyqt5 qfiledialog


    【解决方案1】:

    如果您想要一个更加个性化的过滤器,那么您必须使用 proxyModel,但为此您不能使用 getOpenFileName 方法,因为 QFileDialog 实例不易访问,因为它是一个静态方法。

    class ExecutableFilterModel(QSortFilterProxyModel):
        def filterAcceptsRow(self, source_row, source_index):
            if isinstance(self.sourceModel(), QFileSystemModel):
                index = self.sourceModel().index(source_row, 0, source_index)
                fi = self.sourceModel().fileInfo(index)
                return fi.isDir() or fi.isExecutable()
            return super().filterAcceptsRow(source_row, source_index)
    
    
    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):
            proxy_model = ExecutableFilterModel()
            options = QFileDialog.Options()
            options |= QFileDialog.DontUseNativeDialog
            dialog = QFileDialog(
                self.widget, "Select a source port", self.config.get("sourcePortDir")
            )
            dialog.setOptions(options)
            dialog.setProxyModel(proxy_model)
            if dialog.exec_() == QDialog.Accepted:
                filename = dialog.selectedUrls()[0].toLocalFile()
                self.saveSourcePortPath(fileName)
                self.setSourcePort(fileName)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-03-11
      • 2012-04-01
      • 1970-01-01
      • 2023-02-07
      • 1970-01-01
      • 2010-10-10
      • 2012-04-28
      相关资源
      最近更新 更多