bool read{ // вызываеться из главного потока TThreadBD *threadBD = new TThreadBD(obj,qry,str); threadBD->start(); TThreadProgress * threadProgress = new TThreadProgress(qApp->mainWidget()); threadProgress->start(); int i =0 ; //QWaitCondition waitCondition; while((threadBD->finished () == false)&& (i < 1000000)){ // waitCondition.wait(10); qApp->processEvents(); i++; } if(threadBD->finished () == false) threadBD->terminate () ; threadProgress->terminate(); return }
void frmMain::customEvent( QCustomEvent *pe ){ if((int)pe->type() == TEventProgress::TypeEvent){ int t = ((TEventProgress*)(pe))->value() ; progressbar->setProgress(t); } else QWidget::customEvent(pe); }
class TThreadProgress:public QThread{private: QObject * m_pobjReceiver; volatile bool stoped;public: TThreadProgress(QObject * pobjReceiver); virtual void run(); void stop();};class TEventProgress: public QEvent {private: int m_nValue;public: enum {TypeEvent = User+1}; TEventProgress():QEvent((Type)TypeEvent){} void setValue(int n); int value() const;};void TThreadProgress::run(){ int i=0; while((stoped == false) && (i < 10000)){ usleep(10); TEventProgress * pe = new TEventProgress(); pe->setValue(i); QApplication::postEvent(qApp->mainWidget(),pe); qApp->processEvents(); i++; } stoped =true;}void TThreadProgress::stop(){ stoped = true;}void TEventProgress::setValue(int n){ m_nValue =n ;}int TEventProgress::value() const{ return m_nValue;}}
C++ (Qt)void frMain::StartRead( void ){ threadBD = new TThreadBD(obj,qry,str); connect(threadBD, SIGNAL(finished()), this, SLOT(EndRead())); progressbar->setValue(0); progressbar->show(); threadBD->start();} void frMain::EndRead( void ){ delete threadBD; threadBD = 0; progressbar->hide();}
bool frmMain::readFromBD( QSqlQuery qry, QString str ){ TThreadBD *pthreadBD = new TThreadBD(this,qry,str); pthreadBD->start(); int i =0 ; progressbar->setTotalSteps (10000); //QWaitCondition waitCondition; while( pthreadBD->finished () == false) { progressbar->setProgress(i); // waitCondition.wait(100); i++; qApp->processEvents(); }
void TThreadBD::run(){ int i=0; //readFromBd(); for(i =0 ;i< 10;i++) { usleep(100); qApp->processEvents();}
void TThreadBD::run(){ int i=0; //readFromBd(); for(i =0 ;i< 10;i++) { usleep(100);// qApp->processEvents(); // <<<<<<<<<<< }
C++ (Qt)void TThreadBD::run(){ //readFromBd(); for(int i =0 ; i < 1000; i++) { usleep(1000); emit progressUpdate(i); }} void frmMain::progressUpdate( int value ){ progressbar->setProgress(value); }