Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CPP

qt c++ thread example

MainWindow::MainWindow(QWidget* parent)
    : QMainWindow(parent)
{
    mythingy = new QObject(this);
    QThread* thisthread = this->thread();
    QThread* mainthread = QCoreApplication::instance()->thread();
    //breakpoint here to check thisthread and mainthread
    //*****************
    Worker* worker = new Worker(mythingy);
    QThread* thread = new QThread();
    worker->moveToThread(thread);
    thread->start();
    //*****************
    connect(worker, SIGNAL(deleteObject(QObject*)), this, SLOT(deleteObject(QObject*)));
}

Worker::Worker(QObject* thingy, QObject* parent)
    : QObject(parent)
{
    mythingy = thingy;
//    QThread* thread = new QThread(this);
//    this->moveToThread(thread);

    //use a timer to allow the constructor to exit
    QTimer* timer = new QTimer(this);
    timer->setSingleShot(true);
    timer->start(1000);
    connect(timer, SIGNAL(timeout()), this, SLOT(doWork()));

//    QThread* thisthread = this->thread();
//    QThread* mainthread = QCoreApplication::instance()->thread();
    //breakpoint here to check thisthread and mainthread
//    thread->start();
}
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #qt #thread
ADD COMMENT
Topic
Name
1+2 =