class Worker: public QObject { Q_OBJECTpublic: Worker(); ~Worker();private slots: void startPause(); void process();private: QThread _thread;};
Worker::Worker() { connect( &_thread, &QThread::started, this, &Worker::process ); connect( MainWindow::ui().startPauseButton, &QPushButton::clicked, this, &Worker::startPause);}Worker::~Worker() {}void Worker::process() { while(1) { // useful commands QThread::usleep( 100 ); }}void Worker::startPause() { qDebug() << "s"; if( !_thread.isRunning() ) { moveToThread(&_thread); _thread.start(); }}
int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); Worker worker; return a.exec();}
moveToThread(&_thread);
void Worker::startPause() { qDebug() << "s"; if( !_thread.isRunning() ) { _thread.start(); moveToThread(&_thread); }}
void Worker::process() { while(1) { // useful commands QThread::usleep( 100 ); }}
connect( &_thread, &QThread::started, this, &Worker:: // process???
C++ (Qt) class Worker : public QObject{ Q_OBJECT // ... Q_SLOT void processSlot ();}; class WorkThread : public QThread{ Q_OBJECT private: Worker m_worker; public: WorkThread ( QObject * const parent = 0 ); protected: virtual void run () final; public: Q_SIGNAL void processSlot ();};
C++ (Qt) void Worker::processSlot (){ // my process realization}; void WorkThread::run (){ Worker worker; connect( this, &WorkThread::processSlot, &worker, &Worker::processSlot ); exec();}
C++ (Qt) void foo (){ // ... WorkThread thread; connect( &invoker, &processInvoked, thread, &processSlot ); thread.start(); // ...};
C++ (Qt)connect( &_thread, &QThread::started, this, &Worker:: // process???
C++ (Qt)while(1) { // useful commands QThread::usleep( 100 );}
C++ (Qt)class Worker : public QObject { Q_OBJECT public: Worker(QObject *parent = 0); ~Worker(); public slots: void slot(); private slots: void start(); void process(); private: QThread _thread;};
C++ (Qt)Worker::Worker(QObject *parent) : QObject(parent) { connect( &_thread, &QThread::started, this, &Worker::process ); connect( MainWindow::ui().startPushButton, &QPushButton::clicked, this, &Worker::start ); connect( MainWindow::ui().signalPushButton, &QPushButton::clicked, this, &Worker::slot );} Worker::~Worker() {} void Worker::start() { _thread.start(); moveToThread(&_thread); qDebug() << "Worker::start()";} void Worker::process() { qDebug() << "Worker::process()"; while(1) { ; }} void Worker::slot() { qDebug() << "Worker::slot()";}
C++ (Qt)int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; Worker worker; w.show(); return a.exec();}
while(1) { ; }
C++ (Qt) Worker::Worker(QObject *parent) : QObject(parent) { connect( &_thread, &QThread::started, this, &Worker::process ); connect( MainWindow::ui().startPushButton, &QPushButton::clicked, this, &Worker::start ); connect( MainWindow::ui().signalPushButton, &QPushButton::clicked, this, &Worker::slot );}