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
Hello World.txt
0
1
2
3
4
To copy to clipboard, switch view to plain text mode
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;
...
}
}
}
}
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;
...
}
}
}
}
To copy to clipboard, switch view to plain text mode
In Qt for now I have something like this
void PC::on_pushButtonStream_clicked()
{
if(file.isOpen()){
// while(true) // << CRASH
{
while (!file.atEnd()) {
ui->textBrowserLog->setText(line);
}
// }
}
}
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);
}
// }
}
}
To copy to clipboard, switch view to plain text mode
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())
while(file.isOpen())
To copy to clipboard, switch view to plain text mode
but it crashes.
Thank you in advance.
Bookmarks