gpt4 book ai didi

c++ - 从文件动态更新 Qdialog 中的 QTextBrowser(由另一个作业更新)

转载 作者:太空狗 更新时间:2023-10-29 22:57:03 28 4
gpt4 key购买 nike

我有一个日志文件,它会在作业运行时更新。我想要在文本浏览器中显示的文本内容,并且应该动态更新。

.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));
}
}

完整代码在下面link

关于c++ - 从文件动态更新 Qdialog 中的 QTextBrowser(由另一个作业更新),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46068390/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com