Results 1 to 10 of 10

Thread: Redirect printf to QTextEdit?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Sep 2006
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Redirect printf to QTextEdit?

    I seem to have a similar request for my project.
    In my huge C code base we printed:
    Informational messages,
    Warning messages,
    Error messages.

    All of the obviously doen with printf().
    So we created our own version that sended out signals
    with the corresponding ascii line as an argument.
    I was lucky that we had our own routines for printing
    but I guess some nifty #define can do the trick/ or a big grep.

    Anyway the signal solution is realy neat. It's thread safe and does
    not add a lot of overhead. I tested it with several million of messages
    and there was not a big impact on the performance.

    You do need to take care of the C -> C++ wrappers though.
    Otto Meijer
    email: otto.meijer@synopsys.com

  2. #2
    Join Date
    Aug 2006
    Posts
    20
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Redirect printf to QTextEdit?

    Quote Originally Posted by cwomeijer View Post
    So we created our own version that sended out signals
    with the corresponding ascii line as an argument.
    But then you had to implement a signal in every class?
    Or how did you do it?
    Quote Originally Posted by gfunk View Post
    Perhaps there is a non-blocking version of _read() that you can call?
    Exactly, been looking for that or the possibility to set fcntl O_NONBLOCK but I cant find how to that in windows. For now problem was solved by adding a \n before I do fflush.

    Now I think it's working with some cheating.

    Qt Code:
    1. #ifndef STDOUTREDIRECTOR_H
    2. #define STDOUTREDIRECTOR_H
    3. #include <QString>
    4. #include <iostream>
    5. #include <stdio.h>
    6. #include <fcntl.h>
    7. #include <io.h>
    8. #include <QTextEdit>
    9.  
    10. class StdOutRedirector : public QObject
    11. {
    12. Q_OBJECT
    13. public:
    14. StdOutRedirector()
    15. {
    16. // Redirect
    17. if(_pipe(fdguistd, 4096, _O_BINARY) == -1)
    18. printf("failed!");
    19. //int tr = fcntl(fdguistd, O_NONBLOCK);
    20. // Duplicate stdout file descriptor (next line will close original)
    21. fdStdOut = _dup(_fileno(stdout));
    22. // Duplicate write end of pipe to stdout file descriptor
    23. if(_dup2(fdguistd[1], _fileno(stdout)) != 0)
    24. printf("failed!");
    25. // Close original
    26. close(1);
    27. // Duplicate write end of original
    28. dup2(fdguistd[1], 1);
    29.  
    30. }
    31. void setOutputTF(QTextEdit *_output)
    32. {
    33. output = _output;
    34. }
    35. public slots:
    36. void readOutsToTF()
    37. {
    38. int n_out;
    39. char *buffer = new char [4096];
    40. QString str;
    41. //char buffer[512];
    42. printf("\n");
    43. fflush(stdout);
    44.  
    45. n_out = _read(fdguistd[0], buffer, 4096);
    46.  
    47. if(n_out <= 0)
    48. return;
    49. if(n_out > 1) {
    50. str.append(QString(buffer));
    51. int con = str.lastIndexOf('\n');
    52. int remv = str.at(con-1) == '/n' ? 1 : 0;
    53. if(con) {
    54. str = str.remove(con-remv, str.length());
    55. output->append(str);
    56. }
    57. }
    58.  
    59. }
    60. private:
    61. QTextEdit *output;
    62. int fdStdOut;
    63. int fdguistd[2];
    64. };
    65. #endif
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. a question about visualizing cursor in read-only QTextEdit
    By kennyxing in forum Qt Programming
    Replies: 3
    Last Post: 17th September 2006, 19:52
  2. QTextEdit API questions (plain text)
    By Gaspar in forum Qt Programming
    Replies: 4
    Last Post: 16th May 2006, 06:03
  3. [QT 4] QTextEdit performance
    By fellobo in forum Qt Programming
    Replies: 8
    Last Post: 6th March 2006, 19:27
  4. Painting to QTextEdit
    By gesslar in forum Qt Programming
    Replies: 8
    Last Post: 18th February 2006, 18:40
  5. Obtaining clean (x)html from QTextEdit
    By ccf_h in forum Qt Programming
    Replies: 1
    Last Post: 5th February 2006, 14:47

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.