Linux technical support - [email protected]


Qt multithreading example

#include "mainwindow.h"
    #include <QApplication>
    #include <QDebug>
    #include <QThread>
     
    class MyFirstThread : public QThread
    {
    private:
        void run()
        {
            while(1)
            {
                qDebug() << "MyFirstThread";
                sleep(2);
            }
        }
    };
     
    class MySecondThread : public QThread
    {
    private:
        void run()
        {
            while(1)
            {
                qDebug() << "MySecondThread";
                sleep(4);
            }
        }
    };
     
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        MainWindow w;
        w.show();
     
        MyFirstThread t1;
        MySecondThread t2;
     
        t1.start();
        t2.start();
     
        return a.exec();
    }

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>