您可以使用计时器来触发连续行的读取。最好将此功能分离到它自己的类中,该类在每次有新行可用时发出一个信号:
// https://github.com/KubaO/stackoverflown/tree/master/questions/timed-read-44319722
#include <QtWidgets>
class PeriodicReader : public QObject {
Q_OBJECT
QTimer m_timer{this};
QFile m_file{this};
void readLine() {
if (m_file.atEnd()) {
m_timer.stop();
return;
}
emit newLine(m_file.readLine());
}
public:
explicit PeriodicReader(QObject * parent = {}) : QObject(parent) {
connect(&m_timer, &QTimer::timeout, this, &PeriodicReader::readLine);
}
void load(const QString & fileName) {
m_file.close(); // allow re-opening of the file
m_file.setFileName(fileName);
if (m_file.open(QFile::ReadOnly | QFile::Text)) {
readLine();
m_timer.start(300); // 0.3s interval
}
}
Q_SIGNAL void newLine(const QByteArray &);
};
由于我们使用 aQPlainTextEdit
来显示文本,我们需要将原始行转换为字符串。我们必须删除任何行尾,因为QPlainTextEdit::appendPlainText
已经添加了一个段落结尾:
QString lineToString(QByteArray line) {
while (line.endsWith('\n') || line.endsWith('\r'))
line.chop(1);
return QString::fromUtf8(line);
}
现在把它放在一个演示中是一件简单的事情:
int main(int argc, char ** argv) {
QApplication app{argc, argv};
QWidget window;
QVBoxLayout layout{&window};
QPushButton load{"Load"};
QPlainTextEdit edit;
layout.addWidget(&load);
layout.addWidget(&edit);
window.show();
PeriodicReader reader;
QObject::connect(&load, &QPushButton::clicked, [&]{
auto name = QFileDialog::getOpenFileName(&window);
if (!name.isEmpty()) {
edit.clear(); // allow re-opening of the file
reader.load(name);
}
});
QObject::connect(&reader, &PeriodicReader::newLine, &edit,
[&](const QByteArray & line){ edit.appendPlainText(lineToString(line)); });
return app.exec();
}
#include "main.moc"
完整的例子到此结束。