qtqt6qstring

Changing _qs to _s in Qt6.8


I need to change from operator""_qs to operator""_s, thanks to Qt 6.8. The following code compiles

// notice no includes or using namespace of any kind
auto const label = u"Foo"_qs;

So I tried the following update

#include <StringLiterals>
using namespace Qt::StringLiterals;
auto const label = u"Foo"_s;

but error C3688: invalid literal suffix '_s'; literal operator or literal operator template 'operator ""_s' not found

My first question is, how does it find operator""_qs without any include or namespace action?
And the second is obviously, how do I fix the error? I have the suspicion that cmake isn't loading the newest Qt version although I have installed only Qt6.6.


Solution

  • ChatGPT solved it:

    In Qt 6.6 and newer, the _qs string literal (i.e., operator""_qs) is a built-in part of the QtCore module. It was introduced specifically to allow more seamless use of QString in a modern C++ way. Here's why it "just works":

    So, even without includes or using directives, _qs can work depending on the context of the full translation unit.


    No, you cannot use _s as a QString literal in Qt 6.6 — it's a feature introduced in Qt 6.8. In Qt 6.6:

    So there's no way to make _s work as a QString literal in Qt 6.6, unless you backport the operator yourself (not recommended in production code).