[英]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();
}
編輯:嘗試多種解決方案后,似乎都沒有奏效。 但它應該有,我想。 為什么還有后綴函數......? 現在我手動解決了它。 但我對此並不滿意,應該有更好的解決方案/解釋。
// 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();
}
如果我輸入不帶擴展名的內容,我會得到帶有擴展名的正確文件名。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.