c++ - Append missing extension to result of QFileDialog -


By using

QFileDialog , I prompt the user for a file name and add an extension I am missing.

One suggestion was to add an extension manually after the fact that the problem with this approach is that the user can not be warned that they are overwriting the file (for example, test.txt exists, the user enters test and is not indicated that they are actually overwriting test.txt )

I have read all the documents on QFileDialog and I E has not been successful. I tried setDefaultSuffix , but the result of fileDialog.getSaveFileName () was not expanded. Can someone provide an example of how to do it successfully?

Unfortunately, it seems that when you are using the "native" file dialog, this functionality Not available . However, it works as if you expect that you use the QT file dialog.

Here's how you can use the QT file dialog:

  QFileDialog saveDialog; SaveDialog.setAcceptMode (QFileDialog :: AcceptSave); SaveDialog.setDefaultSuffix ("txt"); SaveDialog.exec (); QString file = saveDialog.selectedFiles (). first ();   

As an alternative, you can apply your own overwrite verification:

  QString file; Do {file = QFileDialog :: getSaveFileName (); If (! File.endsWith (".txt")) {file.append (".txt"); If (QFile :: exist (file)) {if (QMessageBox :: Yes! = QMessageBox :: question (NULL, QString (), "Confirmation Overwrite?", QMessageBox :: Yes | QMessageBox :: No) {file. clear(); }}}} Whereas (file.isEmpty ());    

Comments

Popular posts from this blog

How can I add a seperate action listener to all 14 of my buttons at once that can determine player turn?(java) -

php - Sorting an multidimension array using usort fails -

Simple file compression in C -