如何停用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
相关产品推荐
相关产品推荐

