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
Forum Update on Monday, May 27th 2025

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 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
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on 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
      0
      • SGaistS SGaist

        Hi,

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

        Hope it helps

        ? Offline
        ? Offline
        A Former User
        wrote on last edited by A Former User
        #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
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on 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

          • Login

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