C++ (Qt)struct CThreadUserProc : public QThread { CThreadUserProc( QRunnable & runner ) : mRunner(runner) { } virtual void timerEvent ( QTimerEvent * e ) { if (e->timerId() == mTimerId) UpdateIndicator(); } virtual void run( void ) { mRunner.run(); } void ExecTask( int interval = 100 ) { mTimerId = startTimer(interval); start(); while (!isFinished()) qApp->processEvents(QEventLoop::ExcludeUserInputEvents | QEventLoop::WaitForMoreEvents); } private: int mTimerId; QRunnable & mRunner;};
C++ (Qt)StartIndicator(); // напр заряжаем QProgressDialogCThreadUserProc(*this).ExecTask();
C++ (Qt)struct MyClass : public SomeBase, public QRunnable { virtual void run( void ) { SomeLoooongFunc(...); // этот метод мы хотим вынести в поток }};
C++ (Qt)#include <QtWidgets> class MyWidget : public QWidget {public: MyWidget( void ) { resize(320, 240); QPushButton * btn = new QPushButton("Test", this); QObject::connect(btn, &QPushButton::clicked, this, &MyWidget::TestDialog); } void TestDialog( void ) { QProgressDialog progress("Copying files...", "Abort Copy", 0, 0); progress.setWindowModality(Qt::ApplicationModal); progress.show(); while (!progress.wasCanceled()) qApp->processEvents(QEventLoop::WaitForMoreEvents); }}; int main(int argc, char *argv[]){ QApplication app(argc, argv); MyWidget * win = new MyWidget; win->show(); return app.exec();}