在qt QFileDialog setsuffix is not working in linux,如何解决?
In qt QFileDialog setsuffix is not working in linux, how to solve?
我正在为我的 qt 应用程序创建一个保存对话框。一切正常,但如果文件名后面没有添加文件扩展名,即使选择了过滤器,也不会自动使用文件扩展名保存。
我知道我需要设置一个 defaultsuffix 选项,但即使我这样做了,如果没有提供扩展名,它仍然不会自动添加。
我发现了其他几个类似的问题,我读到它在 windows 中有效,但在 linux 发行版上可能会失败。如果是这样,是否有简单的解决方法?因为现在,我没有可行的解决方案...
void MainWindow::on_actionSave_Chart_As_triggered()
{
QFileDialog *fileDialog = new QFileDialog;
fileDialog->setDefaultSuffix("files (*);;AstroQt aqt (*.aqt)");
QString fileName = fileDialog->getSaveFileName(this, "Save Radix", ui->label_2->text() +".aqt", "AstroQT(*.aqt)");
qDebug() << " save file name " << fileName << endl;
QFile file(fileName);
if (!file.open(QFile::WriteOnly | QFile::Text)) {
QMessageBox::warning(this, "Warning", "Cannot save file: " + file.errorString());
return;
}
setWindowTitle(fileName);
QTextStream out(&file);
QString text = "text that will be saved...";
out << text;
file.close();
}
编辑:尝试多种解决方案后,none 似乎奏效了。但它应该有,我想。为什么还有后缀函数...?现在我手动解决了它。但是我不满意,应该有更好的solution/explanation.
// add extension if none is found.
if(!fileName.endsWith(".aqt"))
fileName.append(".aqt");
如果您使用静态方法 getSaveFileName
似乎可以正常工作:
#include <QFileDialog>
#include <QApplication>
#include <QDebug>
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
QString fileName = QFileDialog::getSaveFileName(
nullptr, QObject::tr("Save File"),
"teste.aqt",
QObject::tr("AstroQt (*.aqt)"));
qDebug() << " save file name " << fileName << endl;
return app.exec();
}
如果我输入没有扩展名的内容,我会得到正确的文件名和扩展名。
我正在为我的 qt 应用程序创建一个保存对话框。一切正常,但如果文件名后面没有添加文件扩展名,即使选择了过滤器,也不会自动使用文件扩展名保存。
我知道我需要设置一个 defaultsuffix 选项,但即使我这样做了,如果没有提供扩展名,它仍然不会自动添加。
我发现了其他几个类似的问题,我读到它在 windows 中有效,但在 linux 发行版上可能会失败。如果是这样,是否有简单的解决方法?因为现在,我没有可行的解决方案...
void MainWindow::on_actionSave_Chart_As_triggered()
{
QFileDialog *fileDialog = new QFileDialog;
fileDialog->setDefaultSuffix("files (*);;AstroQt aqt (*.aqt)");
QString fileName = fileDialog->getSaveFileName(this, "Save Radix", ui->label_2->text() +".aqt", "AstroQT(*.aqt)");
qDebug() << " save file name " << fileName << endl;
QFile file(fileName);
if (!file.open(QFile::WriteOnly | QFile::Text)) {
QMessageBox::warning(this, "Warning", "Cannot save file: " + file.errorString());
return;
}
setWindowTitle(fileName);
QTextStream out(&file);
QString text = "text that will be saved...";
out << text;
file.close();
}
编辑:尝试多种解决方案后,none 似乎奏效了。但它应该有,我想。为什么还有后缀函数...?现在我手动解决了它。但是我不满意,应该有更好的solution/explanation.
// add extension if none is found.
if(!fileName.endsWith(".aqt"))
fileName.append(".aqt");
如果您使用静态方法 getSaveFileName
似乎可以正常工作:
#include <QFileDialog>
#include <QApplication>
#include <QDebug>
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
QString fileName = QFileDialog::getSaveFileName(
nullptr, QObject::tr("Save File"),
"teste.aqt",
QObject::tr("AstroQt (*.aqt)"));
qDebug() << " save file name " << fileName << endl;
return app.exec();
}
如果我输入没有扩展名的内容,我会得到正确的文件名和扩展名。