Originally Posted by
high_flyer
I don't see how the Qt qrapper classes in the threads you quoten could help you even if they would redirect printf().
Well they won't, the first one was just an example of an output and the other link was able to redirect std::cout's to an QTextEdit but not printf.
Originally Posted by
high_flyer
You said you can't/don't want to change the code where the printf()'s are, since you are not sure you can find them (which is not true, you can apply a search on all the files in the project).
Yes I can find them but I don't want to change them because then I have to do it everytime the code is updated. But I can't find the ones or change the ones that might be in the libs, and I know there are a few.
Originally Posted by
high_flyer
A posibility to do what you want would be to use QProcess and to read the printf() output from the console in to a QTextEdit or any other place.
Hmm, that might actually be a good idea. I will take a look at that.
This is what I have done so far. It doesent really work yet but I think this is one way to do it. I created a pipe and made a dublicate of stdout. Then I just have to read from it and output it in my TE.
#include <QApplication>
#include <QTimer>
//#include "q_debugstream.h"
#include "stdoutredirector.h"
#include "testwidget.h"
int main(int argc, char **argv)
{
printf("This should be display!\n");
StdOutRedirector *redir = new StdOutRedirector;
//printf("This should go to fdguistd\n");
TestWidget tw;
tw.show();
redir->setOutputTF(tw.qte);
printf("Testing redirection\n");
redir->readOutsToTF();
// Make a QTimer that update the read every 40 ms
//QObject::connect(redirTimer, SIGNAL(timeout()), redir, SLOT(readOutsToTF()));
//redirTimer->start(40);
//Q_DebugStream cout(std::cout, tw.qte);
//Q_DebugStream cerr(std::cerr, tw.qte);
std::cout << "Printing something else" << std::endl;
redir->readOutsToTF();
return app.exec();
}
#include <QApplication>
#include <QTimer>
//#include "q_debugstream.h"
#include "stdoutredirector.h"
#include "testwidget.h"
int main(int argc, char **argv)
{
printf("This should be display!\n");
StdOutRedirector *redir = new StdOutRedirector;
//printf("This should go to fdguistd\n");
QApplication app(argc, argv);
TestWidget tw;
tw.show();
redir->setOutputTF(tw.qte);
printf("Testing redirection\n");
redir->readOutsToTF();
// Make a QTimer that update the read every 40 ms
QTimer *redirTimer = new QTimer;
//QObject::connect(redirTimer, SIGNAL(timeout()), redir, SLOT(readOutsToTF()));
//redirTimer->start(40);
//Q_DebugStream cout(std::cout, tw.qte);
//Q_DebugStream cerr(std::cerr, tw.qte);
std::cout << "Printing something else" << std::endl;
redir->readOutsToTF();
return app.exec();
}
To copy to clipboard, switch view to plain text mode
#ifndef STDOUTREDIRECTOR_H
#define STDOUTREDIRECTOR_H
#include <QString>
#include <iostream>
#include <stdio.h>
#include <fcntl.h>
#include <io.h>
#include <QTextEdit>
class StdOutRedirector
: public QObject{
Q_OBJECT
public:
StdOutRedirector()
{
// Redirect
if(_pipe(fdguistd, 512, _O_BINARY) == -1)
printf("failed!");
// Duplicate stdout file descriptor (next line will close original)
fdStdOut = _dup(_fileno(stdout));
// Duplicate write end of pipe to stdout file descriptor
if(_dup2(fdguistd[1], _fileno(stdout)) != 0)
printf("failed!");
// Close original
close(1);
// Duplicate write end of original
dup2(fdguistd[1], 1);
}
{
output = _output;
}
//public slots:
void readOutsToTF()
{
int nout;
char *buffer = new char [512];
//char buffer[512];
printf(" ");
fflush(stdout);
nout = _read(fdguistd[0], buffer, 512);
if(nout <= 0)
return;
if(nout) {
//output->append(str.setNum(nout));
}
delete buffer;
}
private:
int fdStdOut;
int fdguistd[2];
};
#endif
#ifndef STDOUTREDIRECTOR_H
#define STDOUTREDIRECTOR_H
#include <QString>
#include <iostream>
#include <stdio.h>
#include <fcntl.h>
#include <io.h>
#include <QTextEdit>
class StdOutRedirector : public QObject
{
Q_OBJECT
public:
StdOutRedirector()
{
// Redirect
if(_pipe(fdguistd, 512, _O_BINARY) == -1)
printf("failed!");
// Duplicate stdout file descriptor (next line will close original)
fdStdOut = _dup(_fileno(stdout));
// Duplicate write end of pipe to stdout file descriptor
if(_dup2(fdguistd[1], _fileno(stdout)) != 0)
printf("failed!");
// Close original
close(1);
// Duplicate write end of original
dup2(fdguistd[1], 1);
}
void setOutputTF(QTextEdit *_output)
{
output = _output;
}
//public slots:
void readOutsToTF()
{
int nout;
char *buffer = new char [512];
//char buffer[512];
printf(" ");
fflush(stdout);
nout = _read(fdguistd[0], buffer, 512);
if(nout <= 0)
return;
if(nout) {
QString str;
//output->append(str.setNum(nout));
output->append(QString(buffer));
}
delete buffer;
}
private:
QTextEdit *output;
int fdStdOut;
int fdguistd[2];
};
#endif
To copy to clipboard, switch view to plain text mode
One problem is that when there is nothing in the fdguistd buffer it stops there. Another is that it doesent output the text in TE until it reaches the last line in the main method.
Bookmarks