gpt4 book ai didi

c++ - 从 QThread 使用 QCamera/QCameraInfo

转载 作者:太空宇宙 更新时间:2023-11-04 12:50:19 25 4
gpt4 key购买 nike

我有一个 Camera 类,目前只返回连接到计算机的可用相机数量。在我的 PhotoProcessor 类 (QWidget) 中创建了一个 Camera 对象,然后将其移至另一个线程,因为我在检查可用相机时注意到一些轻微的挂起:

在 photoprocessor.cpp 中

void PhotoProcessor::GetAvailableCameras()
{
//Create new camera to go and fetch number of cameras.
Camera *camera_connection = new Camera;

//Create thread and send away.
QThread *thread = new QThread;
camera_connection->moveToThread(thread);
connect(camera_connection, SIGNAL(CameraCountSignal(int)), this, SLOT(CameraCountSlot(int)));
connect(thread, SIGNAL(started()), camera_connection, SLOT(GetAvailableCameras()));
connect(camera_connection, SIGNAL(CloseCameraThreadSignal()), thread, SLOT(quit()));
connect(camera_connection, SIGNAL(CloseCameraThreadSignal()), camera_connection, SLOT(deleteLater()));
connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
thread->start();

return;
}

相机.h

#ifndef CAMERA_H
#define CAMERA_H

#include <QObject>
#include <QCamera>
#include <QCameraInfo>

class Camera : public QObject
{
Q_OBJECT

public:
Camera();

public slots:
void GetAvailableCameras();

signals:
void CameraCountSignal(int);
void CloseCameraThreadSignal();
};

#endif // CAMERA_H

相机.cpp

#include "camera.h"

Camera::Camera()
{

}

void Camera::GetAvailableCameras()
{
QList<QCameraInfo> cameras = QCameraInfo::availableCameras();
emit CameraCountSignal(cameras.size());
emit CloseCameraThreadSignal();
return;
}

当从 photoprocessor.cpp 运行 QCameraInfo::availableCameras() 时,它说有一个连接的相机是正确的,但是无论我做什么,线程相机对象永远无法检测到相机,即相机的大小为零.

这里发生了什么?

最佳答案

时隔一年,我决定重新审视这个问题。

我认为在我的实际代码中我创建了一个 QCamera 对象,当线程被删除时,我删除不正确,并且在线程完成后,我无法再使用 QCameraInfo::availableCameras() 找到任何相机。

设置新 QCamera 对象的父对象为我明确解决了这个问题,即

QCamera *camera = new QCamera(QCameraInfo);
camera->setParent(this);

关于c++ - 从 QThread 使用 QCamera/QCameraInfo,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49379214/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com