You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何停用QVideoProbe?调用setSource(0)触发类型错误

问题解决:QVideoProbe.setSource(0)触发TypeError的问题

问题原因

Qt文档里提到的“source为零”是C++语境下的空指针(nullptr),但在PySide2中,对应的Python值是None,而非整数0。直接传入整数0会因为类型不匹配触发TypeError——setSource仅接受QMediaObject、QMediaRecorder类型参数,或用None表示空源以停用探测器。

解决方案

将代码中的self.probe.setSource(0)替换为self.probe.setSource(None),即可正确停用探测器。

修改后的示例代码

import sys
from PySide2 import QtCore, QtMultimedia
from PySide2.QtMultimedia import *
from PySide2.QtMultimediaWidgets import *
from PySide2.QtWidgets import *

class MainWindow(QMainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)
        
        self.available_cameras = QCameraInfo.availableCameras()
        self.camera = QCamera(self.available_cameras[0])
        self.probe = QtMultimedia.QVideoProbe(self)
        self.probe.videoFrameProbed.connect(self.processFrame)
        self.probe.setSource(self.camera)
        # 用None替代整数0,停用探测器
        self.probe.setSource(None)
        
    def processFrame(self, frame):
        pass

if __name__ == "__main__":
    app = QApplication(sys.argv)
    mainWindow = MainWindow()
    mainWindow.show()
    sys.exit(app.exec_())

内容的提问来源于stack exchange,提问作者ramiwi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 10:11:33