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,19 +541,18 @@ 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 // Note: we do not set QIODevice::Text to avoid generating CRLF end of lines
bool file_opening = file.open(QIODevice::WriteOnly); bool file_opening = file.open(QIODevice::WriteOnly);
if (!file_opening) { if (!file_opening)
if (error_message) { {
*error_message = QString( if (error_message)
QObject::tr( {
"Impossible d'ouvrir le fichier %1 en écriture, erreur %2 rencontrée.", *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" "error message when attempting to write an XML file")).arg(filepath).arg(file.error());
)
).arg(filepath).arg(file.error());
} }
return(false); return(false);
} }
@@ -561,7 +561,15 @@ bool QET::writeXmlFile(QDomDocument &xml_doc, const QString &filepath, QString *
out.setCodec("UTF-8"); out.setCodec("UTF-8");
out.setGenerateByteOrderMark(false); out.setGenerateByteOrderMark(false);
out << xml_doc.toString(4); out << xml_doc.toString(4);
file.close(); if (!file.commit())
{
if (error_message) {
*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); return(true);
} }