qdialog中qtextbrowser的动态更新来自文件(由另一个作业更新)
Dynamic update of QTextBrowser in a Qdialog from a file(that gets updated by another job)
我有一个日志文件,该文件在运行时会更新。我想要文本浏览器中显示的文本内容,并且应该动态更新。
.h文件
public:
void fillloginfo();
void initializeQTimer();
void closeEvent(QCloseEvent *event);
void fillLogInfoChronically(const QString& logFilePath);
private:
QTimer* m_Timer;
QString m_logFilePath;
std::ifstream m_logFileStream;
public slots:
void fillLogInfoChronicallySlot();
.cpp文件
void logdialog::initializeQTimer(){
m_Timer = NULL;
//create the timer object
m_Timer = new QTimer(this);
QObject::connect(m_Timer,SIGNAL(timeout()), this,SLOT(fillLogInfoChronicallySlot()));
}
void logdialog::closeEvent(QCloseEvent *event)
{
m_Timer->stop();
if ( m_logFileStream.is_open()){
m_logFileStream.close();
}
}
void logdialog::fillLogInfoChronically(const QString &logFilePath)
{
uilog->textBrowser->clear();
m_LastLinePos = 0;
m_logFilePath = logFilePath;
std::string m_logFilePathStr= m_logFilePath.toStdString();
m_logFileStream.open(m_logFilePathStr.c_str());
if (m_logFileStream.is_open()){
fillloginfo();
m_Timer->start(1000);
}
}
void logdialog::fillloginfo()
{
std::string line;
while (getline(m_logFileStream,line)){
uilog->textBrowser->append(QString::fromStdString(line));
}
}
void logdialog::fillLogInfoChronicallySlot()
{
fillloginfo();
}
因此,我只能在第一个呼叫上读取文件,其余的呼叫以获取文件的更新不起作用。
预先感谢
您需要在初始读取后在输入流中调用std::ios::clear()
。当您读取整个文件时,它将在流中设置failbit
并拒绝继续读取,即使文件在此期间发生了变化。
在您的情况下,您必须再次阅读之前:
void logdialog::fillloginfo()
{
std::string line;
m_logFileStream.clear();
while (getline(m_logFileStream,line)){
uilog->textBrowser->append(QString::fromStdString(line));
}
}
完整的代码在以下链接中
相关文章:
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- 运行同一解决方案的另一个项目的项目
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 基于另一个成员参数将函数调用从类传递给它的一个成员
- C++从另一个类访问公共静态向量的正确方法是什么
- C++-试图将函数指针推回到另一个CPP文件中的矢量时出错
- 使用std::transform将一个范围的元素添加到另一个范围中
- 输入到文件并输出到另一个文件,并将流文件传递给函数
- 从另一个线程更新QT小部件的一种详细方法
- 我能否确定在被另一个实体修改时会更新常量引用
- qdialog中qtextbrowser的动态更新来自文件(由另一个作业更新)
- 如何通过在另一个线程中提出的升压信号更新QT GUI
- Gtkmm : 如何从另一个线程更新 UI?不断
- 从另一个类中更新QProgressBar
- 如何从另一个线程更新列表视图
- SQL基于一个表中的行而不是另一个表的行来更新和联接三个表
- 从另一个cpp更新文本框值
- 将一个向量移动到另一个,地址没有更新
- 从另一个对话框更新对话框时出现问题
- 如何将 std::map 元素从一个减去到另一个并在 C++ 中更新它