c++ - Qt crashes/doesn't appear when i use Qthread::sleep to update progress bar -


i kinda new qt wondering why invalid:

i have progress bar , want update using class inherits qthread.

void mt::run(qprogressbar * asd){  for(int = 0;i<100;i++){     asd->setvalue(i);     qthread::sleep(100); }  } 

mt class inherits qthread. run overloaded qprogressbar argument. main ui thread send it's progressbar m.run(ui->progressbar);. if remove qthread::sleep(100); work fine won't able see increment because thread done fast. if put little delay, screen won't appear @ all.

you can access , update gui elements main thread only.

if want prepare data inside custom thread, should use signals/slots mechanism send data widgets.

here's basic example:

class mythread : public qthread {     q_object public:     mythread(qobject *parent = 0);  signals:     void valuechanged(int value);  protected:     void run(); }  void mythread::run() {     (int = 0; < 100; i++)     {         emit valuechanged(i);         qthread::sleep(100);     } }  mywidget::mywidget(qwidget *parent) :      qwidget(parent) {     qhbovlayout *layout = new qhbovlayout(this);     qprogressbar *pb = new qprogressbar;     layout->addwidget(pb);      mythread *t = new mythread(this);     connect(t, signal(valuechanged(int)), pb, slot(setvalue(int)));      t->start(); }  int main(int argc, char *argv[]) {     qapplication a(argc, argv);      // in main thread here, can create widget     mywidget w;     w.show();      return a.exec(); } 

Comments

Popular posts from this blog

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -

YouTubePlayerFragment cannot be cast to android.support.v4.app.Fragment -