// widget.h
include <QPushButton>
#include <QFileSystemWatcher>
#include <QMessageBox>
#include <cstdio>
Q_OBJECT
public:
explicit Widget
(const QString
& fileName,
QWidget * parent
= NULL) ,_nBytes(0)
{
connect(btn, SIGNAL(clicked()), this, SLOT(onClick()));
watcher->addPath(fileName);
connect(watcher,
SIGNAL(fileChanged
(QString)),
this,
SLOT(fileChanged
(QString)));
}
private slots:
void onClick(){
static int counter = 0;
printf("text (%i)",counter++);
}
void fileChanged(const QString& path){
f.seek(_nBytes);
_nBytes += ba.size();
QMessageBox::information(this,
"stdout",path
+ " changed:\n\n" + ba
);
} else {
}
}
private:
int _nBytes;
};
#endif
// main.cpp
#include <QApplication>
#include <cstdio>
#include "widget.h"
int main(int argc, char ** argv){
stdout = freopen("test.txt","w",stdout);
setvbuf(stdout, NULL, _IONBF, 0);
Widget w("test.txt");
w.show();
return app.exec();
}
// widget.h
include <QPushButton>
#include <QFileSystemWatcher>
#include <QMessageBox>
#include <cstdio>
class Widget : public QWidget{
Q_OBJECT
public:
explicit Widget(const QString& fileName, QWidget * parent = NULL)
:QWidget(parent)
,_nBytes(0)
{
QPushButton * btn = new QPushButton("add text", this);
connect(btn, SIGNAL(clicked()), this, SLOT(onClick()));
QFileSystemWatcher * watcher = new QFileSystemWatcher(this);
watcher->addPath(fileName);
connect(watcher, SIGNAL(fileChanged(QString)), this, SLOT(fileChanged(QString)));
}
private slots:
void onClick(){
static int counter = 0;
printf("text (%i)",counter++);
}
void fileChanged(const QString& path){
QFile f(path);
if( f.open(QIODevice::ReadOnly) ){
f.seek(_nBytes);
const QByteArray ba = f.readAll();
_nBytes += ba.size();
QMessageBox::information(this,"stdout",path + " changed:\n\n" + ba);
} else {
QMessageBox::critical(this,"stdout","can't open!");
}
}
private:
int _nBytes;
};
#endif
// main.cpp
#include <QApplication>
#include <cstdio>
#include "widget.h"
int main(int argc, char ** argv){
stdout = freopen("test.txt","w",stdout);
setvbuf(stdout, NULL, _IONBF, 0);
QApplication app(argc,argv);
Widget w("test.txt");
w.show();
return app.exec();
}
To copy to clipboard, switch view to plain text mode
Bookmarks