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
Please read Threads and Event loops first.
while (!file.atEnd())
{
emit SignalUpdateLog(in.readLine());
}
You are emitting signals continuously in this while(!file.atEnd())
loop but event loop has no chance to process these signals and send to the destined object; since UI is not being drawn because of this busy loop your UI is frozen.
Another problem is that your slot is being invoked in wrong thread, you need to Qt::connect
your signal with Qt::QueuedConnection
as a third argument. It might fix your problem but be aware of your design choice.
Connect signal-slot with Qt::QueuedConnection. In multi-thread, you should connect signals-slots with different types depending on practical situations .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.