0

I have a function called refreshLogDisplay() in my MainWindow class which does a lot of UI work. The code in it is like this:

ui->tablewidget->setRowCount(100);
// ...     

So the function deals with a lot of protected properties of MainWindow class. But I want to move the function to another QThread. In that QThread, I want a while loop to call the 'refreshLogDisplay' 500 times per second. The 500 times per second is very important, and I do not want any timer to do this because timers are too slow.

I only know how to implement a subclass inheriting QThread, which cannot access ui->tablewidget things. There is a QObject::moveToThread() function, but it doesn't seem to help.

Any good suggestions?

Al2O3
  • 3,103
  • 4
  • 26
  • 52
  • 2
    For starters, check the refresh rate of your monitor. That is the _absolute maximum_ amount of different images per second you are going to see. If the monitor refresh rate is 60 Hz, there is no need to make any user interface updates more often than 60 times per second. –  Oct 19 '12 at 16:31
  • For example, [this question](http://stackoverflow.com/questions/8701781/updating-qlabel-in-non-gui-thread-continuously) is about accessing GUI from other threads. –  Oct 19 '12 at 16:34
  • See also this note on why subclassing QThread is something you only want to do when you're adding management functionality to QThread that it doesn't already have built-in (as opposed to methods that involve code that *actually runs* on another thread): http://blog.qt.digia.com/2010/06/17/youre-doing-it-wrong/ – HostileFork says dont trust SE Oct 19 '12 at 16:54
  • You can't do UI work in a background thread. You can decide what to do in a background thread, but actual UI actions cannot be done there. Changing the rowCount of a table widget must be done in the GUI thread, no ifs ands or buts. If that's the slow part of the program, you're just going to have to figure out how to do it less often. – cgmb Oct 19 '12 at 16:57
  • Regarding what @Slavik81 said, see my answer here for the finer points of how even if you separate the model and the view widget out *and* use locking to control data access, it doesn't work: http://stackoverflow.com/questions/9485339/design-pattern-qt-model-view-and-multiple-threads/9489604#9489604 – HostileFork says dont trust SE Oct 19 '12 at 19:03

1 Answers1

0

You can't control UI from thread,you have to build Slots and connect them to the signals in the thread . like this :

void Whatever::increment(){i+=2;emit incremental();} 
void Default::Progress(){ui->tablewidget->setRowCount(i);}
void main(){ //do thread
connect(Whatever,SIGNAL(increamental()),this,SLOT(Progress()));}
Amr Jer
  • 55
  • 7