Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-125064

Wanted: a class QStringDataLiteral, for static strings needed as QString type

    XMLWordPrintable

Details

    • Suggestion
    • Resolution: Unresolved
    • Not Evaluated
    • None
    • None
    • None

    Description

      If one has the need for static strings which then are passed to API which requires QString type arguments, it would be good to have the option to define those static strings using a helper type from which a QString instance can be created on-demand, using the very static string literal without doing a deep copy. Like QStringLiteral, but with the actual string defined elsewhere.

      Use case A: single-place id define, to have compiler catch typos:

      static const StringDataLiteralType configEntryId = u"MyEntry";

      void Foo::readConfig(const Config *config) {

          m_foo = config.read(configEntryId); // read() takes QString

      }

      void Foo::writeConfig(Config *config) const {

          config.write(configEntryId, {}m_foo{}); // write() takes QString

      }

      Use case B: table with prepared strings

      static const std::array<StringDataLiteralType, 2> names = {

          u"Bar",

          u"Foo",

      };

      QString name(int i) {

          return (0 <= i && i < names.size()) ? names[i] : QString();

      }

      See as Qt code example QWindowsIconEngine::glyphs() which uses QStringView as type for the static strings, but then has to call it->second.toString() when having to deliver the final QString type.

      KDE has come up with some util struct struct RawStringData which can be used for static string literals, from which then QString instances are created on demand (ab)using Qt::Literals::StringLiterals::operator""_s(). See https://invent.kde.org/frameworks/kconfigwidgets/-/merge_requests/220/diffs for the MR introducing it, which also has some further discussion. The class/struct has meanwhile been copied already in other places, to serve the same purpose.

      Perhaps something similar to RawStringData can be added to Qt directly? By a name QStringDataLiteral or similar?

      While that struct currently requires explicit conversion to a QString, using toString(), perhaps implicit conversion to QString might make using the class more convenient (e.g. by an implicit QString(..) constructor). Not yet pondered in details.

      Attachments

        Issue Links

          No reviews matched the request. Check your Options in the drop-down menu of this sections header.

          Activity

            People

              thiago Thiago Macieira
              kossebau Friedrich W. H. Kossebau
              Votes:
              0 Vote for this issue
              Watchers:
              5 Start watching this issue

              Dates

                Created:
                Updated:

                Gerrit Reviews

                  There are no open Gerrit changes