我正在尝试在后台读取日志文件并更新
QTextEdit
. 下面是代码。但是一旦我发出信号,用户界面就会冻结。有人可以指出我在使用 QtConcurrent 时做错了什么吗?将信号连接到插槽
connect(this, SIGNAL(SignalUpdateLog(QString)),this,SLOT(SlotUpdateLog(QString)));
更新日志按钮事件
void on_ButtonClicked()
{
...
//Show busy dialog
QtConcurrent::run(this, &ClassA::UpdateReaderLog);
}
后台任务
void ClassA::UpdateReaderLog()
{
QFile file("/home/Debug.txt");
if (file.open(QIODevice::ReadOnly | QIODevice::Text))
{
QTextStream in(&file);
in.setCodec("UTF-8");
while (!file.atEnd())
{
emit SignalUpdateLog(in.readLine());
}
emit SignalUpdateLog("finishedReadingFile");
qWarning("\nRead finished");
}
else
{
//! emit signal to show failure pop up
}
}
插槽
void ClassA::SlotUpdateReaderLog(QString str)
{
if(str.contains("finishedReadingFile"))
{
qWarning("\nSetting reader screen");
SetToScreen(SCREEN__READER_LOG);
//Close the busy dialog
}
else
{
ui->textEditReaderLog->append(str);
}
}
编辑: 更改为发出信号以显示从 UpdateReaderLog() 弹出的文件打开失败