我想使用计时器每秒读取一行文件。定时器启动后,读第一行,一秒后,读第二行……
但是QTextStream中没有读取特定行的函数。关于如何实现这一目标的任何想法?
如果我运行下面的代码,它总是会返回
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);
}
最佳答案
在 on_simON_clicked
中,您将 textsim
定义为局部变量,并在 sendmsg
中使用同名变量。但它不是同一个变量!
在 on_simON_clicked
中,您应该改用(显然)成员变量,因为局部变量在函数外不可用。如果您在编译器中打开更多警告,您将收到关于让局部变量“影子”成员/全局变量的警告。
关于c++ - 如何使用定时器一次使用QTextStream readline,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11497860/