简体   繁体   English

QFileDialog-保存具有指定文件名的文件

[英]QFileDialog - Saving a file with specified file name

How can I let the user save a file using QFileDialog without allowing the user to change the file name? 如何让用户使用QFileDialog保存文件而不允许用户更改文件名?

I have been using this code: 我一直在使用此代码:

QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"),"commands.txt", tr("TXT files (*.txt)"));

However, it allows the user to change the file name. 但是,它允许用户更改文件名。 I want it so that the user can't change the file name and can only save the file under "commands.txt". 我想要它,以便用户不能更改文件名,而只能将文件保存在“ commands.txt”下。

If you want to choose the folder to save commands.txt use QFileDialog::getExistingDirectory and save the file into the specified folder. 如果要选择保存QFileDialog::getExistingDirectory的文件夹,请使用QFileDialog::getExistingDirectory并将文件保存到指定的文件夹中。 Don't forget to check returned string isEmpty() , this means that user clicked "cancel". 不要忘记检查返回的字符串isEmpty() ,这意味着用户单击了“取消”。

If you want to let user decide to save commands.txt or not use QMessageBox with Yes and No buttons. 如果要让用户决定保存QMessageBox还是不使用,请使用带有Yes和No按钮的QMessageBox

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM