Qt6: deal with changes from QStringRef to QStringView

QStringRef is gone in Qt6 and mostly replaced by QStringView.  The one major
difference is that direct comparisons with string literals are no longer
possible.

Thanks to Thiago Macieira for helping me avoid more conditional compilation
here.

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
Dirk Hohndel 2022-02-09 16:04:33 -08:00
parent 47d900bee5
commit 78361ef8e3
3 changed files with 25 additions and 7 deletions

16
core/namecmp.h Normal file
View file

@ -0,0 +1,16 @@
// SPDX-License-Identifier: GPL-2.0
#ifndef NAMECMP_H
#define NAMECMP_H
#ifdef __cplusplus
#include <QXmlStreamReader>
// this is annoying Qt5 / Qt6 incompatibility where we can't compare against string literals anymore
static inline int nameCmp(QXmlStreamReader &r, const char * cs)
{
return r.name().compare(QLatin1String(cs));
}
#endif
#endif // NAMECMP_H

View file

@ -1,6 +1,7 @@
// SPDX-License-Identifier: GPL-2.0
#include "core/parse-gpx.h"
#include "core/subsurface-time.h"
#include "core/namecmp.h"
#include <QFile>
#include <QXmlStreamReader>
@ -43,7 +44,7 @@ int getCoordsFromGPXFile(struct dive_coords *coords, QString fileName)
while (!gpxReader.atEnd()) {
gpxReader.readNext();
if (gpxReader.isStartElement()) {
if (gpxReader.name() == "trkpt") {
if (nameCmp(gpxReader, "trkpt") == 0) {
trkpt_found = true;
line++;
foreach (const QXmlStreamAttribute &attr, gpxReader.attributes()) {
@ -53,7 +54,7 @@ int getCoordsFromGPXFile(struct dive_coords *coords, QString fileName)
lon = attr.value().toString().toDouble();
}
}
if (gpxReader.name() == "time" && trkpt_found) { // Ignore the <time> element in the GPX file header
if (nameCmp(gpxReader, "time") == 0 && trkpt_found) { // Ignore the <time> element in the GPX file header
QString dateTimeString = gpxReader.readElementText();
bool ok;
tm1.tm_year = dateTimeString.left(4).toInt(&ok, 10); // Extract the date/time components:

View file

@ -1,6 +1,7 @@
// SPDX-License-Identifier: GPL-2.0
#include "desktop-widgets/subsurfacewebservices.h"
#include "core/qthelper.h"
#include "core/namecmp.h"
#include "core/webservice.h"
#include "core/settings/qPrefCloudStorage.h"
#include "desktop-widgets/mainwindow.h"
@ -157,7 +158,7 @@ static DiveListResult parseDiveLogsDeDiveList(const QByteArray &xmlData)
DiveListResult result;
result.idCount = 0;
if (reader.readNextStartElement() && reader.name() != "DiveDateReader") {
if (reader.readNextStartElement() && nameCmp(reader, "DiveDateReader") != 0) {
result.errorCondition = invalidXmlError;
result.errorDetails =
gettextFromC::tr("Expected XML tag 'DiveDateReader', got instead '%1")
@ -166,8 +167,8 @@ static DiveListResult parseDiveLogsDeDiveList(const QByteArray &xmlData)
}
while (reader.readNextStartElement()) {
if (reader.name() != "DiveDates") {
if (reader.name() == "Login") {
if (nameCmp(reader, "DiveDates") != 0) {
if (nameCmp(reader, "Login") == 0) {
QString status = reader.readElementText();
// qDebug() << "Login status:" << status;
@ -185,11 +186,11 @@ static DiveListResult parseDiveLogsDeDiveList(const QByteArray &xmlData)
// process <DiveDates>
seenDiveDates = true;
while (reader.readNextStartElement()) {
if (reader.name() != "date") {
if (nameCmp(reader, "date") != 0) {
// qDebug() << "Skipping" << reader.name();
continue;
}
QStringRef id = reader.attributes().value("divelogsId");
auto id = reader.attributes().value("divelogsId");
// qDebug() << "Found" << reader.name() << "with id =" << id;
if (!id.isEmpty()) {
result.idList += id.toLatin1();