0

In my code I have the following function that updates a qlabel. The qlabel is updated very frequently. The code is

void foo::someMethod(std:atring a)
{
     {//begin
     boost::lock_guard<boost::mutex> lock(mutex_label);
     frm->mylabel->setText(a.c_str());
     }//end lock
}

And then after a while I get an assertion error:

assert failure in documentRect: "document rect called for label that is not a text label! , file widgets\qlabel.cpp

Any suggestions what might be causing it ? Also sometimes the value is never shown. I have to move the form for the values to be updated

Rajeshwar
  • 11,179
  • 26
  • 86
  • 158

1 Answers1

3

You cannot touch the GUI from another thread. No amount of mutexes will help here, Qt does not support that. The correct way is doing this via signals and slots.

You can read more at Qt signaling across threads, one is GUI thread? .

Community
  • 1
  • 1
Jan Kundrát
  • 3,700
  • 1
  • 18
  • 29
  • Could you explain that a little. Are you suggesting that I create a signal and then pass data using that signal ? – Rajeshwar Apr 17 '13 at 16:36
  • You can't call setText from a non-GUI thread (for the most part, the first thread started in your program is the GUI thread). Instead, declare a slot that executes in the main thread, and connect signals (emitted in non-gui threads) that connect to it. That way the setText will only be called from the GUI thread. – Michael Kohne Mar 09 '14 at 15:27