Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. How to remove the "confirm save as dialog box" from QFileDialog
Forum Update on Tuesday, May 27th 2025

How to remove the "confirm save as dialog box" from QFileDialog

Scheduled Pinned Locked Moved Unsolved Qt for Python
3 Posts 2 Posters 723 Views 1 Watching
  • 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.
  • D Offline
    D Offline
    duddal_
    wrote on last edited by
    #1

    Hello guys,

    Hope you are doing well amid this pandemic.

    I have been working on extending the functionality of the following:
    https://github.com/tzutalin/labelImg

    After hitting the save button for the first time, an XML file with the basename as that of the image is created. But after hitting the save button the next time, a dialog box pops up and asks for the confirmation to overwrite the existing file (the file which is selected). I want to eliminate this behavior.

    What I want is: Once the file is selected the first time and when the same file is saved again, it should not open a dialog box to select the particular file and open a "confirm save as" dialog box. Once the file is selected the first time, without opening the dialog box and ask the user to select the same file, the file should be selected automatically.

    I have checked the QT Documentation and found that there is a parameter called "DontConfirmOverwrite" in the QFileDialog.getSaveFileName. But this method "getSaveFileName" is not used in the aforementioned script. I think the changes are to be made in the following section of the code (Line 1386) but not completely sure:

        def saveFile(self, _value=False):
            if self.defaultSaveDir is not None and len(ustr(self.defaultSaveDir)):
                if self.filePath:
                    imgFileName = os.path.basename(self.filePath)
                    savedFileName = os.path.splitext(imgFileName)[0]
                    savedPath = os.path.join(ustr(self.defaultSaveDir), savedFileName)
                    self._saveFile(savedPath)
            else:
                imgFileDir = os.path.dirname(self.filePath)
                imgFileName = os.path.basename(self.filePath)
                savedFileName = os.path.splitext(imgFileName)[0]
                savedPath = os.path.join(imgFileDir, savedFileName)
                self._saveFile(savedPath if self.labelFile
                               else self.saveFileDialog(removeExt=False))
    
        def saveFileAs(self, _value=False):
            assert not self.image.isNull(), "cannot save empty image"
            self._saveFile(self.saveFileDialog())
    
        def saveFileDialog(self, removeExt=True):
            caption = '%s - Choose File' % __appname__
            filters = 'File (*%s)' % LabelFile.suffix
            openDialogPath = self.currentPath()
            dlg = QFileDialog(self, caption, openDialogPath, filters)
            dlg.setDefaultSuffix(LabelFile.suffix[1:])
            dlg.setAcceptMode(QFileDialog.AcceptSave)
            filenameWithoutExtension = os.path.splitext(self.filePath)[0]
            dlg.selectFile(filenameWithoutExtension)
            dlg.setOption(QFileDialog.DontUseNativeDialog, False)
            if dlg.exec_():
                fullFilePath = ustr(dlg.selectedFiles()[0])
                if removeExt:
                    return os.path.splitext(fullFilePath)[0] # Return file path without the extension.
                else:
                    return fullFilePath
            return ''
    
        def _saveFile(self, annotationFilePath):
            if annotationFilePath and self.saveLabels(annotationFilePath):
                self.setClean()
                self.statusBar().showMessage('Saved to  %s' % annotationFilePath)
                self.statusBar().show()
    

    One more thing I tried is taking the filename selected the first time and using that to save the file every time but it didn't work either.

    I appreciate any help I can get. Thanks!!
    Take care.

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      But if you call
      self._saveFile(savedPath)
      dont it save without showing a dialog ?

      Your logic seems ok as you ask
      do i have defaultSaveDir then just save , else use dialog.

      But i dont see you set defaultSaveDir when using the dialog.
      Do you set it somewhere ?

      1 Reply Last reply
      0
      • D Offline
        D Offline
        duddal_
        wrote on last edited by
        #3

        @mrjj,
        Thanks for the reply.

        defaultSaveDir is an argument passed during running the script. (Argument Parser).

        But I found another way to set the DefaultSaveDir. If anyone wants to eliminate this behavior:
        Go to the "OpenDirDialog" function and set defaultDirPath as targetDirPath. This will do the trick. This is only helpful though if you want to save the annotation files (XML files) in the same directory as that of images. This is a workaround.

        1 Reply Last reply
        1

        • Login

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