Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Example for Parallel Programming in Qt

Example for Parallel Programming in Qt

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 4.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mounipanditi
    wrote on last edited by
    #1

    Hi I am a beginner in Qt and while i was browsing about parallel programming in Qt to keep GUI responsive if a task more than normal time i.e 5sec i found this link

    https://doc.qt.io/archives/qq/qq27-responsive-guis.html

    I need a simple example to understand about Parallel Programming in Qt.
    Please help me.

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      Hi and welcome
      Use the worker approach
      https://john.nachtimwald.com/2015/05/02/effective-threading-using-qt/
      https://mayaposch.wordpress.com/2011/11/01/how-to-really-truly-use-qthreads-the-full-explanation/

      Minimal code. Something like.

      class Task : public QObject
      {
      Q_OBJECT
      public:
          Task(){};
          ~Task(){};
      public slots:
          void doWork();
      signals:
          void workFinished();
      };
      
       QThread* thread = new QThread( );
        Task*    task   = new Task();
        task->moveToThread(thread);
        connect( thread, SIGNAL(started()), task, SLOT(doWork()) );
        connect( task, SIGNAL(workFinished()), thread, SLOT(quit()) );
        //automatically delete thread and task object when work is done:
        connect( thread, SIGNAL(finished()), task, SLOT(deleteLater()) );
        connect( thread, SIGNAL(finished()), thread, SLOT(deleteLater()) );
        thread->start();
      
      void Task::doWork() {
        static int cc = 0;
        while(1) { // ugly
          qDebug() << "alive:" << cc++;
        }
      }
      
      

      Note there is also qtconcurrent
      http://doc.qt.io/qt-5/qtconcurrent-runfunction-main-cpp.html

      So it depends on what you need. :)

      Notice that Qt does not come with beginner documentation on Threads
      That you must already know some about.
      http://doc.qt.io/qt-5/thread-basics.html

      M 1 Reply Last reply
      3
      • mrjjM mrjj

        Hi and welcome
        Use the worker approach
        https://john.nachtimwald.com/2015/05/02/effective-threading-using-qt/
        https://mayaposch.wordpress.com/2011/11/01/how-to-really-truly-use-qthreads-the-full-explanation/

        Minimal code. Something like.

        class Task : public QObject
        {
        Q_OBJECT
        public:
            Task(){};
            ~Task(){};
        public slots:
            void doWork();
        signals:
            void workFinished();
        };
        
         QThread* thread = new QThread( );
          Task*    task   = new Task();
          task->moveToThread(thread);
          connect( thread, SIGNAL(started()), task, SLOT(doWork()) );
          connect( task, SIGNAL(workFinished()), thread, SLOT(quit()) );
          //automatically delete thread and task object when work is done:
          connect( thread, SIGNAL(finished()), task, SLOT(deleteLater()) );
          connect( thread, SIGNAL(finished()), thread, SLOT(deleteLater()) );
          thread->start();
        
        void Task::doWork() {
          static int cc = 0;
          while(1) { // ugly
            qDebug() << "alive:" << cc++;
          }
        }
        
        

        Note there is also qtconcurrent
        http://doc.qt.io/qt-5/qtconcurrent-runfunction-main-cpp.html

        So it depends on what you need. :)

        Notice that Qt does not come with beginner documentation on Threads
        That you must already know some about.
        http://doc.qt.io/qt-5/thread-basics.html

        M Offline
        M Offline
        mounipanditi
        wrote on last edited by
        #3

        @mrjj

        Hi @mrjj thanks for sharing links, i will go through them and i will do my best to understand concepts.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved