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. Camera class in QThread

Camera class in QThread

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 4 Posters 1.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.
  • S Offline
    S Offline
    stryga42
    wrote on last edited by stryga42
    #2

    I think the problem is that you call run directly from your GUI thread - this is a simple (blocking) call, not the start of a new thread. You should call start() on your camera object, I guess.
    Edit: Your have two QThread objects - cameraThread and camera (which is derived from QThread) why? You mix the two options to use QThread (moving a worker object to QThread or reimplementing QThread::run(). )

    C 1 Reply Last reply
    1
    • S stryga42

      I think the problem is that you call run directly from your GUI thread - this is a simple (blocking) call, not the start of a new thread. You should call start() on your camera object, I guess.
      Edit: Your have two QThread objects - cameraThread and camera (which is derived from QThread) why? You mix the two options to use QThread (moving a worker object to QThread or reimplementing QThread::run(). )

      C Offline
      C Offline
      Creatorczyk
      wrote on last edited by
      #3

      @stryga42 If I change the inheritance from QThread to QObject, nothing will change

      Christian EhrlicherC 1 Reply Last reply
      0
      • C Creatorczyk

        @stryga42 If I change the inheritance from QThread to QObject, nothing will change

        Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #4

        @Creatorczyk Since you don't start the thread anywhere nothing happens.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        C 1 Reply Last reply
        1
        • Christian EhrlicherC Christian Ehrlicher

          @Creatorczyk Since you don't start the thread anywhere nothing happens.

          C Offline
          C Offline
          Creatorczyk
          wrote on last edited by
          #5

          @Christian-Ehrlicher I change code in main.cpp to:

          QThread cameraThread;
              Camera *camera = new Camera();
              QObject::connect(camera, &Camera::FrameReady, mqttClient, &MqttClient::sendFrame);
              camera->moveToThread(&cameraThread);
              cameraThread.start();
              camera->run();
              qDebug()<<"Started";   //Will not be done
              camera->stop();   //Will not be done
              qDebug()<<"Stopped";   //Will not be done
          

          Unfortunately nothing happened. Futher code does not execute

          Christian EhrlicherC S 2 Replies Last reply
          0
          • C Creatorczyk

            @Christian-Ehrlicher I change code in main.cpp to:

            QThread cameraThread;
                Camera *camera = new Camera();
                QObject::connect(camera, &Camera::FrameReady, mqttClient, &MqttClient::sendFrame);
                camera->moveToThread(&cameraThread);
                cameraThread.start();
                camera->run();
                qDebug()<<"Started";   //Will not be done
                camera->stop();   //Will not be done
                qDebug()<<"Stopped";   //Will not be done
            

            Unfortunately nothing happened. Futher code does not execute

            Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #6

            @Creatorczyk Again: you don't start your thread anywhere, you just call a function named run() in your Camera object - nothing more.

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            C 1 Reply Last reply
            1
            • Christian EhrlicherC Christian Ehrlicher

              @Creatorczyk Again: you don't start your thread anywhere, you just call a function named run() in your Camera object - nothing more.

              C Offline
              C Offline
              Creatorczyk
              wrote on last edited by
              #7

              @Christian-Ehrlicher calling "cameraThread.start();" will not start thread?

              Christian EhrlicherC jsulmJ 2 Replies Last reply
              0
              • C Creatorczyk

                @Christian-Ehrlicher calling "cameraThread.start();" will not start thread?

                Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #8

                @Creatorczyk It will, but then you call a blocking function in your main thread.

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                1 Reply Last reply
                0
                • C Creatorczyk

                  @Christian-Ehrlicher I change code in main.cpp to:

                  QThread cameraThread;
                      Camera *camera = new Camera();
                      QObject::connect(camera, &Camera::FrameReady, mqttClient, &MqttClient::sendFrame);
                      camera->moveToThread(&cameraThread);
                      cameraThread.start();
                      camera->run();
                      qDebug()<<"Started";   //Will not be done
                      camera->stop();   //Will not be done
                      qDebug()<<"Stopped";   //Will not be done
                  

                  Unfortunately nothing happened. Futher code does not execute

                  S Offline
                  S Offline
                  stryga42
                  wrote on last edited by
                  #9

                  @Creatorczyk

                  cameraThread.start();
                  

                  is the right thing to do. Yes, this starts a new thread "within" the object cameraThread. This call to start() does not block. Check it it out by inserting a qDebug() output right after the call.
                  But then you call (unnecessarily) Camera::run(), which will block. Use signals to "trigger" your camera object. Again, I think you mix up the two ways to use QThread: Usually you EITHER use the "moveToTrhread-style" OR the "reimplement QThread::run-style", but not both at the same time. If you want your code in Camera::run() to be executed without signaling, you have to call start() on your (then QThread derived) Camera object. Som thing like

                  camera->start();
                  

                  should do the job. cameraThread is then not required at all.

                  1 Reply Last reply
                  1
                  • C Creatorczyk

                    @Christian-Ehrlicher calling "cameraThread.start();" will not start thread?

                    jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by
                    #10

                    @Creatorczyk said in Camera class in QThread:

                    calling "cameraThread.start();" will not start thread?

                    Did you read https://doc.qt.io/qt-5/qthread.html#start ?
                    start() calls run() - you do not have to call run() by yourself and you should NOT!

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    2
                    • C Offline
                      C Offline
                      Creatorczyk
                      wrote on last edited by
                      #11

                      @stryga42 @Christian-Ehrlicher @jsulm Thanks, I fixed it!

                      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