Python PyQt Pyside - setNameFilters in QFileDialog does not work

寵の児 提交于 2020-01-23 02:46:10

问题


(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?


回答1:


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_()



回答2:


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)]]


来源:https://stackoverflow.com/questions/27994017/python-pyqt-pyside-setnamefilters-in-qfiledialog-does-not-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!