void MainWindow::RedirectOutput(int type) {
if (type==0) {
// redirect stdout to file
freopen("redirect.txt","w",stdout);
// disable buffering
setvbuf(stdout,NULL,_IONBF,0);
// check if file changed
watcher->addPath("redirect.txt");
connect(watcher,
SIGNAL(fileChanged
(QString)),
this,
SLOT(HandleFileChange
(QString)));
} else {
// reenable buffering, not really needed
//setvbuf(stdout,NULL,_IOFBF,4000);
//return stdout to display
freopen( "CON","w",stdout);
disconnect
(watcher,
SIGNAL(fileChanged
(QString)),
this,
SLOT(HandleFileChange
(QString)));
//<doesnt' work if (QFile::exists("redirect.txt")) QFile::remove("redirect.txt");
}
}
void MainWindow
::HandleFileChange(const QString filename
){ //QMessageBox::information(this,"info",QString::number(f.size()));
f.seek(fbytes);
fbytes += ba.size();
//QMessageBox::information(this,"stdout",filename + " changed:\n\n" + ba);
//cout << qPrintable(ba) << endl;
ui->textEdit->append(qPrintable(ba));
} else {
QMessageBox::critical(this,
"stdout",
"Can't open file");
}
}
void MainWindow::RedirectOutput(int type) {
QFileSystemWatcher * watcher = new QFileSystemWatcher(this);
if (type==0) {
// redirect stdout to file
freopen("redirect.txt","w",stdout);
// disable buffering
setvbuf(stdout,NULL,_IONBF,0);
// check if file changed
watcher->addPath("redirect.txt");
connect(watcher,SIGNAL(fileChanged(QString)),this,SLOT(HandleFileChange(QString)));
} else {
// reenable buffering, not really needed
//setvbuf(stdout,NULL,_IOFBF,4000);
//return stdout to display
freopen( "CON","w",stdout);
disconnect(watcher,SIGNAL(fileChanged(QString)),this,SLOT(HandleFileChange(QString))); //<doesnt' work
if (QFile::exists("redirect.txt"))
QFile::remove("redirect.txt");
}
}
void MainWindow::HandleFileChange(const QString filename){
QFile f(filename);
if (f.open(QIODevice::ReadOnly)) {
//QMessageBox::information(this,"info",QString::number(f.size()));
f.seek(fbytes);
const QByteArray ba = f.readAll();
fbytes += ba.size();
//QMessageBox::information(this,"stdout",filename + " changed:\n\n" + ba);
//cout << qPrintable(ba) << endl;
ui->textEdit->append(qPrintable(ba));
} else {
QMessageBox::critical(this,"stdout","Can't open file");
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks