简体   繁体   中英

Python PyQt Pyside - setNameFilters in QFileDialog does not work

(Windows 7 64 Bit, PyCharm 3.4.1 Pro, Python 3.4.0, PySide 1.2.2)

I want to make a file dialog with filters and preselect one filter.

If i use the static method, it works, i can use filters and preselect one filter.

dir = self.sourceDir
filters = "Text files (*.txt);;Images (*.png *.xpm *.jpg)"
selected_filter = "Images (*.png *.xpm *.jpg)"
fileObj = QFileDialog.getOpenFileName(self, " File dialog ", dir, filters, selected_filter)

If i use an object it does not work, my filters are not there.

file_dialog = QFileDialog(self)
file_dialog.setNameFilters("Text files (*.txt);;Images (*.png *.jpg)")
file_dialog.selectNameFilter("Images (*.png *.jpg)")
file_dialog.getOpenFileName() 

Why does this not work?

在此输入图像描述在此输入图像描述

You have misunderstood how QFileDialog works.

The functions getOpenFileName , getSaveFileName , etc are static . They create an internal file-dialog object, and the arguments to the function are used to set properties on it.

But when you use the QFileDialog constructor, it creates an external instance, and so setting properties on it have no effect on the internal file-dialog object created by the static functions.

What you have to do instead, is show the external instance you created:

file_dialog = QFileDialog(self)
# the name filters must be a list
file_dialog.setNameFilters(["Text files (*.txt)", "Images (*.png *.jpg)"])
file_dialog.selectNameFilter("Images (*.png *.jpg)")
# show the dialog
file_dialog.exec_()

Global solution is here. You can get directory, filename and file extension. You can use that:

d = Dialog(self)
        d.filters = ['A dosyaları (*.aaa)', 'B Dosyaları (*.bbb)', 'C Dosyaları (*.ccc)', 'Tüm Dosyalar (*.*)']
        d.default_filter_index = 3
        d.exec()
        print(d.filename)
        print(d.path)

and class is here!

from PyQt5.QtWidgets import QFileDialog, QDialog
import os


class Dialog():

    def __init__(self, mainform):
        self.__mainform = mainform
        self.__dialog = QFileDialog()
        self.__directory = ''
        self.__filename = ['', '', '']
        self.__filters = []
        self.__default_filter_index = 0
        self.__path = ''

    @property
    def path(self):
        return self.__path

    @property
    def filename(self):
        return self.__filename

    @property
    def directory(self):
        return self.__directory

    @directory.setter
    def directory(self, value):
        self.__directory = value

    @property
    def filters(self):
        return self.__filters

    @filters.setter
    def filters(self, value):
        self.__filters = value

    @property
    def default_filter_index(self):
        return self.__default_filter_index

    @default_filter_index.setter
    def default_filter_index(self, value):
        self.__default_filter_index = value

    def exec(self, load =True):
        self.__dialog.setNameFilters(self.__filters)
        self.__dialog.selectNameFilter(self.__filters[self.__default_filter_index])
        self.__dialog.setDirectory(self.__directory)
        if load == True:
            self.__dialog.setLabelText(QFileDialog.Accept, 'Aç')
            self.__dialog.setWindowTitle('Aç')
        else:
            self.__dialog.setLabelText(QFileDialog.Accept, 'Kaydet')
            self.__dialog.setWindowTitle('Kaydet')
        if self.__dialog.exec() == QDialog.Accepted:
            self.__path = self.__dialog.selectedFiles()[0]
            fn = os.path.split(self.__path)
            ex = os.path.splitext(self.__path)[1]
            self.__filename = [fn[0], fn[1], ex[1:len(ex)]]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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