UI blocked when writing into textEdit
I've an application witch update execute commands on a database. I've a function witch execute that commands and in the in this function I display some messages to the user.
My problem is that the texts written into the textEdit are displayed at the end of the function work.
I've tried to use QThread but it does'nt resolve the problem
this->moveToThread(&threadText); connect(&threadText, SIGNAL(started()), this, SLOT(writeTexte())); threadText.start();
Does anyone have an idea how to proceed to write into textEdit and not block the UI ?
You were close to the right solution.
thisshould emit a signal and this signal (not the thread started) should be connected to a slot that then prints it to the textEdit
Alternative and worst solution is to add
QApplication::processEvents()to your existing function
thanks for replying.
I've tried the following code but the problem persists
this->moveToThread(&threadText); connect(this, SIGNAL(writeIntoText()), textEdit, SLOT(append(QString))); // writeIntoText() is a signal that I'm created threadText.start(); updateDatabase();
Can you tell what I'm wrong
writeIntoText()should have a qstring arguments that gets passed to the slot so it knows what to write
Thanks for replying,
I've modified the slot signature but the ui is still blocked until the treatement end.
How I must use the thread to unblock the UI when the function is updating database ?
What is the object that contains
I would like to link the textEdit to the Thread but it's impossible with Qt (Message : "Can't move object having parent").
Reading most documents, I found that the problem can be resolved by a simple signal / slot, It's true ? If yes, can you tell me what I'm wrong in this code :
connect(this, SIGNAL(writeIntoText(QString)), textEdit, SLOT(WriteMessage(QString))); // writeIntoText(QString) and WriteMessage(QString) are created by me
I'm afraifd you're not giving us enough information here.
For example if your text/String Generating code is in the same Thread as your UI, the UI will be blocked regardless of yous SIGNAL/SLOT connection working or not.
on the otherhand, how is
created, is it actually an pointer to your QTextEdit or not. What is your compiler error if you have one, you're using the old syntax so you'll see the error messages only after running the program.
try to change it to the new syntax
connect(sender, &QObject::Signal, receiver, &QObject::Slot);
it will not compile if you have an error and you can give us the error msg.
@mourad_bilog You cannot use Qt GUI classes in other threads (only in main/GUI thread).
Yes, signal/slot should work.
Is textEdit a QTextEdit? If so then what you have will not work as there is no such slot in QTextEdit: WriteMessage
Why not append(QString)?
Your setup is currently not clear since it seems you have an object that contains a thread that you move to and that object might or might not be a widget.
The usual modus operandi is something like:
WARNING Code example, it doesn't mean that you have to do this specifically in the main function.
main.cpp: QApplication app(arg, argv); MyWidget widget; MyWorkerObject worker; QThread thread; worker.moveToThread(&thread); connect(&worker, &MyWorkerObject::mySignal, &widget, &MyWidget::mySlot); thread.start(); widget.show(); return app.exec();
you should fix the typo,
workeror its a bit confusing.
@J-Hilk, Thanks for your remarks. Effectively, messages inserted into the TextEdit are in the same Thread used by the UI.
In fact, I've not errors displayed. Juste the UI is blocked during the treatement of database update and all the message are diplayed at the end when function is totaly executed.
textEdit is an object QTextEdit created by the Qt Creator.
@jsulm Thanks for replying. textEdit is a QTextEdit Object. I've used the append signal but I've the following error :
QObject::connect: No such signal QTextEdit::writeMessage(QString) in TheriaUpdater.cpp:866 QObject::connect: (sender name: 'textEdit') QObject::connect: (receiver name: 'frmTheriaUpdater')
As the error message state: you are trying to connect a signal that doesn't exist in QTextEdit.
ok, the correct way to connect your generated QString to be displayed in the textedit should be this: (assuming your class is called MainWindow)
//to Replace old text connect(this, &MainWindow::writeIntoText, ui->textEdit, QTextEdit::setText); //to append new String to the old text connect(this, &MainWindow::writeIntoText, ui->textEdit, QTextEdit::append);
But as long as your function, that "emits writeIntoText" is not wraped into a QThread or QtConcurrent, the gui will most likly still be frozen.