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

Odd format in Qurl c'tor

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 5 Posters 1.0k Views
  • 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.
  • M Offline
    M Offline
    mzimmers
    wrote on 3 Oct 2022, 18:47 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
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 3 Oct 2022, 19:14 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
      • J Offline
        J Offline
        JoeCFD
        wrote on 3 Oct 2022, 19:15 last edited by JoeCFD 10 Mar 2022, 19:20
        #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
        • M Offline
          M Offline
          mzimmers
          wrote on 3 Oct 2022, 21:39 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...

          J 1 Reply Last reply 3 Oct 2022, 21:56
          0
          • M mzimmers
            3 Oct 2022, 21:39

            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...

            J Offline
            J Offline
            JonB
            wrote on 3 Oct 2022, 21:56 last edited by JonB 10 Mar 2022, 21:58
            #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.

            M 1 Reply Last reply 3 Oct 2022, 22:37
            3
            • C Online
              C Online
              Chris Kawa
              Lifetime Qt Champion
              wrote on 3 Oct 2022, 22:09 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.

              M 1 Reply Last reply 3 Oct 2022, 23:03
              1
              • J JonB
                3 Oct 2022, 21:56

                @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.

                M Offline
                M Offline
                mzimmers
                wrote on 3 Oct 2022, 22:37 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?

                C 1 Reply Last reply 3 Oct 2022, 23:11
                0
                • C Chris Kawa
                  3 Oct 2022, 22:09

                  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.

                  M Offline
                  M Offline
                  mzimmers
                  wrote on 3 Oct 2022, 23:03 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
                  • M mzimmers
                    3 Oct 2022, 22:37

                    @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?

                    C Online
                    C Online
                    Chris Kawa
                    Lifetime Qt Champion
                    wrote on 3 Oct 2022, 23:11 last edited by Chris Kawa 10 Mar 2022, 23:14
                    #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

                    4/9

                    3 Oct 2022, 21:39

                    • Login

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