@clarify said in File-save doesn't prepend extension?:
E.g. if my code tells the File Save dialog to save as a ".txt" file, then the user sees the .txt to the right of the cursor where he has to type the file name and he cannot delete the .txt.
I really don't see how your accepted solution satisfies your stated requirement. Which is why I said Qt does not offer this.
QDialog::setNameFiler() does nothing other than control what existing files QFileDialog displays. It does not even have any effect on the string(s) returned, which are just the current path in the viewport. Which may or may not have a .txt extension. You have to deal with that yourself in code.
Also, the solution you have now is a file dialog for opening a file with the intention of reading. You should add dialog.setAcceptMode(QFileDialog::AcceptSave) for your intended "File Save operation". This will (a) change the title of the dialog (from "Open" to "Save As") and (b) change behaviour if an existing file name is selected or typed in (so that it asks about overwriting, as a user would expect).
It's still not great as if there is a file named abc.txt and rather than selecting it the user types abc the file dialog does not treat this as abc.txt for checking, even though your code is likely to be appending the .txt according to your rule. But it's still better than leaving it on the default QFileDialog::AcceptOpen.
So far as I can see, your requirement is reasonable, and may be implemented in other applications, but is not well supported in Qt.