I am trying to read a log file in the background and update the text in
QTextEdit
. Below is the code. But once I emit the signal the UI freezes. Can someone point me out what I am doing wrong here with QtConcurrent?Connect the signal to the slot
connect(this, SIGNAL(SignalUpdateLog(QString)),this,SLOT(SlotUpdateLog(QString)));
Update Log Button Event
void on_ButtonClicked()
{
...
//Show busy dialog
QtConcurrent::run(this, &ClassA::UpdateReaderLog);
}
Background Task
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
}
}
The Slot
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);
}
}
Edit: Changed to emit signal to show pop up from UpdateReaderLog() for file open failure