Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QCameraImageCapture moveToThread error



  • 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.


  • Qt Champions 2017

    @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.



  • 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.


  • Qt Champions 2017

    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.


Log in to reply