How to remove the "confirm save as dialog box" from QFileDialog
-
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/labelImgAfter 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. -
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 ? -
@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.