Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. QThread with Android
Forum Updated to NodeBB v4.3 + New Features

QThread with Android

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
12 Posts 2 Posters 2.0k Views
  • 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.
  • A Offline
    A Offline
    ashajg
    wrote on 21 Nov 2018, 05:47 last edited by
    #1

    Hi Guys
    I have written a code to demonstrate QThread on Android mobile.

    header:
    #ifndef FUNCTIONCLASS_H
    #define FUNCTIONCLASS_H
    #include <QObject>
    #include<QThread>
    #include<QDebug>
    class FunctionClass : public QObject
    {
         Q_OBJECT
    public:
        FunctionClass();
    signals: void signalofmainclass();
    public slots:
       void workdone();
    };
    #endif // FUNCTIONCLASS_H
    

    .CPP:

    #include "functionclass.h"
    
    FunctionClass::FunctionClass()
    {
    }
    void FunctionClass::workdone()
    {    qDebug()<<"In workdone class";
        // qDebug()<<"FUNCTION CALLED:"<<Q_FUNC_INFO<<endl;
        // qDebug()<<"storedatathread:"<<QThread::currentThreadId()<<endl;
         QThread::sleep(10);
          qDebug()<<"*******************8SUCCESS**************************";
         emit signalofmainclass();
    }
    

    ///////////////////////////////////////////////////////////////////////////////////////////
    header:

    #ifndef MULTICLASS_H
    #define MULTICLASS_H
    #include <QObject>
    #include <functionclass.h>
    class Multiclass :  public QThread
    {
        Q_OBJECT
    public:
        explicit Multiclass();
        signals: void dicomStoreComplete();
    protected :
        void run() override;
    private: FunctionClass *funt;
    public slots:
    };
    #endif // MULTICLASS_H
    
    .cpp:
    #include "multiclass.h"
    Multiclass::Multiclass()
    {
    }
    void Multiclass::run()
    {
     funt = new FunctionClass;
      qDebug()<<"IN multiclass";
      qDebug()<<"WORKER THREAD ID:"<<QThread::currentThreadId()<<endl;
      connect(funt,SIGNAL(signalofmainclass()),this,SIGNAL(dicomStoreComplete()));
      funt->workdone();
    }
    

    ///////////////////////////////////////////////////////////////////////////////////////////

    #include <QGuiApplication>
    #include <QQmlApplicationEngine>
    #include <functionclass.h>
    #include <QQmlContext>
    #include<QDebug>
    #include <multiclass.h>
    int main(int argc, char *argv[])
    {
        QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
        QGuiApplication app(argc, argv);
        qDebug()<<"MAIN THREAD ID:"<<QThread::currentThreadId()<<endl;
        FunctionClass DB;
        Multiclass *task=new Multiclass;
        QQmlApplicationEngine engine;
        QQmlContext *context =engine.rootContext();
        context->setContextProperty("db",task);
        engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
        if (engine.rootObjects().isEmpty())
            return -1;
        return app.exec();
    }
    

    ////////////////////////////////////////////////////////////////////
    My expectation is when i click 2 times on my screen this statement should execute 2 times with a duration of 10 sec
    But it is not executing twice it is just executing once.
    qDebug()<<"8SUCCESS*****";**
    My QML FILE is:

    import QtQuick 2.6
    import QtQuick.Window 2.2
    
    Window {
        visible: true
        width: 640
        height: 480
        title: qsTr("Hello World")
    
        Rectangle
        {id:xyz
        anchors.fill:parent
        color:"RED"
        MouseArea
        {
        anchors.fill:parent
        onClicked:{db.start();xyz.color="GREEN";}
    
        }
        }
    
        Connections
        {
            target:db
            onDicomStoreComplete:{console.log("lunch");}
        }
    }
    

    What is missing in my code?

    1 Reply Last reply
    0
    • J Offline
      J Offline
      J.Hilk
      Moderators
      wrote on 21 Nov 2018, 05:52 last edited by J.Hilk
      #2

      @ashajg said in QThread with Android:
      besides a bunch of other stuff, done do not use Thread::sleep, and it should work.

      QThread::sleep(10);


      edit that should have been don't instead of done


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      1 Reply Last reply
      0
      • A Offline
        A Offline
        ashajg
        wrote on 21 Nov 2018, 06:01 last edited by
        #3

        @J.Hilk said in QThread with Android:

        Thread::sleep

        Hi ,

        I have added #include<thread> still Thread::sleep is throwing error that Thread is undeclared.

        J 1 Reply Last reply 21 Nov 2018, 06:04
        0
        • A ashajg
          21 Nov 2018, 06:01

          @J.Hilk said in QThread with Android:

          Thread::sleep

          Hi ,

          I have added #include<thread> still Thread::sleep is throwing error that Thread is undeclared.

          J Offline
          J Offline
          J.Hilk
          Moderators
          wrote on 21 Nov 2018, 06:04 last edited by J.Hilk
          #4

          @ashajg

          try

          void FunctionClass::workdone()
          {    qDebug()<<"In workdone class";
          
               QTime time; 
               time.start();
               while(time.elapsed() < 10000) {}
          
                qDebug()<<"*******************8SUCCESS**************************";
               emit signalofmainclass();
          }
          

          You're not meant to call QThread::sleep, in Qt. All kinds of strange things happen, when one does it.


          actually what you should rather do is something like this:

          void FunctionClass::workdone()
          {    qDebug()<<"In workdone class";
          
               QTimer::singleShot(10000, this, [=]()->void{
                    qDebug()<<"*******************8SUCCESS**************************";
                    emit signalofmainclass();
               });
          }
          

          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          1 Reply Last reply
          0
          • A Offline
            A Offline
            ashajg
            wrote on 21 Nov 2018, 06:25 last edited by
            #5

            @J-Hilk

            I tried both things but didn't work.

            J 1 Reply Last reply 21 Nov 2018, 06:30
            0
            • A ashajg
              21 Nov 2018, 06:25

              @J-Hilk

              I tried both things but didn't work.

              J Offline
              J Offline
              J.Hilk
              Moderators
              wrote on 21 Nov 2018, 06:30 last edited by
              #6

              @ashajg
              looking a bit closer at your code.

              You have only one instance of your "multiclass" where you call start on, start calls automatically run, you can't simply call again start when the thread is already running!
              They way you created this makes this currently a one time use only instance/function


              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              1 Reply Last reply
              0
              • A Offline
                A Offline
                ashajg
                wrote on 21 Nov 2018, 06:37 last edited by
                #7

                @J-Hilk

                Ok.
                Can you please suggest what change I should do to sort this issue.

                J 1 Reply Last reply 21 Nov 2018, 06:59
                0
                • A ashajg
                  21 Nov 2018, 06:37

                  @J-Hilk

                  Ok.
                  Can you please suggest what change I should do to sort this issue.

                  J Offline
                  J Offline
                  J.Hilk
                  Moderators
                  wrote on 21 Nov 2018, 06:59 last edited by
                  #8

                  @ashajg
                  I think subclassing QThread is for your case the wrong solution. It is in fact the wrong one for most cases.
                  I would suggest reading through this block post to get an Idea how QThread is supposed to be used most of the time.
                  https://mayaposch.wordpress.com/2011/11/01/how-to-really-truly-use-qthreads-the-full-explanation/

                  In your code example I would actually go to QtConcurrent.

                  You could mark a function Q_INVOKABLE so you can access it from QML and start a ne QtConCurrent function.
                  For example

                  //.h
                  Q_INVOKABLE void doInParallel();
                  
                  //actual function
                  void myClass:: doInParallel(){
                       QtConcurrent::run([=]()->void{
                            auto funt = new FunctionClass;
                            qDebug()<<"IN multiclass";
                            qDebug()<<"WORKER THREAD ID:"<<QThread::currentThreadId()<<endl;
                            connect(funt,SIGNAL(signalofmainclass()),this,SIGNAL(dicomStoreComplete()));
                            connect(func, &FunctionClass:: signalofmainclass, func, & FunctionClass::deleteLater());//Your previously leaked the function instance, this fixes that.
                            funt->workdone();
                       });
                  }
                  
                  

                  this is untested of course, just food for thought.


                  Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                  Q: What's that?
                  A: It's blue light.
                  Q: What does it do?
                  A: It turns blue.

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    ashajg
                    wrote on 21 Nov 2018, 07:02 last edited by
                    #9

                    ok
                    I ll try this code in my multiclass file

                    1 Reply Last reply
                    0
                    • A Offline
                      A Offline
                      ashajg
                      wrote on 22 Nov 2018, 05:01 last edited by
                      #10

                      Hi @J-Hilk

                      Just want to ask that in order to call run we use start so in my program I am using db.start in my QML.
                      For QtConcurrent how we can start run() through qml?

                      J 1 Reply Last reply 22 Nov 2018, 06:57
                      0
                      • A ashajg
                        22 Nov 2018, 05:01

                        Hi @J-Hilk

                        Just want to ask that in order to call run we use start so in my program I am using db.start in my QML.
                        For QtConcurrent how we can start run() through qml?

                        J Offline
                        J Offline
                        J.Hilk
                        Moderators
                        wrote on 22 Nov 2018, 06:57 last edited by
                        #11

                        @ashajg

                        here a working example

                        //multiclass
                        
                        #ifndef MULTICLASS_H
                        #define MULTICLASS_H
                        
                        #include <QObject>
                        
                        class Multiclass : public QObject
                        {
                            Q_OBJECT
                        public:
                            explicit Multiclass(QObject *parent = nullptr);
                        
                            Q_INVOKABLE void doInParallel();
                        
                        signals:
                            void working(qint64 id, int index);
                            void workDone();
                        
                        };
                        
                        #endif // MULTICLASS_H
                        
                        
                        //cpp
                        #include "multiclass.h"
                        
                        #include <QtConcurrent>
                        
                        #include <QDebug>
                        
                        Multiclass::Multiclass(QObject *parent) : QObject(parent)
                        {
                            connect(this, &Multiclass::working, this, [=](qint64 id, int index){
                                qDebug() << "Thread:" << id << "index" << index;
                            });
                        }
                        
                        void Multiclass::doInParallel()
                        {
                            QtConcurrent::run([=]()->void{
                                      qDebug()<<"doParallel";
                                      qDebug()<<"WORKER THREAD ID:"<< QThread::currentThreadId()<<endl;
                        
                                      for(int i(0); i < 0xFFFF; i++){
                                          if(i % 1000 == 0)
                                              emit working(reinterpret_cast<qint64>(QThread::currentThread()), i);
                                          qDebug() << sqrt(i);
                                      }
                                      emit workDone();
                                 });
                        }
                        
                        //main.cpp
                        #include <QGuiApplication>
                        #include <QQmlApplicationEngine>
                        #include <QQmlContext>
                        
                        
                        #include "multiclass.h"
                        
                        int main(int argc, char *argv[])
                        {
                            QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
                        
                            QGuiApplication app(argc, argv);
                        
                            Multiclass *backEnd = new Multiclass;
                        
                            QQmlApplicationEngine engine;
                            QQmlContext *context =engine.rootContext();
                            context->setContextProperty("BackEnd",backEnd);
                        
                            engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
                        
                        
                        
                            if (engine.rootObjects().isEmpty())
                                return -1;
                        
                            return app.exec();
                        }
                        
                        //main.qml
                        import QtQuick 2.6
                        import QtQuick.Window 2.2
                        
                        Window {
                            visible: true
                            width: 640
                            height: 480
                            title: qsTr("Hello World")
                        
                            Rectangle
                            {
                                id:xyz
                                anchors.fill:parent
                                color:"RED"
                                MouseArea
                                {
                                    anchors.fill:parent
                                    onClicked:{
                                        BackEnd.doInParallel();
                                        xyz.color ="red";
                                    }
                        
                                }
                            }
                        
                            Connections
                            {
                                target:BackEnd
                                onWorking: console.log(id, index);
                                onWorkDone: {
                                    xyz.color ="green";
                                    console.log("Work done");
                                }
                            }
                        }
                        

                        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                        Q: What's that?
                        A: It's blue light.
                        Q: What does it do?
                        A: It turns blue.

                        1 Reply Last reply
                        1
                        • A Offline
                          A Offline
                          ashajg
                          wrote on 22 Nov 2018, 09:58 last edited by
                          #12

                          Thank you sir @J-Hilk

                          1 Reply Last reply
                          0

                          1/12

                          21 Nov 2018, 05:47

                          • Login

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