qt监测⽂件新增的内容_Qt实现实时读取与显⽰动态更新的⽂
html内容文本框本⽂件
功能描述:
利⽤QFile读取在不断刷新的⽂本⽂件,并⽤QTextBrowser组件进⾏实时显⽰。
实现:
对于⽂本的实时显⽰主要利⽤定时器QTimer实现 ,每隔x秒则在QTextBrowser中“增量式”显⽰⽂本内容。“增量式”即与上次读到的⽂本相⽐,只在显⽰⽂本中不断增加⽂本中新增的内容。
代码⽰例:
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include #include #include #include #include #include #include #include #include #include #include class RightBrowser : public QWidget
{
Q_OBJECT
public:
explicit RightBrowser(QWidget *parent = nullptr);
QTextBrowser *tb;
QTimer * timerTxt;
int lastReadRowNums=0;//上次读到的⽂件⾏数,初始为0
signals:
public slots:
void showFileContents();
};
mainwindow.cpp
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent):QMainWindow(parent)
{
timerTxt=new QTimer;
tb=new QTextBrowser;
}
void MainWindow::showFileContents()
{
//读已经完全写好的结果⽂件,每隔3秒显⽰⼀⾏数据
QStringList sl;
//右下⽂本框读内容,显⽰
QFile file(":/resultdata/");
if(!file.open(QFile::ReadOnly|QFile::Text))
qDebug()
}
while(!stream.atEnd()){
QString adLine();
if (line.startsWith("#") || line.startsWith("variables"))
continue;
sl.append(" "+immed());//trimmed():Returns a string that has whitespace removed from the start and the end. }
int len=sl.size();
tb->append(sl.at(lastReadRowNums++));
qDebug()
qDebug()
}
}
运⾏截图:
完整项⽬代码获取:
download.csdn/download/vvyingning/10979809

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。