Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QFileDialog not responding to done() method as QDialog does

QFileDialog not responding to done() method as QDialog does

Scheduled Pinned Locked Moved Unsolved General and Desktop
19 Posts 6 Posters 1.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MrAWD
    wrote on 7 Dec 2022, 15:26 last edited by
    #1

    I need to have a bit more control of the QFileDialog widget when "Save" button are pressed (I am using QFileDialog.AcceptSave as accept mode), so I created my own version of the dialog derived from QFileDialog. When I am using regular QDialog, I could control this by overriding done() method and there decide if dialog will be closed or not.

    Overriding the save method from the QFileDialog gets that event, but it always closes the dialog regardless of what gets returned. Here is the simple code for this:

    from PySide2.QtWidgets import QApplication, QDialog, QFileDialog
    import sys
    
    class MyFileDlg(QFileDialog):
    
        def __init__(self, parent=None , *args, **kwargs):
            super().__init__(parent, *args, **kwargs)
            self.setAcceptMode(QFileDialog.AcceptSave)
            
        def done(self, result):
            if result == QDialog.Rejected:
                print("accepted as Rejected and it should close with the next line")
                return QFileDialog.done(self, result)
    
            print("Accepting without any return, but it still closes dialog")
    
    if __name__ == '__main__':
        app = QApplication(sys.argv)
        dlg = MyFileDlg()
        dlg.exec_()
        sys.exit(app.exec_())   
    

    Is there another way to do this?

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 7 Dec 2022, 18:45 last edited by
      #2

      Hi,

      Which OS are you on ?
      What exactly do you want to do if accepted ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • M Offline
        M Offline
        MrAWD
        wrote on 7 Dec 2022, 18:57 last edited by
        #3

        I am currently on Windows 10, but this should work on Linux too.

        What I am trying to do here?
        When user selects the name and folder for the file to be saved, the file contains information that could collide with existing files, so I would like to keep the dialog up and tell them they need to change something there in order to save it.

        I guess, I could close the dialog, pop a message, and than open another one, but looks inefficient and missing the flow that would be there if dialog was regular QDialog

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 7 Dec 2022, 19:26 last edited by
          #4

          How do you determine that the collision will happen ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • M Offline
            M Offline
            MrAWD
            wrote on 7 Dec 2022, 20:11 last edited by
            #5

            All the files were parsed at the startup of the app, so I know what is there. Those files contain particular components and they have to have unique names. If someone tries to save another file with component name that is already in that folder, procedure has to be stopped and user has to either change component name or save it to a different folder.

            But, I don't know where is user going to save the file until it presses on Save button. At that point I was hoping to intercept that action, pop the message to tell them what they need to do there in order to proceed - all while dialog is still up.

            I could close the dialog, get the info about where the file is going to saved, than check it, pop the message, and reopen the dialog. But, I don't think that is the right way to do this

            J M 2 Replies Last reply 7 Dec 2022, 20:29
            0
            • M MrAWD
              7 Dec 2022, 20:11

              All the files were parsed at the startup of the app, so I know what is there. Those files contain particular components and they have to have unique names. If someone tries to save another file with component name that is already in that folder, procedure has to be stopped and user has to either change component name or save it to a different folder.

              But, I don't know where is user going to save the file until it presses on Save button. At that point I was hoping to intercept that action, pop the message to tell them what they need to do there in order to proceed - all while dialog is still up.

              I could close the dialog, get the info about where the file is going to saved, than check it, pop the message, and reopen the dialog. But, I don't think that is the right way to do this

              J Offline
              J Offline
              JonB
              wrote on 7 Dec 2022, 20:29 last edited by
              #6

              @MrAWD
              If this is still unresolved tomorrow (it's night here!) I will take a look. It seems surprising you say you can accomplish your desire with QDialog::done() but not from QFileDialog::done().

              M 1 Reply Last reply 7 Dec 2022, 21:49
              0
              • M MrAWD
                7 Dec 2022, 20:11

                All the files were parsed at the startup of the app, so I know what is there. Those files contain particular components and they have to have unique names. If someone tries to save another file with component name that is already in that folder, procedure has to be stopped and user has to either change component name or save it to a different folder.

                But, I don't know where is user going to save the file until it presses on Save button. At that point I was hoping to intercept that action, pop the message to tell them what they need to do there in order to proceed - all while dialog is still up.

                I could close the dialog, get the info about where the file is going to saved, than check it, pop the message, and reopen the dialog. But, I don't think that is the right way to do this

                M Offline
                M Offline
                mpergand
                wrote on 7 Dec 2022, 20:56 last edited by mpergand 12 Jul 2022, 20:56
                #7

                @MrAWD
                I can't test on Windows, but on Mac and Linux, a warning dialog pops up if the file name your enter already exist.
                It's not like that on Windows ?

                For your "done" issue, have a try with a non native file dialog :
                setOption(QFileDialog::DontUseNativeDialog)

                M 1 Reply Last reply 7 Dec 2022, 21:59
                0
                • J JonB
                  7 Dec 2022, 20:29

                  @MrAWD
                  If this is still unresolved tomorrow (it's night here!) I will take a look. It seems surprising you say you can accomplish your desire with QDialog::done() but not from QFileDialog::done().

                  M Offline
                  M Offline
                  MrAWD
                  wrote on 7 Dec 2022, 21:49 last edited by
                  #8

                  @JonB said in QFileDialog not responding to done() method as QDialog does:

                  @MrAWD
                  If this is still unresolved tomorrow (it's night here!) I will take a look. It seems surprising you say you can accomplish your desire with QDialog::done() but not from QFileDialog::done().

                  I agree! I have used this feature before and works as designed. It is puzzling why would a dialog that is derived from the same code work differently.

                  1 Reply Last reply
                  0
                  • M mpergand
                    7 Dec 2022, 20:56

                    @MrAWD
                    I can't test on Windows, but on Mac and Linux, a warning dialog pops up if the file name your enter already exist.
                    It's not like that on Windows ?

                    For your "done" issue, have a try with a non native file dialog :
                    setOption(QFileDialog::DontUseNativeDialog)

                    M Offline
                    M Offline
                    MrAWD
                    wrote on 7 Dec 2022, 21:59 last edited by
                    #9

                    @mpergand said in QFileDialog not responding to done() method as QDialog does:

                    @MrAWD
                    I can't test on Windows, but on Mac and Linux, a warning dialog pops up if the file name your enter already exist.
                    It's not like that on Windows ?

                    For your "done" issue, have a try with a non native file dialog :
                    setOption(QFileDialog::DontUseNativeDialog)

                    Actually, I just tried it and it one works as it should and doesn't close the dialog due to code in done() method when "Save" button is clicked.
                    It looks like this has something to do with that native Windows "Save As" dialog that is ignoring what overridden done() is doing

                    1 Reply Last reply
                    0
                    • M Offline
                      M Offline
                      MrAWD
                      wrote on 8 Dec 2022, 01:54 last edited by MrAWD 12 Aug 2022, 01:56
                      #10

                      I have been trying to make things to work without using native dialog, but performances of this other dialog are really bad. It takes more than a second to load the content of the directory to the list (even though only two files were there) and that is a big turn off. It is also ugly as hell! :)

                      I will have to figure out how to use native dialog here, since this one is not going to cut it. If you have any other ideas on what to do here, I would appreciate it.

                      J 1 Reply Last reply 8 Dec 2022, 13:37
                      0
                      • Kent-DorfmanK Offline
                        Kent-DorfmanK Offline
                        Kent-Dorfman
                        wrote on 8 Dec 2022, 08:41 last edited by
                        #11

                        Kind of a stab in the dark here, but what I remember about dialogs was taht they can be modal or non-modal, thus having different implications of how to submit their data. QFileDialog is probably modal by default, but QDialog may not be. Something in the far reaches of my memory hints at this being relevant.

                        1 Reply Last reply
                        0
                        • M MrAWD
                          8 Dec 2022, 01:54

                          I have been trying to make things to work without using native dialog, but performances of this other dialog are really bad. It takes more than a second to load the content of the directory to the list (even though only two files were there) and that is a big turn off. It is also ugly as hell! :)

                          I will have to figure out how to use native dialog here, since this one is not going to cut it. If you have any other ideas on what to do here, I would appreciate it.

                          J Offline
                          J Offline
                          JonB
                          wrote on 8 Dec 2022, 13:37 last edited by
                          #12

                          @MrAWD
                          I said I would get back to you once tested. I can only say that under Linux (Qt5.15.x, PySide2) the code does behave as you want it to, in particular the "Save" dialog does stay visible after "accepting". And it does so with either the native or Qt file dialogs. So your behaviour must be a Windows native dialog issue?

                          M 1 Reply Last reply 8 Dec 2022, 13:48
                          0
                          • J JonB
                            8 Dec 2022, 13:37

                            @MrAWD
                            I said I would get back to you once tested. I can only say that under Linux (Qt5.15.x, PySide2) the code does behave as you want it to, in particular the "Save" dialog does stay visible after "accepting". And it does so with either the native or Qt file dialogs. So your behaviour must be a Windows native dialog issue?

                            M Offline
                            M Offline
                            MrAWD
                            wrote on 8 Dec 2022, 13:48 last edited by
                            #13

                            @JonB said in QFileDialog not responding to done() method as QDialog does:

                            @MrAWD
                            I said I would get back to you once tested. I can only say that under Linux (Qt5.15.x, PySide2) the code does behave as you want it to, in particular the "Save" dialog does stay visible after "accepting". And it does so with either the native or Qt file dialogs. So your behaviour must be a Windows native dialog issue?

                            Jon, thanks for testing this and confirming that Linux side works as expected.

                            Now, what to do about that Windows one that is my primary platform here?

                            1 Reply Last reply
                            0
                            • S Offline
                              S Offline
                              SGaist
                              Lifetime Qt Champion
                              wrote on 8 Dec 2022, 13:54 last edited by
                              #14

                              The thing is: one Windows and macOS, it's the native dialog that is used however there's no such thing on Linux.
                              Try to force the use of non-native file dialog and see if it does what you want.

                              Interested in AI ? www.idiap.ch
                              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                              J 1 Reply Last reply 8 Dec 2022, 14:14
                              0
                              • S SGaist
                                8 Dec 2022, 13:54

                                The thing is: one Windows and macOS, it's the native dialog that is used however there's no such thing on Linux.
                                Try to force the use of non-native file dialog and see if it does what you want.

                                J Offline
                                J Offline
                                JonB
                                wrote on 8 Dec 2022, 14:14 last edited by JonB 12 Aug 2022, 14:16
                                #15

                                @SGaist said in QFileDialog not responding to done() method as QDialog does:

                                The thing is: one Windows and macOS, it's the native dialog that is used however there's no such thing on Linux.

                                ?? Dialog is quite different under Ubuntu (GNOME) with vs without DontUseNativeDialog! It most certainly does have a native dialog :) I think Linux/Ubuntu may lack a directory selector native dialog, but that's quite different/not this case.

                                1 Reply Last reply
                                0
                                • S Offline
                                  S Offline
                                  SGaist
                                  Lifetime Qt Champion
                                  wrote on 8 Dec 2022, 14:26 last edited by
                                  #16

                                  Might be related to Gnome using GTK.

                                  Interested in AI ? www.idiap.ch
                                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                  J 1 Reply Last reply 8 Dec 2022, 14:30
                                  1
                                  • S SGaist
                                    8 Dec 2022, 14:26

                                    Might be related to Gnome using GTK.

                                    J Offline
                                    J Offline
                                    JonB
                                    wrote on 8 Dec 2022, 14:30 last edited by
                                    #17

                                    @SGaist Oh, OK, I only have experience of Ubuntu/GNOME, which I tested earlier for the OP.

                                    1 Reply Last reply
                                    0
                                    • K Offline
                                      K Offline
                                      Kaostheory
                                      wrote on 28 Mar 2025, 16:36 last edited by
                                      #18

                                      I know the post is quite old, but I'm also facing the issue on windows, using PyQt5.
                                      Did you find a way to prempt the closure of the native windows filedialog ?

                                      M 1 Reply Last reply 28 Mar 2025, 20:23
                                      0
                                      • K Kaostheory
                                        28 Mar 2025, 16:36

                                        I know the post is quite old, but I'm also facing the issue on windows, using PyQt5.
                                        Did you find a way to prempt the closure of the native windows filedialog ?

                                        M Offline
                                        M Offline
                                        MrAWD
                                        wrote on 28 Mar 2025, 20:23 last edited by
                                        #19

                                        @Kaostheory said in QFileDialog not responding to done() method as QDialog does:

                                        I know the post is quite old, but I'm also facing the issue on windows, using PyQt5.
                                        Did you find a way to prempt the closure of the native windows filedialog ?

                                        The way I made it work for me was to use non native dialog...nothing else worked back in days. Since those days, I am using pySide6 now (no more pySide2 here) and I didn't retest original behavior - now thinking I should have!

                                        The same goes for you with PyQt5 and test with PyQt6 might be an option to try.

                                        If I get around to test this again with pySide6, I will update this thread. Good luck!

                                        1 Reply Last reply
                                        0

                                        • Login

                                        • Login or register to search.
                                        • First post
                                          Last post
                                        0
                                        • Categories
                                        • Recent
                                        • Tags
                                        • Popular
                                        • Users
                                        • Groups
                                        • Search
                                        • Get Qt Extensions
                                        • Unsolved