Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Odd format in Qurl c'tor
Forum Updated to NodeBB v4.3 + New Features

Odd format in Qurl c'tor

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 5 Posters 1.1k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • mzimmersM Offline
    mzimmersM Offline
    mzimmers
    wrote on last edited by
    #1

    Hi all -

    I created a Qt Quick project from Creator, and got this in my main.cpp:

        const QUrl url(u"qrc:/xxx/main.qml"_qs);
    

    I don't understand the argument; can someone fill me in?

    Thanks...

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      AFAIK, it is a string literal notation. See the Qt StringLiterals namespace.

      Basically, you create a QString out of a UTF-16 string. See here for the C++ doc about them.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • JoeCFDJ Offline
        JoeCFDJ Offline
        JoeCFD
        wrote on last edited by JoeCFD
        #3

        https://doc.qt.io/qt-6.2/newclasses62.html#new-functions-in-namespaces
        operator""_qs(const char16_t *str, size_t size)
        https://doc.qt.io/qt-6.2/qstring.html#operator-22-22_qs

        1 Reply Last reply
        1
        • mzimmersM Offline
          mzimmersM Offline
          mzimmers
          wrote on last edited by
          #4

          Thanks, guys. So, the "u" preceding the string creates a string literal (16-bit flavor), and the "_qs" after the string converts it to a QString. (According to the docs that JoeCFD referenced, _qs needs a 16-bit string literal.)

          So...if _qs is a function, why doesn't it look like one in the line I posted above? Is this more new C++ stuff? I'm starting to feel antediluvian...

          JonBJ 1 Reply Last reply
          0
          • mzimmersM mzimmers

            Thanks, guys. So, the "u" preceding the string creates a string literal (16-bit flavor), and the "_qs" after the string converts it to a QString. (According to the docs that JoeCFD referenced, _qs needs a 16-bit string literal.)

            So...if _qs is a function, why doesn't it look like one in the line I posted above? Is this more new C++ stuff? I'm starting to feel antediluvian...

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #5

            @mzimmers said in Odd format in Qurl c'tor:

            So...if _qs is a function,

            It isn't, it's an operator :) QString operator""_qs(const char16_t *str, size_t size). So, doubtless by some piece of C++2000 magic, ""_qs does its (compile-time) magic on the string in between "..." quotes and followed by _qs. Gotta love C++.

            BTW, u"qrc:/xxx/main.qml"_qs equates to

            QStringLiteral("qrc:/xxx/main.qml")
            

            which is a bit more understandable.

            mzimmersM 1 Reply Last reply
            3
            • Chris KawaC Offline
              Chris KawaC Offline
              Chris Kawa
              Lifetime Qt Champion
              wrote on last edited by
              #6

              u prefix signifies a char16_t string literal.
              _qs suffix is a user defined literal that produces a QString using that literal as the data directly, without runtime allocation.

              It's nothing new. Both of these are C++11, so way over decade old by now.

              mzimmersM 1 Reply Last reply
              1
              • JonBJ JonB

                @mzimmers said in Odd format in Qurl c'tor:

                So...if _qs is a function,

                It isn't, it's an operator :) QString operator""_qs(const char16_t *str, size_t size). So, doubtless by some piece of C++2000 magic, ""_qs does its (compile-time) magic on the string in between "..." quotes and followed by _qs. Gotta love C++.

                BTW, u"qrc:/xxx/main.qml"_qs equates to

                QStringLiteral("qrc:/xxx/main.qml")
                

                which is a bit more understandable.

                mzimmersM Offline
                mzimmersM Offline
                mzimmers
                wrote on last edited by
                #7

                @JonB said in Odd format in Qurl c'tor:

                It isn't, it's an operator :)

                OK, so, to further my education:

                operator""_qs(const char16_t *str, size_t size)
                

                How does one go about providing str and size to this operator - is it somehow implicit from the line of code I posted originally?

                Chris KawaC 1 Reply Last reply
                0
                • Chris KawaC Chris Kawa

                  u prefix signifies a char16_t string literal.
                  _qs suffix is a user defined literal that produces a QString using that literal as the data directly, without runtime allocation.

                  It's nothing new. Both of these are C++11, so way over decade old by now.

                  mzimmersM Offline
                  mzimmersM Offline
                  mzimmers
                  wrote on last edited by
                  #8

                  @Chris-Kawa said in Odd format in Qurl c'tor:

                  It's nothing new. Both of these are C++11, so way over decade old by now.

                  Chris - anything newer than the Reagan administration is new to me. Nice haircut, BTW...

                  1 Reply Last reply
                  1
                  • mzimmersM mzimmers

                    @JonB said in Odd format in Qurl c'tor:

                    It isn't, it's an operator :)

                    OK, so, to further my education:

                    operator""_qs(const char16_t *str, size_t size)
                    

                    How does one go about providing str and size to this operator - is it somehow implicit from the line of code I posted originally?

                    Chris KawaC Offline
                    Chris KawaC Offline
                    Chris Kawa
                    Lifetime Qt Champion
                    wrote on last edited by Chris Kawa
                    #9

                    How does one go about providing str and size to this operator - is it somehow implicit from the line of code I posted originally?

                    u"qrc:/xxx/main.qml" becomes the str argument and size is calculated by the compiler.

                    The size argument is optional too, so if you'd like to create your own suffix, for example one that converts a string literal to an int at compile time you would do something like:

                    int operator"" _mysuffix(const char* str) { return atoi(str); }
                    

                    and then you can use it like this:

                    int foo = "42"_mysuffix;
                    

                    Obviously this is not a very useful example, but the string can come from anywhere, so you could do something like take a preprocessor macro with a version string and convert it to an int at compile time. Or if you have a complex number class you could define MyComplex operator"" _cx(const char*, size_t) and have it parse at compile time to create a variable of class MyComplex like this: "4+3i"_cx;.

                    It's worth pointing out that apart from string literals this operator can also take numbers. For example the std::chrono namespace defines a bunch of them for time units, so you can write something like sleep(250ms) and no longer wonder what unit the parameter is.

                    1 Reply Last reply
                    3

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved