Use of QSAveFile instead a QFile.

This commit is contained in:
joshua
2019-07-01 20:12:12 +02:00
parent 0f93e028ba
commit 097645f5ff

View File

@@ -22,6 +22,7 @@
#include <QGraphicsSceneContextMenuEvent> #include <QGraphicsSceneContextMenuEvent>
#include <QAction> #include <QAction>
#include <QFileInfo> #include <QFileInfo>
#include <QSaveFile>
/** /**
Permet de convertir une chaine de caracteres ("n", "s", "e" ou "w") Permet de convertir une chaine de caracteres ("n", "s", "e" ou "w")
@@ -540,30 +541,37 @@ bool QET::compareCanonicalFilePaths(const QString &first, const QString &second)
what happened when this function returns false. what happened when this function returns false.
@return false if an error occurred, true otherwise @return false if an error occurred, true otherwise
*/ */
bool QET::writeXmlFile(QDomDocument &xml_doc, const QString &filepath, QString *error_message) { bool QET::writeXmlFile(QDomDocument &xml_doc, const QString &filepath, QString *error_message)
QFile file(filepath); {
QSaveFile file(filepath);
// Note: we do not set QIODevice::Text to avoid generating CRLF end of lines
bool file_opening = file.open(QIODevice::WriteOnly); // Note: we do not set QIODevice::Text to avoid generating CRLF end of lines
if (!file_opening) { bool file_opening = file.open(QIODevice::WriteOnly);
if (error_message) { if (!file_opening)
*error_message = QString( {
QObject::tr( if (error_message)
"Impossible d'ouvrir le fichier %1 en écriture, erreur %2 rencontrée.", {
"error message when attempting to write an XML file" *error_message = QString(QObject::tr("Impossible d'ouvrir le fichier %1 en écriture, erreur %2 rencontrée.",
) "error message when attempting to write an XML file")).arg(filepath).arg(file.error());
).arg(filepath).arg(file.error()); }
} return(false);
return(false); }
}
QTextStream out(&file);
QTextStream out(&file); out.setCodec("UTF-8");
out.setCodec("UTF-8"); out.setGenerateByteOrderMark(false);
out.setGenerateByteOrderMark(false); out << xml_doc.toString(4);
out << xml_doc.toString(4); if (!file.commit())
file.close(); {
if (error_message) {
return(true); *error_message = QString(QObject::tr("Une erreur est survenue lors de l'écriture du fichier %1, erreur %2 rencontrée.",
"error message when attempting to write an XML file")).arg(filepath).arg(file.error());
}
return false;
}
return(true);
} }
/** /**