0

我想使用计时器每秒读取一行文件。一旦定时器启动,读取第一行,一秒钟后,读取第二行......

但是在 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);
}
4

2 回答 2

1

on_simON_clicked您定义textsim为局部变量,并在sendmsg. 但它不是同一个变量!

on_simON_clicked你应该使用(显然)成员变量,因为局部变量在函数之外不可用。如果您在编译器中打开更多警告,您将收到有关将局部变量“遮蔽”成员/全局变量的警告。

于 2012-07-16T05:37:06.027 回答
0

不是每次定时器槽触发时都打开文件,而是让 QFile 成为simulatorwindow 的成员。程序启动时打开它,每当计时器触发时从它读取。

于 2012-07-16T04:19:52.470 回答