Modifying QLabel text from another thread
-
This code below modifies QLabel text from another thread, but doesn't throw cross thread exception, as i remember Qt throws an exception if gui was modified from another thread, or I'm missing something!!
class myWidget: public QWidget { myWidget():QWidget(){ m_label = new QLabel("text from main thread"); // setup UI } public slots: void onButtonClick(){ QtConcurrent::task([](QLabel* label){ label->setText("text from thread pool"); }) .args(m_label) .spawn(QtConcurrent::FutureResult::Ignore); } };Thanks in advance.
-
This code below modifies QLabel text from another thread, but doesn't throw cross thread exception, as i remember Qt throws an exception if gui was modified from another thread, or I'm missing something!!
class myWidget: public QWidget { myWidget():QWidget(){ m_label = new QLabel("text from main thread"); // setup UI } public slots: void onButtonClick(){ QtConcurrent::task([](QLabel* label){ label->setText("text from thread pool"); }) .args(m_label) .spawn(QtConcurrent::FutureResult::Ignore); } };Thanks in advance.
@0x4D6F652E51 said in Modifying QLabel text from another thread:
as i remember Qt throws an exception if gui was modified from another thread
I'm not sure it does, or maybe that it is not guaranteed to do so and behaviour might have changed or whatever.
I thought you are simply not supposed to alter a UI object from a secondary thread, and it is your responsibility not to do so. Are you just complaining that you know you must not do this but are disappointed not to get a warning?
-
This code below modifies QLabel text from another thread, but doesn't throw cross thread exception, as i remember Qt throws an exception if gui was modified from another thread, or I'm missing something!!
class myWidget: public QWidget { myWidget():QWidget(){ m_label = new QLabel("text from main thread"); // setup UI } public slots: void onButtonClick(){ QtConcurrent::task([](QLabel* label){ label->setText("text from thread pool"); }) .args(m_label) .spawn(QtConcurrent::FutureResult::Ignore); } };Thanks in advance.
@0x4D6F652E51 said in Modifying QLabel text from another thread:
as i remember Qt throws an exception if gui was modified from another thread
No, it does not.
You're on your own checking all gui related stuff is done in the main (gui) thread. -
This code below modifies QLabel text from another thread, but doesn't throw cross thread exception, as i remember Qt throws an exception if gui was modified from another thread, or I'm missing something!!
class myWidget: public QWidget { myWidget():QWidget(){ m_label = new QLabel("text from main thread"); // setup UI } public slots: void onButtonClick(){ QtConcurrent::task([](QLabel* label){ label->setText("text from thread pool"); }) .args(m_label) .spawn(QtConcurrent::FutureResult::Ignore); } };Thanks in advance.
@0x4D6F652E51 said in Modifying QLabel text from another thread:
as i remember Qt throws an exception if gui was modified from another thread
From my experience: Sometimes it crashes when you do this. Maybe most of the time it doesn't. But even if it doesn't it most likely will on the next computer. Qt does not give any warning.