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 to stream

Printf to stream

Scheduled Pinned Locked Moved Solved General and Desktop
printfstream
5 Posts 3 Posters 2.1k 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
    MartinD
    wrote on 11 Jan 2016, 21:07 last edited by
    #1

    Hello,
    I have this function:

    void app_trace_log(const char * format, ... ) {
    //qDebug() << ???????????;
    }

    The format argument uses printf like syntax for printing variable number of arguments (...). What's the easiest way to transform it to qt stream output (like qDebug())?

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 11 Jan 2016, 22:25 last edited by
      #2

      Hi,

      You could use QString::asprintf (new in 5.5) or QTextStream

      Hope it helps

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

      ? 2 Replies Last reply 11 Jan 2016, 22:45
      0
      • S SGaist
        11 Jan 2016, 22:25

        Hi,

        You could use QString::asprintf (new in 5.5) or QTextStream

        Hope it helps

        ? Offline
        ? Offline
        A Former User
        wrote on 11 Jan 2016, 22:45 last edited by A Former User 1 Nov 2016, 23:13
        #3

        @SGaist How would you use QTextStream for this? The only solution I know goes like:

        void app_trace_log(const char *format, ...) {
            va_list args;
            va_start(args, format);
            char *result;
            const int n = vasprintf(&result, format, args);
            va_end(args);
            if (n>=0) {
                qDebug() << result;
                free(result);
            }
        }
        
        1 Reply Last reply
        1
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 11 Jan 2016, 23:01 last edited by
          #4

          Sorry, I didn't meant it as drop-in replacement.

          Thanks for the example.

          Note that I'd rather recommend the use of QLoggingCategory for that kind of task.

          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
          0

          2/5

          11 Jan 2016, 22:25

          • Login

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