Reputation: 69
I want to read a file one line at each second using timer. Once the timer is started, read the first line, after one second, read the second line......
But there is no function to read specific line in QTextStream. Any ideas on how to achieve this?
If I run the following code, it will always returns
QTextStream: no device QTextStream: no device QTextStream: no device QTextStream: no device
timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(sendmsg()));
void simulatorwindow::on_simON_clicked()
{
simfile = QFileDialog::getOpenFileName(this, tr("Open"),"", tr("Files (*.txt)"));
QFile simfile(simfile);
if (!simfile.open(QIODevice::ReadOnly | QIODevice::Text))
return;
QTextStream textsim(&simfile);
timer->start(1000);
qDebug("Start simulation");
}
void simulatorwindow::on_simOFF_clicked()
{
timer->stop();
qDebug("Stop simulation");
}
void simulatorwindow::sendmsg()
{
QString line = textsim.readLine();
QString title = line.section(',', 0,0);
QString chopped = line.section(',', 1,1);
}
Upvotes: 0
Views: 1500
Reputation: 409176
In on_simON_clicked
you define textsim
as a local variable, and you use a variable of the same name in sendmsg
. But it is not the same variable!
In on_simON_clicked
you should use the (apparently) member variable instead, as the local variable is not available outside the function. If you turn on more warnings in the compiler you will get a warning about having a local variable "shadow" a member/global variable.
Upvotes: 1
Reputation: 11736
Instead of opening the file every time the timer slot fires, make the QFile a member of simulatorwindow. Open it when the program starts, read from it whenever the timer fires.
Upvotes: 0