Search code examples
pythonpyqtpyqt5qfiledialog

Add widget (QCheckBox) to QFileDialog in PyQt5 (Python) not working


I would like to add a QCheckBox to a QFileDialog. I would like to use the static method QFileDialog.getSaveFileName() to show the dialog.

I have found several similar questions, all in c++:

  1. How to add checkbox to QFileDialog window in QT3?
  2. Adding a widget in QFileDialog
  3. https://www.qtcentre.org/threads/42858-Creating-a-Custom-FileOpen-Dialog
  4. https://forum.qt.io/topic/103964/add-checkbox-to-qfiledialog/7

I did my best to translate these discussions to python, but have not gotten to the solution yet. My code runs, but the checkbox does not show up, even when I use QFileDialog.DontUseNativeDialog.

This is how I am subclassing QFileDialog:

from PyQt5.QtWidgets import QFileDialog
from PyQt5.QtWidgets import QCheckBox

class ChkBxFileDialog(QFileDialog):
    def __init__(self, chkBxTitle=""):
        super().__init__()
        self.setOption(QFileDialog.DontUseNativeDialog)
        chkBx = QCheckBox(chkBxTitle)
        self.layout().addWidget(chkBx)
    #end __init__
#end ChkBxFileDialog

I have run this in two ways.

Option 1 (with extra QFileDialog.DontUseNativeDialog):

import sys    
from PyQt5.QtWidgets import QApplication
if __name__ == "__main__":
        app = QApplication(sys.argv)
        fileDialog = ChkBxFileDialog(chkBxTitle="Chkbx")
        fileName = fileDialog.getSaveFileName(filter='*.txt', initialFilter='*.txt',
                                              options=QFileDialog.DontUseNativeDialog)[0]
        sys.exit(app.exec_())

Option 2 (without extra QFileDialog.DontUseNativeDialog):

import sys    
from PyQt5.QtWidgets import QApplication    
if __name__ == "__main__":
        app = QApplication(sys.argv)
        fileDialog = ChkBxFileDialog(chkBxTitle="Chkbx")
        fileName = fileDialog.getSaveFileName(filter='*.txt', initialFilter='*.txt')[0]
        sys.exit(app.exec_())

The checkbox doesn't show with either option. Option 1 uses different window styling. Option 2 shows the typical PyQt QFileDialog.

Does anyone know what I am missing?


Solution

  • I would like to use the static method QFileDialog.getSaveFileName() to show the dialog

    That's not possible. The static method as is defined in the C++ code knows nothing about your derived class, so it will create an instance of the base class, which doesn't contain your modifications. You have to explicitly instantiate your derived class, call exec() on the instance, check the return code and possibly call its selectedFiles() method to see what files were selected.