Search
 
SCRIPT & CODE EXAMPLE
 

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();
}
Comment

qt c++ thread example

Worker* worker = new Worker(mythingy);
QThread* thread = new QThread();
worker->moveToThread(thread);
thread->start();

//wrong: directly invoking doWork in mainthread    
worker->doWork();

//correct: through signal-slot mechanics
connect(this, SIGNAL(startWork()), worker, SLOT(doWork()));
Comment

PREVIOUS NEXT
Code Example
Cpp :: c++ void pointer 
Cpp :: glm multiply vector by scalar 
Cpp :: run a c++ file in terminal 
Cpp :: Stream Overloading 
Cpp :: why does the pointer value doesn;t change when I change it in funciton 
Cpp :: how to delete repeated element in stack c++ 
Cpp :: c++ loop through an array 
Cpp :: count substrings codechef solution in c++ 
Cpp :: printing sub arrays 
Cpp :: determining whether a array is a subsequence of another array 
Cpp :: how to delay text in c++ console app 
Cpp :: In-range Adder 
Cpp :: How to assign two dimensional initializer list in c++ 
Cpp :: sort 3 numbers using swap cpp 
Cpp :: vector.rbegin() 
Cpp :: online c++ graphics compiler 
Cpp :: tic tac toe in cpp 
Cpp :: c++ to c converter 
Cpp :: c++ sleep function 
Cpp :: inverse lerp c++ 
Cpp :: assignment operator 
Cpp :: palindrome no example 
Cpp :: convert from hex to decimal c++ 
Cpp :: aliasing c++ 
Cpp :: freeing array in c++ 
C :: c bold text 
C :: c remove last character from a string 
C :: convert string to float c 
C :: find power of a number in c 
C :: successeur ("123") 
ADD CONTENT
Topic
Content
Source link
Name
9+2 =