Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Override default GUI and functionality of QFileDialog globally



  • Hello,

    I am currently working with an open source aplication that uses the QtGui.QFileDialog.htm from PySide.
    https://doc.qt.io/qtforpython/PySide2/QtWidgets/QFileDialog.html

    This dialog is used hundreds of times in the application so manually replacing all instances is not really an option.

    I was hoping that there might be a way to override the current functionality of this dialog and replacing it with one of my own design. The one of my own design would talk to a server for its listing copy some files locally after a user selected them before allowing the application to continue normally.

    Any help would be great,


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    First thing: a good IDE would allow you to replace QFileDialog with your own class pretty easily and would be the right thing to do with regard to longterm maintenance and peace of mind.

    Now since it's python, you can apply some black magic. The brutal way to do it would be something like:

    import QtWidgets
    
    class MyCoolFileDialog(QtWidgets.QWidget):
        ....
    QtWidgets.QFileDialog = MyCoolFileDialog
    

    I am guessing that you might just be using one of the static method of QFileDialog, if that's the case, then just replace the static method rather that the whole class.

    The right term is monkey patching.

    You definitely want to put some warning somewhere about what you are doing here and document it properly.



  • @SGaist
    So that is what "monkey patching" is? [:shudders:]



  • Thanks.
    This was really helpful.


  • Banned

    This post is deleted!

Log in to reply