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. QCameraImageCapture moveToThread error
Qt 6.11 is out! See what's new in the release blog

QCameraImageCapture moveToThread error

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 2.5k 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.
  • N Offline
    N Offline
    nefarious
    wrote on last edited by
    #1

    I am sort of new to Qt and am trying to sort out how to work with the QCamera and QCameraImageCapture technology.

    I have the QCamera and QCameraImageCapture in a thread with no ui. The invocation of the thread is:

    QCameraImageCapture theImageCapture = new ImageCapture();
    QThread* theImageThread = new QThread();
    theImageCapture->moveToThread(theImageThread);
    
    connect(theImageThread, SIGNAL(started()), theImageCapture, SLOT(process()));
    connect(theImageCapture, SIGNAL(finished()), theImageThread, SLOT(quit()));
    
    connect(theImageThread, SIGNAL(finished()), theImageThread, SLOT(deleteLater()));
    theImageThread->start();
    

    The imageCapture instance instantiates a QCamera and a QCameraImageCapture. All of this code is taken from the camera demo.

    the captureImage process method is:

    void ImageCapture::process()
    {
        inProcess = true;
        isRunning = true;
    
        while (inProcess)
        {
            sync.lock();
            inProcess = isRunning;
            sync.unlock();
    
            if (isReadyForCapture){
                toggleLock();
                takeImage();
                toggleLock();
            }
        }
        emit finished();
    }
    

    The image is sent to a widget for processing with:

    void ImageCapture::processCapturedImage(int requestId, const QImage& img)
    {
        Q_UNUSED(requestId);
        QImage scaledImage = img.scaled(QSize(400, 300), //ui->viewfinder->size(),
                                        Qt::KeepAspectRatio,
                                        Qt::SmoothTransformation);
    
        thePixmap = QPixmap::fromImage(scaledImage);
        emit ProcessPixmap(thePixmap);
    }
    

    The process works for a few images before I get the message

    QObject::moveToThread: Current thread (0x2775377aa10) is not the object's thread (0x2775377c540).
    Cannot move to target thread (0x277536ad1f0)

    And after the message the images continue to be sent to the Widget.

    If I comment out the takeImage() line in the thread process line the message goes away. I do not have a view finder connected to the camera.

    What did I do wrong that causes the message?

    Is there an alternative approach to the situation. What I want to do is take an image periodically, get the QPixmap of the image so I can draw on it, and then write it to a Widget.

    kshegunovK 1 Reply Last reply
    0
    • N nefarious

      I am sort of new to Qt and am trying to sort out how to work with the QCamera and QCameraImageCapture technology.

      I have the QCamera and QCameraImageCapture in a thread with no ui. The invocation of the thread is:

      QCameraImageCapture theImageCapture = new ImageCapture();
      QThread* theImageThread = new QThread();
      theImageCapture->moveToThread(theImageThread);
      
      connect(theImageThread, SIGNAL(started()), theImageCapture, SLOT(process()));
      connect(theImageCapture, SIGNAL(finished()), theImageThread, SLOT(quit()));
      
      connect(theImageThread, SIGNAL(finished()), theImageThread, SLOT(deleteLater()));
      theImageThread->start();
      

      The imageCapture instance instantiates a QCamera and a QCameraImageCapture. All of this code is taken from the camera demo.

      the captureImage process method is:

      void ImageCapture::process()
      {
          inProcess = true;
          isRunning = true;
      
          while (inProcess)
          {
              sync.lock();
              inProcess = isRunning;
              sync.unlock();
      
              if (isReadyForCapture){
                  toggleLock();
                  takeImage();
                  toggleLock();
              }
          }
          emit finished();
      }
      

      The image is sent to a widget for processing with:

      void ImageCapture::processCapturedImage(int requestId, const QImage& img)
      {
          Q_UNUSED(requestId);
          QImage scaledImage = img.scaled(QSize(400, 300), //ui->viewfinder->size(),
                                          Qt::KeepAspectRatio,
                                          Qt::SmoothTransformation);
      
          thePixmap = QPixmap::fromImage(scaledImage);
          emit ProcessPixmap(thePixmap);
      }
      

      The process works for a few images before I get the message

      QObject::moveToThread: Current thread (0x2775377aa10) is not the object's thread (0x2775377c540).
      Cannot move to target thread (0x277536ad1f0)

      And after the message the images continue to be sent to the Widget.

      If I comment out the takeImage() line in the thread process line the message goes away. I do not have a view finder connected to the camera.

      What did I do wrong that causes the message?

      Is there an alternative approach to the situation. What I want to do is take an image periodically, get the QPixmap of the image so I can draw on it, and then write it to a Widget.

      kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on last edited by kshegunov
      #2

      @nefarious said in QCameraImageCapture moveToThread error:

      QObject::moveToThread: Current thread (0x2775377aa10) is not the object's thread (0x2775377c540).
      Cannot move to target thread (0x277536ad1f0)

      It appears you're trying to "pull" an object from a thread, so that's why Qt's complaining. QObject::moveToThread is NOT thread safe and can be used only from the thread the object currently resides to "push" into another.

      On a related note you shouldn't use pixmaps from worker threads QPixmap::fromImage (assuming processCapturedImage is run in the worker). The QPixmap class isn't reentrant.

      Kind regards.

      Read and abide by the Qt Code of Conduct

      1 Reply Last reply
      1
      • N Offline
        N Offline
        nefarious
        wrote on last edited by
        #3

        It is interesting. I changed the implementation to use a timer in the widget so I have no QThreads of my own and I still get the message. Is there a way to catch the message, I get the error when I run from the ide, but when I run as a real application I can't know that something is wrong, and handle the situation or write a status.

        If even anything is wrong.

        I can show all of the capture error messages and the camera error messages and none are shown.

        Like I said, I am new the qt and trying to figure this out. The message quotes a thread address, it looks like an address, is there a way to find what thread is causing the message.

        kshegunovK 1 Reply Last reply
        0
        • N nefarious

          It is interesting. I changed the implementation to use a timer in the widget so I have no QThreads of my own and I still get the message. Is there a way to catch the message, I get the error when I run from the ide, but when I run as a real application I can't know that something is wrong, and handle the situation or write a status.

          If even anything is wrong.

          I can show all of the capture error messages and the camera error messages and none are shown.

          Like I said, I am new the qt and trying to figure this out. The message quotes a thread address, it looks like an address, is there a way to find what thread is causing the message.

          kshegunovK Offline
          kshegunovK Offline
          kshegunov
          Moderators
          wrote on last edited by
          #4

          The "easy" way is to put a breakpoint in Qt's source where the message is emitted and just walk through the stack to see what call causes it.

          Read and abide by the Qt Code of Conduct

          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