Skip to content

Commit

Permalink
Fixed compile against Qt 6.8.2
Browse files Browse the repository at this point in the history
In Qt 6.8.2, a forward declaration was added for the QStringRef class
from Qt 5, which breaks compilation because it is incompatible with the
"using QStringRef = QStringView" line I was using for compatibility
between Qt 5 and 6.

Fortunately, as of Qt 5.15.2, the QStringView API is complete enough, so
by raising the minimum required Qt version we no longer need QStringRef
at all.
  • Loading branch information
bjorn committed Feb 3, 2025
1 parent 339b504 commit 776d3b6
Show file tree
Hide file tree
Showing 23 changed files with 24 additions and 134 deletions.
2 changes: 1 addition & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Added support for SVG 1.2 / CSS blending modes to layers (#3932)
* AutoMapping: Don't match rules based on empty input indexes
* AutoMapping: Optimized reloading of rule maps and load rule maps on-demand
* Raised minimum supported Qt version from 5.12 to 5.15
* Raised minimum supported Qt version from 5.12 to 5.15.2

### Tiled 1.11.2 (28 Jan 2025)

Expand Down
2 changes: 1 addition & 1 deletion src/libtiled/libtiled.qbs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ DynamicLibrary {
cpp.dynamicLibraryPrefix: "lib"

Depends { name: "cpp" }
Depends { name: "Qt"; submodules: "gui"; versionAtLeast: "5.15" }
Depends { name: "Qt"; submodules: "gui"; versionAtLeast: "5.15.2" }

Probes.PkgConfigProbe {
id: pkgConfigZstd
Expand Down
23 changes: 7 additions & 16 deletions src/libtiled/mapreader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,14 @@ class MapReaderPrivate
void readTileLayerData(TileLayer &tileLayer);
void readTileLayerRect(TileLayer &tileLayer,
Map::LayerDataFormat layerDataFormat,
QStringRef encoding,
QStringView encoding,
QRect bounds);
void decodeBinaryLayerData(TileLayer &tileLayer,
const QByteArray &data,
Map::LayerDataFormat format,
QRect bounds);
void decodeCSVLayerData(TileLayer &tileLayer,
QStringRef text,
QStringView text,
QRect bounds);

/**
Expand Down Expand Up @@ -511,9 +511,9 @@ void MapReaderPrivate::readTilesetTile(Tileset &tileset)

// Read tile quadrant terrain ids as Wang IDs. This is possible because the
// terrain types (loaded as WangSet) are always stored before the tiles.
const QStringRef terrain = atts.value(QLatin1String("terrain"));
const auto terrain = atts.value(QLatin1String("terrain"));
if (!terrain.isEmpty() && tileset.wangSetCount() > 0) {
QVector<QStringRef> quadrants = terrain.split(QLatin1Char(','));
const auto quadrants = terrain.split(QLatin1Char(','));
WangId wangId;
if (quadrants.size() == 4) {
for (int i = 0; i < 4; ++i) {
Expand Down Expand Up @@ -777,7 +777,7 @@ void MapReaderPrivate::readTilesetWangSets(Tileset &tileset)
} else if (xml.name() == QLatin1String("wangtile")) {
const QXmlStreamAttributes tileAtts = xml.attributes();
const int tileId = tileAtts.value(QLatin1String("tileid")).toInt();
const QStringRef wangIdString = tileAtts.value(QLatin1String("wangid"));
const auto wangIdString = tileAtts.value(QLatin1String("wangid"));

bool ok = true;
WangId wangId;
Expand Down Expand Up @@ -965,7 +965,7 @@ void MapReaderPrivate::readTileLayerData(TileLayer &tileLayer)

void MapReaderPrivate::readTileLayerRect(TileLayer &tileLayer,
Map::LayerDataFormat layerDataFormat,
QStringRef encoding,
QStringView encoding,
QRect bounds)
{
Q_ASSERT(xml.isStartElement() && (xml.name() == QLatin1String("data") ||
Expand Down Expand Up @@ -1046,7 +1046,7 @@ void MapReaderPrivate::decodeBinaryLayerData(TileLayer &tileLayer,
}

void MapReaderPrivate::decodeCSVLayerData(TileLayer &tileLayer,
QStringRef text,
QStringView text,
QRect bounds)
{
int currentIndex = 0;
Expand Down Expand Up @@ -1289,21 +1289,12 @@ QPolygonF MapReaderPrivate::readPolygon()
break;
}

#if QT_VERSION >= QT_VERSION_CHECK(6,0,0)
const qreal x = QStringView(point).left(commaPos).toDouble(&ok);
if (!ok)
break;
const qreal y = QStringView(point).mid(commaPos + 1).toDouble(&ok);
if (!ok)
break;
#else
const qreal x = point.leftRef(commaPos).toDouble(&ok);
if (!ok)
break;
const qreal y = point.midRef(commaPos + 1).toDouble(&ok);
if (!ok)
break;
#endif

polygon.append(QPointF(x, y));
}
Expand Down
7 changes: 1 addition & 6 deletions src/libtiled/propertytype.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,12 +171,7 @@ QVariant EnumPropertyType::toPropertyValue(const QVariant &value, const ExportCo
if (valuesAsFlags) {
int flags = 0;

#if QT_VERSION < QT_VERSION_CHECK(6,0,0)
const QVector<QStringRef> stringValues = stringValue.splitRef(QLatin1Char(','), Qt::SkipEmptyParts);
#else
const QList<QStringView> stringValues = QStringView(stringValue).split(QLatin1Char(','), Qt::SkipEmptyParts);
#endif

const auto stringValues = QStringView(stringValue).split(QLatin1Char(','), Qt::SkipEmptyParts);
for (const auto &stringValue : stringValues) {
const int index = indexOf(values, stringValue);

Expand Down
26 changes: 0 additions & 26 deletions src/libtiled/qtcompat_p.h

This file was deleted.

2 changes: 1 addition & 1 deletion src/libtiled/wangset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ unsigned WangId::toUint() const
return id;
}

WangId WangId::fromString(QStringRef string, bool *ok)
WangId WangId::fromString(QStringView string, bool *ok)
{
WangId id;

Expand Down
4 changes: 1 addition & 3 deletions src/libtiled/wangset.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@
#include <QString>
#include <QList>

#include "qtcompat_p.h"

namespace Tiled {

class TILEDSHARED_EXPORT WangId
Expand Down Expand Up @@ -134,7 +132,7 @@ class TILEDSHARED_EXPORT WangId
static WangId fromUint(unsigned id);
unsigned toUint() const;

static WangId fromString(QStringRef string, bool *ok = nullptr);
static WangId fromString(QStringView string, bool *ok = nullptr);
QString toString() const;

private:
Expand Down
11 changes: 0 additions & 11 deletions src/libtiled/world.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,8 @@ QRect World::mapRect(const QString &fileName) const
for (const WorldPattern &pattern : patterns) {
QRegularExpressionMatch match = pattern.regexp.match(fileName);
if (match.hasMatch()) {
#if QT_VERSION >= QT_VERSION_CHECK(6,0,0)
const int x = match.capturedView(1).toInt();
const int y = match.capturedView(2).toInt();
#else
const int x = match.capturedRef(1).toInt();
const int y = match.capturedRef(2).toInt();
#endif

return QRect(QPoint(x * pattern.multiplierX,
y * pattern.multiplierY) + pattern.offset,
pattern.mapSize);
Expand All @@ -132,13 +126,8 @@ QVector<WorldMapEntry> World::allMaps() const
for (const QString &fileName : entries) {
QRegularExpressionMatch match = pattern.regexp.match(fileName);
if (match.hasMatch()) {
#if QT_VERSION >= QT_VERSION_CHECK(6,0,0)
const int x = match.capturedView(1).toInt();
const int y = match.capturedView(2).toInt();
#else
const int x = match.capturedRef(1).toInt();
const int y = match.capturedRef(2).toInt();
#endif

WorldMapEntry entry;
entry.fileName = dir.filePath(fileName);
Expand Down
7 changes: 0 additions & 7 deletions src/plugins/flare/flareplugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@
#include <QDir>
#include <QFileInfo>
#include <QStringList>
#if QT_VERSION >= QT_VERSION_CHECK(6,0,0)
#include <QStringView>
#endif
#include <QTextStream>

#include <memory>
Expand Down Expand Up @@ -88,12 +86,7 @@ std::unique_ptr<Tiled::Map> FlarePlugin::read(const QString &fileName)

while (!stream.atEnd()) {
line = stream.readLine();
#if QT_VERSION >= QT_VERSION_CHECK(6,0,0)
const QStringView lineView(line);
#else
const QStringRef lineView(&line);
#endif

if (!line.length())
continue;

Expand Down
2 changes: 0 additions & 2 deletions src/plugins/rpmap/rpmapplugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@
#include <QDir>
#include <QFileInfo>
#include <QStringList>
#if QT_VERSION >= QT_VERSION_CHECK(6,0,0)
#include <QStringView>
#endif
#include <QTextStream>
#include <QXmlStreamWriter>
#include <QUuid>
Expand Down
6 changes: 0 additions & 6 deletions src/plugins/tbin/tbinplugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@

#include <QCoreApplication>
#include <QDir>
#if QT_VERSION >= QT_VERSION_CHECK(6,0,0)
#include <QStringView>
#endif

#include <cmath>
#include <fstream>
Expand Down Expand Up @@ -183,11 +181,7 @@ std::unique_ptr<Tiled::Map> TbinMapFormat::read(const QString &fileName)
continue;

const QString name = QString::fromStdString(prop.first);
#if QT_VERSION >= QT_VERSION_CHECK(6,0,0)
const auto strs = QStringView(name).split(QLatin1Char('@'));
#else
const auto strs = name.splitRef('@');
#endif
if (strs[1] == QLatin1String("TileIndex")) {
int index = strs[2].toInt();
tbin::Properties dummyProps;
Expand Down
8 changes: 0 additions & 8 deletions src/plugins/tengine/tengineplugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,11 @@
#include <QCoreApplication>
#include <QHash>
#include <QList>
#if QT_VERSION >= QT_VERSION_CHECK(6,0,0)
#include <QStringView>
#endif
#include <QTextStream>

#include <QtMath>

#include "qtcompat_p.h"

using namespace Tengine;

TenginePlugin::TenginePlugin()
Expand All @@ -61,11 +57,7 @@ bool TenginePlugin::write(const Tiled::Map *map, const QString &fileName, Option

// Write the header
const QString header = map->property("header").toString();
#if QT_VERSION >= QT_VERSION_CHECK(6,0,0)
const auto lines = QStringView(header).split(QStringLiteral("\\n"));
#else
const auto lines = header.splitRef("\\n");
#endif
for (const auto &line : lines)
out << line << Qt::endl;

Expand Down
16 changes: 1 addition & 15 deletions src/tiled/actionsearch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,22 +91,13 @@ void ActionMatchDelegate::paint(QPainter *painter,
painter->save();

const QString name = index.data().toString();

#if QT_VERSION < QT_VERSION_CHECK(6,0,0)
const auto ranges = Utils::matchingRanges(mWords, &name);
#else
const auto ranges = Utils::matchingRanges(mWords, name);
#endif

QString nameHtml;
int nameIndex = 0;

auto nameRange = [&] (int first, int last) -> QStringRef {
#if QT_VERSION >= QT_VERSION_CHECK(6,0,0)
auto nameRange = [&] (int first, int last) -> QStringView {
return QStringView(name).mid(first, last - first + 1);
#else
return name.midRef(first, last - first + 1);
#endif
};

for (const auto &range : ranges) {
Expand Down Expand Up @@ -261,12 +252,7 @@ QVector<ActionLocatorSource::Match> ActionLocatorSource::findActions(const QStri
QString sanitizedText = action->text();
sanitizedText.replace(re, QString());

#if QT_VERSION >= QT_VERSION_CHECK(6,0,0)
const int totalScore = Utils::matchingScore(words, sanitizedText);
#else
const int totalScore = Utils::matchingScore(words, &sanitizedText);
#endif

if (totalScore > 0) {
result.append(Match {
totalScore,
Expand Down
3 changes: 1 addition & 2 deletions src/tiled/automappingmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,11 +242,10 @@ bool AutomappingManager::loadRulesFile(const QString &filePath)
if (trimmedLine.startsWith(QLatin1Char('[')) && trimmedLine.endsWith(QLatin1Char(']'))) {
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
auto filter = trimmedLine.mid(1, trimmedLine.length() - 2);
mMapNameFilter.setPattern(QRegularExpression::wildcardToRegularExpression(filter.toString()));
#else
auto filter = trimmedLine.sliced(1, trimmedLine.length() - 2);
mMapNameFilter.setPattern(QRegularExpression::wildcardToRegularExpression(filter));
#endif
mMapNameFilter.setPattern(QRegularExpression::wildcardToRegularExpression(filter));
continue;
}

Expand Down
2 changes: 1 addition & 1 deletion src/tiled/libtilededitor.qbs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ DynamicLibrary {
Depends { name: "libtiled" }
Depends { name: "translations" }
Depends { name: "qtsingleapplication" }
Depends { name: "Qt"; submodules: ["core", "widgets", "concurrent", "qml", "svg"]; versionAtLeast: "5.15" }
Depends { name: "Qt"; submodules: ["core", "widgets", "concurrent", "qml", "svg"]; versionAtLeast: "5.15.2" }
Depends { name: "Qt.openglwidgets"; condition: Qt.core.versionMajor >= 6; required: false }
Depends { name: "Qt.dbus"; condition: qbs.targetOS.contains("linux") && project.dbus; required: false }
Depends { name: "Qt.gui-private"; condition: qbs.targetOS.contains("windows") && Qt.core.versionMajor >= 6 }
Expand Down
4 changes: 0 additions & 4 deletions src/tiled/locatorwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,7 @@ void FileMatchDelegate::paint(QPainter *painter,

QString filePath = index.data().toString();
const int lastSlash = filePath.lastIndexOf(QLatin1Char('/'));
#if QT_VERSION < QT_VERSION_CHECK(6,0,0)
const auto ranges = Utils::matchingRanges(mWords, &filePath);
#else
const auto ranges = Utils::matchingRanges(mWords, filePath);
#endif

filePath = QDir::toNativeSeparators(filePath);

Expand Down
4 changes: 0 additions & 4 deletions src/tiled/projectmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,7 @@ static void findFiles(const FolderEntry &entry, int offset, const QStringList &w
{
for (const auto &childEntry : entry.entries) {
if (childEntry->entries.empty()) {
#if QT_VERSION >= QT_VERSION_CHECK(6,0,0)
const auto relativePath = QStringView(childEntry->filePath).mid(offset);
#else
const auto relativePath = childEntry->filePath.midRef(offset);
#endif
const int totalScore = Utils::matchingScore(words, relativePath);

if (totalScore > 0) {
Expand Down
4 changes: 0 additions & 4 deletions src/tiled/scriptmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -304,11 +304,7 @@ bool ScriptManager::checkError(QJSValue value, const QString &program)
QString errorString = value.toString();
QString stack = value.property(QStringLiteral("stack")).toString();

#if QT_VERSION >= QT_VERSION_CHECK(6,0,0)
const auto stackEntries = QStringView(stack).split(QLatin1Char('\n'));
#else
const auto stackEntries = stack.splitRef(QLatin1Char('\n'));
#endif
if (stackEntries.size() > 0 && !stackEntries.first().startsWith(QLatin1String("%entry@"))) {
// Add stack if there were more than one entries
errorString.append(QLatin1Char('\n'));
Expand Down
5 changes: 1 addition & 4 deletions src/tiled/tiledproxystyle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -811,11 +811,8 @@ void TiledProxyStyle::drawControl(ControlElement element,

QRect textRect(xpos, y + windowsItemVMargin, w - xm - windowsRightBorder - tab + 1, h - 2 * windowsItemVMargin);
QRect vTextRect = visualRect(opt->direction, menuitem->rect, textRect);
#if QT_VERSION >= QT_VERSION_CHECK(6,0,0)
QStringView s(menuitem->text);
#else
QStringRef s(&menuitem->text);
#endif

if (!s.isEmpty()) { // draw text
p->save();
int t = s.indexOf(QLatin1Char('\t'));
Expand Down
Loading

5 comments on commit 776d3b6

@svenstaro
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bjorn Hey, could I get a release with this fix in it? It's currently blocking the package update in Arch and I don't want to backport this if I can help it.

@bjorn
Copy link
Member Author

@bjorn bjorn commented on 776d3b6 Feb 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@svenstaro Hmm, yeah this compilation breakage is rather unfortunate. This fix is too involved to backport, with the biggest issue being that it raises the minimum Qt version to 5.15.2 whereas 1.11.2 only required 5.12. So I think applying this patch (and 76e4f61, which it would depend on) until Tiled 1.12 is out would be the easiest way for now, unless we figure out a smaller change to get 1.11.2 to compile.

(I can try the backport soon, so you would at least have clean patches to apply)

@svenstaro
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The patches don't apply cleanly and I don't have time to work this out so unless you can produce a patch or a tiled 1.11.3 that fixes this, we might have to leave users at an old version of tiled in Arch for the time being.

@bjorn
Copy link
Member Author

@bjorn bjorn commented on 776d3b6 Feb 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@svenstaro I found a much smaller patch to make this work in 60388da00, please use that for now. :-)

@svenstaro
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool! That works.

Please sign in to comment.