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. printf for qstring
Forum Updated to NodeBB v4.3 + New Features

printf for qstring

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 5 Posters 15.3k Views 2 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.
  • S Offline
    S Offline
    s002wjh
    wrote on last edited by
    #1

    what is the easiest way to convert qstring to use in printf? for example

    qstring tmp = "test"

    printf(" %s \n", test);

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      See qPrintable() - but why?

      S 1 Reply Last reply
      5
      • Chris KawaC Online
        Chris KawaC Online
        Chris Kawa
        Lifetime Qt Champion
        wrote on last edited by
        #3

        On Windows you can also use printf("%ls\n", qUtf16Printable(tmp)) which has the nice benefit of (usually) avoiding conversion. Note it's not applicable to any platform where wchar_t is not 2 bytes.

        aha_1980A 1 Reply Last reply
        2
        • Kent-DorfmanK Offline
          Kent-DorfmanK Offline
          Kent-Dorfman
          wrote on last edited by
          #4

          generally not the best way to handle qstrings but in direct answer to your question:

          QString mystring("some string");
          printf("%s\n", mystring.toStdString().c_str());
          

          There may be a Qstring direct method to get the const char* address of the stirng data but the method I show is pretty solid.

          IMHO, you really shouldn't be using c style printf in C++ code. Learn to use the streams I/O.

          Chris KawaC 1 Reply Last reply
          1
          • Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #5
            This post is deleted!
            1 Reply Last reply
            0
            • Kent-DorfmanK Kent-Dorfman

              generally not the best way to handle qstrings but in direct answer to your question:

              QString mystring("some string");
              printf("%s\n", mystring.toStdString().c_str());
              

              There may be a Qstring direct method to get the const char* address of the stirng data but the method I show is pretty solid.

              IMHO, you really shouldn't be using c style printf in C++ code. Learn to use the streams I/O.

              Chris KawaC Online
              Chris KawaC Online
              Chris Kawa
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @Kent-Dorfman said:

              generally not the best way to handle qstrings

              Agreed, especially that it requires two libraries to do a simple task. You can stay in Qt world with

              printf("%s\n", mystring.toUtf8().data());
              

              but that's essentially what qPrintable() does so what @Christian-Ehrlicher said.

              1 Reply Last reply
              2
              • Christian EhrlicherC Christian Ehrlicher

                See qPrintable() - but why?

                S Offline
                S Offline
                s002wjh
                wrote on last edited by s002wjh
                #7

                @Christian-Ehrlicher said in printf for qstring:

                See qPrintable() - but why?

                oh, QT was compiled as lib on my machine so I wasn't able to use qDebug or any qWarning etc.

                also I have a custom function that printout the file name, and line numbers using printf for debugging. unless there is some c++ or Qt way to print out the file name and line numbers,?

                1 Reply Last reply
                0
                • Christian EhrlicherC Offline
                  Christian EhrlicherC Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by Christian Ehrlicher
                  #8

                  @s002wjh said in printf for qstring:

                  so I wasn't able to use qDebug or any qWarning etc.

                  See qInstallMessageHandler() and/or QLoggingCategory or use a proper logging library (printf is no function to print out debug / logging stuff) like e.g. Log4Qt

                  1 Reply Last reply
                  0
                  • Chris KawaC Chris Kawa

                    On Windows you can also use printf("%ls\n", qUtf16Printable(tmp)) which has the nice benefit of (usually) avoiding conversion. Note it's not applicable to any platform where wchar_t is not 2 bytes.

                    aha_1980A Offline
                    aha_1980A Offline
                    aha_1980
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @Chris-Kawa

                    Note it's not applicable to any platform where wchar_t is not 2 bytes.

                    I'm not sure this limitation exists.

                    qUtf16Printable is used in Qt itself on Linux and Windows at least. Which platforms did you have in mind?

                    Regards

                    Qt has to stay free or it will die.

                    Chris KawaC 1 Reply Last reply
                    0
                    • aha_1980A aha_1980

                      @Chris-Kawa

                      Note it's not applicable to any platform where wchar_t is not 2 bytes.

                      I'm not sure this limitation exists.

                      qUtf16Printable is used in Qt itself on Linux and Windows at least. Which platforms did you have in mind?

                      Regards

                      Chris KawaC Online
                      Chris KawaC Online
                      Chris Kawa
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      @aha_1980 said in printf for qstring:

                      I'm not sure this limitation exists.

                      Isn't the size of wchar_t on most Linux distros and mac 4 bytes? %ls takes wchar_t* and as far as I can tell all qUtf16Printable does is some forceful casting to silence compiler warnings. Still, it uses the underlying QString data directly. QString uses 2 byte chars, so I don't see how this would work?
                      Unless there's more funky trickery going on on Linux, I don't know. I mostly keep to Windows so if you know how that works I'd be interested to learn.

                      aha_1980A 1 Reply Last reply
                      0
                      • Chris KawaC Chris Kawa

                        @aha_1980 said in printf for qstring:

                        I'm not sure this limitation exists.

                        Isn't the size of wchar_t on most Linux distros and mac 4 bytes? %ls takes wchar_t* and as far as I can tell all qUtf16Printable does is some forceful casting to silence compiler warnings. Still, it uses the underlying QString data directly. QString uses 2 byte chars, so I don't see how this would work?
                        Unless there's more funky trickery going on on Linux, I don't know. I mostly keep to Windows so if you know how that works I'd be interested to learn.

                        aha_1980A Offline
                        aha_1980A Offline
                        aha_1980
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        @Chris-Kawa,

                        you are right: https://doc.qt.io/qt-5/qtglobal.html#qUtf16Printable states that the result is no valid wchar_t. so you can use the result with qDebug and QString::asprintf, but not with printf.

                        Regards

                        Qt has to stay free or it will die.

                        1 Reply Last reply
                        1

                        • Login

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