本页内容

WindowCapture QML Type

此类型用于捕获窗口。更多...

Import Statement: import QtMultimedia
Since: Qt 6.6
In C++: QWindowCapture

属性

信号

方法

详细说明

WindowCapture 用于捕获窗口。它由CaptureSession 管理,捕获的窗口可在视频预览对象中显示,或录制到文件中。

下面的代码展示了一个简单的捕获会话,该会话使用 WindowCapture 捕获一个可用窗口,并在VideoOutput 中进行回放。

CaptureSession {
    id: captureSession
    windowCapture: WindowCapture {
        id: capture
    }
    videoOutput: VideoOutput {
        id: videoOutput
    }

    Component.onCompleted: {
        let windows = capture.capturableWindows()
        if (windows.length > 0) {
            capture.window = windows[0]
            capture.active = true
        }
    }
}

WindowCapture 的限制

使用 WindowCapture 时受以下限制:

  • WindowCapture 仅在 FFmpeg 后端下受支持。
  • 在某些平台上,当捕获的窗口内容保持不变时,不会输出新的视频帧。因此,应用程序不应依赖于以请求的帧率持续接收帧流。

在 X11 系统上使用 `QWindowCapture ` 时,存在以下限制:

  • 在 Linux X11 系统中,当窗口部分移出可见屏幕区域时,仅会捕获可见区域。因此,输出的视频帧尺寸可能小于窗口的几何尺寸。
  • 位于可见屏幕区域之外的窗口无法被捕获,此时会发出错误信号。
  • 最小化窗口或位于不可见虚拟工作区中的窗口的行为取决于窗口管理器。例如,在 GNOME 中可以捕获此类窗口,而在 WindowMaker 或 Xfwm 中则不允许此类捕获,且窗口捕获实例会发出错误。

另请参阅 CaptureSession 和CapturableWindow 。

属性文档

active : bool

描述捕获功能当前是否处于活动状态。

另请参阅 start() 和stop()。

error : enumeration [read-only]

返回上一次错误的代码。

常量描述
WindowCapture.NoError无错误
WindowCapture.InternalError内部窗口捕获驱动程序错误
WindowCapture.CapturingNotSupported不支持窗口捕获
WindowCapture.CaptureFailed捕获窗口失败
WindowCapture.NotFound未找到所选窗口

errorString : string [read-only]

返回一个描述错误原因的人类可读字符串。

maximumFrameRate : real [since 6.12]

窗口捕获帧率的上限。

此参数可设置为覆盖默认的捕获帧率(该帧率通常基于显示器刷新率等参数),但仅作为上限,因为窗口捕获产生的帧率是可变的。不建议将此值设置为高于显示器刷新率,否则可能会导致错误。

若设置为 -1,则使用取决于平台的默认值。

对该属性的任何更改将在WindowCapture 下次激活时生效。

该属性在 Qt 6.12 中引入。

window : Window

描述用于捕获的窗口。

若将此属性设置为处于活动状态的WindowCapture 上的无效窗口,将导致该 变为非活动状态并触发错误。

另请参阅 capturableWindows 。

Signal 文档

errorChanged()

当error 或errorString 属性发生变化时,会触发此信号。

当抛出多个相同的错误时,不会发出此信号。要跟踪此类错误,请使用信号errorOccurred 。

注意: 对应的处理程序 为onErrorChanged 。

errorOccurred(int error, string errorString)

当发生error 时触发该信号,同时触发errorString 。

关于 error 参数,请参阅error 中的枚举表以了解可传递的值。

注意: 相应的处理程序 是onErrorOccurred 。

另请参阅 error 。

方法文档

list<CapturableWindow> capturableWindows()

返回一个包含当前可用于捕获的CapturableWindow 对象的列表。

注意:在 macOS上 ,调用此方法会触发“屏幕录制”权限对话框。如果尚未授予权限,此方法将返回一个空列表。多次调用该方法会将此对话框置于前台。

void start()

开始捕获window 。

这相当于将active 属性设置为true 。

void stop()

停止捕获。

这相当于将active 属性设置为false 。

© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.