PDA

View Full Version : Constantly Update QFile read



digimonkey
4th November 2014, 08:16
Hi,
Im pretty new at Qt and english isnt my main language but I will try to explain my doubt.

Im creating a program that reads a text file and print its last line in a textBox after you press a button (Stream). The thing is I want the textBox to update automatically after a new line is add in the text file, while I keep read the text file and dont close it.

For example, this is my text file at first time I read it:



Hello World.txt

0
1
2
3
4


There should be printed ‘4’ in the textBox. (until here all OK)

But after the first read I add a new line in the text file with the number ‘5’, then the program should update automatically the textBox and add that new line.

I’ve made the same program a few months ago in C++ but now I want to put it in Qt


while (1)
{
if (logfile.is_open())
{
logfile.seekg(0, logfile.end);
string line;
while (true)
{
while (getline(logfile, line))
{
strncpy_s(send_data, line.c_str(), sizeof(send_data));
send_data[sizeof(send_data)-1] = 0;
...
}
}
}
}

In Qt for now I have something like this


void PC::on_pushButtonStream_clicked()
{
QFile file(filename);
if(!file.open(QIODevice::ReadOnly))
QMessageBox::information(0,"info",file.errorString());
if(file.isOpen()){
// while(true) // << CRASH
{
while (!file.atEnd()) {
QByteArray line = file.readLine();
ui->textBrowserLog->setText(line);
}
// }
}
}

For now I have to click all time the Stream button to update the textBox

I’ve also tried to do a


while(file.isOpen())

but it crashes.

Thank you in advance.

myta212
4th November 2014, 08:36
Hi,
Are you mean created a program to watch your file and update the Qt Program when that file modified ?
If yes, I think you can use " QFileSystemWatcher "
Hope this class will solve your problem.

Best regards,

myta

digimonkey
4th November 2014, 08:43
Hi,
Are you mean created a program to watch your file and update the Qt Program when that file modified ?
If yes, I think you can use " QFileSystemWatcher "
Hope this class will solve your problem.

Best regards,

myta

Thank you will check it.

EDIT
It seems that QFileSystemWatcher is used to monotorize files on folders (directories). What I want is monotorize if there are changes inside the file, in this case a text file.

Lesiok
4th November 2014, 09:18
From QFileSystemWatcher doc : The fileChanged() signal is emitted when a file has been modified, renamed or removed from disk