
Originally Posted by
cwomeijer
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?

Originally Posted by
gfunk
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.
#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, 4096, _O_BINARY) == -1)
printf("failed!");
//int tr = fcntl(fdguistd, O_NONBLOCK);
// 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 n_out;
char *buffer = new char [4096];
//char buffer[512];
printf("\n");
fflush(stdout);
n_out = _read(fdguistd[0], buffer, 4096);
if(n_out <= 0)
return;
if(n_out > 1) {
int con = str.lastIndexOf('\n');
int remv = str.at(con-1) == '/n' ? 1 : 0;
if(con) {
str = str.remove(con-remv, str.length());
output->append(str);
}
}
}
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, 4096, _O_BINARY) == -1)
printf("failed!");
//int tr = fcntl(fdguistd, O_NONBLOCK);
// 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 n_out;
char *buffer = new char [4096];
QString str;
//char buffer[512];
printf("\n");
fflush(stdout);
n_out = _read(fdguistd[0], buffer, 4096);
if(n_out <= 0)
return;
if(n_out > 1) {
str.append(QString(buffer));
int con = str.lastIndexOf('\n');
int remv = str.at(con-1) == '/n' ? 1 : 0;
if(con) {
str = str.remove(con-remv, str.length());
output->append(str);
}
}
}
private:
QTextEdit *output;
int fdStdOut;
int fdguistd[2];
};
#endif
To copy to clipboard, switch view to plain text mode
Bookmarks