Search code examples
pythonpyqt4qfiledialog

Setting QFileDialog


I am using static method:

path = QtGui.QFileDialog.getSaveFileName(self, SAVE_TO_STR, NAME_STR, 'CSV(*.csv)')

where I get path as full_path\some_name.csv

but I need to set different language to buttons and labels of dialog, so I've been looking at docs and find out that I can't do that with static method and I've come up with this code:

    ddd = QtGui.QFileDialog(self, SAVE_TO_IN_OTHER_LANGUAGE_STR, NAME_STR, 'CSV(*.csv)')
    ddd.setAcceptMode (QtGui.QFileDialog.AcceptSave)
    ddd.setLabelText( QtGui.QFileDialog.Accept, "Save - in other language" )
    ddd.setLabelText( QtGui.QFileDialog.Reject, "Cancel - in other language" )
    ddd.setLabelText( QtGui.QFileDialog.LookIn, "Look in - in other language" )
    if ddd.exec_():
        path = QtCore.QString(ddd.selectedFiles()[0])

I am trying to set it to look like first one so my questions are:

  1. path I get is ok, but missing .csv at the end, so it saves file with no extension. should I manually add .csv at the end of the path?

  2. when I choosing where to save and click on folder, "save" button turns to "open". How to change that button text to "Open" in other language?

  3. folders list at the left side of dialog is not complex as when I use QtGui.QFileDialog.getSaveFileName() , it shows only My Computer and User, instead of modern tree with favorites and partitions under My Computer.


Solution

  • 1) path I get is ok, but missing .csv at the end, so it saves file with no extension. should I manually add .csv at the end of the path?

    Answer: I think you shouldn't manually add .csv at the end of the path. In PyQt API have this solution to solve it, use QFileDialog.setDefaultSuffix (self, QString suffix);

    pathQFileDialog = QtGui.QFileDialog(self)
    pathQFileDialog.setAcceptMode(QtGui.QFileDialog.AcceptSave)
    pathQFileDialog.setNameFilter('CSV(*.csv)')
    pathQFileDialog.setDefaultSuffix('csv')
    

    Reference: http://pyqt.sourceforge.net/Docs/PyQt4/qfiledialog.html#setDefaultSuffix


    2) when I choosing where to save and click on folder, "save" button turns to "open". How to change that button text to "Open" in other language?

    Answer: My opinion of PyQt, No. In Qt (C++) at file qfiledialog.cpp I found your problem in method void QFileDialogPrivate::_q_updateOkButton() at line between 2886 and 2888. It force "&Open" label;

    button->setEnabled(enableButton);
    if (acceptMode == QFileDialog::AcceptSave)
        button->setText(isOpenDirectory ? QFileDialog::tr("&Open") : acceptLabel);
    

    Reference: https://qt.gitorious.org/qt/qt/source/57756e72adf2081137b97f0e689dd16c770d10b1:src/gui/dialogs/qfiledialog.cpp#L2796-2888


    3) folders list at the left side of dialog is not complex as when I use QtGui.QFileDialog.getSaveFileName() , it shows only My Computer and User, instead of modern tree with favorites and partitions under My Computer.

    Answer: Because on Windows, Mac OS X and Symbian^3, this static function (QtGui.QFileDialog.getSaveFileName()) will use the native file dialog and not a QFileDialog.

    Reference: pyqt.sourceforge.net/Docs/PyQt4/qfiledialog.html#getSaveFileName


    Regards,