QScreenCapture Class

该类用于捕捉屏幕。更多

Header: #include <QScreenCapture>
CMake: find_package(Qt6 REQUIRED COMPONENTS Multimedia)
target_link_libraries(mytarget PRIVATE Qt6::Multimedia)
qmake: QT += multimedia
Qt 6.5
在 QML 中: ScreenCapture
继承: QObject

公共类型

enum Error { NoError, InternalError, CapturingNotSupported, CaptureFailed, NotFound }

属性

公共功能

QMediaCaptureSession *captureSession() const
QScreenCapture::Error error() const
QString errorString() const
bool isActive() const
QScreen *screen() const
void setScreen(QScreen *screen)

公共插槽

void setActive(bool active)
void start()
void stop()

信号

void activeChanged(bool)
void errorChanged()
void errorOccurred(QScreenCapture::Error error, const QString &errorString)
void screenChanged(QScreen *)

详细说明

该类用于捕捉屏幕。它由QMediaCaptureSession 类管理,捕获的屏幕可显示在视频预览对象中或录制到文件中。

QMediaCaptureSession session;
QAudioInput audioInput;
session.setAudioInput(&input);
QMediaRecorder recorder;
session.setRecorder(&recorder);
recorder.setQuality(QMediaRecorder::HighQuality);
recorder.setOutputLocation(QUrl::fromLocalFile("test.mp3"));
recorder.record();

屏幕捕捉限制

在 Qt 6.5.2 及以上版本中,使用 QScreenCapture 有以下限制:

  • 仅支持 FFmpeg 后端。
  • 在使用 Wayland 合成器的 Linux 系统上,屏幕捕捉实现是试验性的,有以下限制。由于 Wayland 协议的限制,无法通过QScreenCapture 类的 API 设置和获取目标屏幕。相反,操作系统会在调用QScreenCapture::setActive(true) 时显示屏幕选择向导。屏幕捕捉功能需要安装通过XDG Desktop Portal和 {https://pipewire.org/}{PipeWire} (0.3) 支持的ScreenCast服务。这些限制将来可能会改变。
  • 移动操作系统不支持该功能,安卓系统除外。安卓系统上的屏幕捕捉需要在AndroidManifest.xml 文件中添加额外的安卓前台服务权限
    <manifest ...>
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PROJECTION" />
    <application ...>
        <service android:name="org.qtproject.qt.android.multimedia.QtScreenCaptureService"
            android:foregroundServiceType="mediaProjection"
            android:exported="false"/>
        </service>
    </application>
    </manifest>
  • 在带有 EGLFS 的嵌入式系统上,它的功能有限。对于Qt Quick 应用程序,该类目前是通过QQuickWindow::grabWindow 实现的,这可能会导致性能问题。
  • 在大多数情况下,我们设置的屏幕捕获帧频等于屏幕刷新率,但 Windows 系统除外,因为 Windows 系统的刷新率可能比较灵活。如果捕获的屏幕分辨率为 4K,这样的帧频(75/120 FPS)可能会对性能较弱的 CPU 造成性能问题。在 EGLFS 上,捕捉帧频目前锁定为 30 FPS。

另请参阅 QWindowCaptureQMediaCaptureSession

成员类型文档

enum QScreenCapture::Error

枚举了可由QScreenCapture 类发出信号的错误代码。errorString() 提供有关错误原因的详细信息。

常量说明
QScreenCapture::NoError0无错误
QScreenCapture::InternalError1屏幕捕捉驱动程序内部错误
QScreenCapture::CapturingNotSupported2不支持捕捉
QScreenCapture::CaptureFailed4捕捉屏幕失败
QScreenCapture::NotFound5未找到所选屏幕

属性文档

active : bool

该属性显示捕捉当前是否处于活动状态。

访问功能:

bool isActive() const
void setActive(bool active)

通知信号:

void activeChanged(bool)

[read-only] error : const Error

该属性保存上次错误的代码。

访问功能:

QScreenCapture::Error error() const

通知信号:

void errorChanged()

[read-only] errorString : const QString

该属性包含描述错误原因的可读字符串。

访问功能:

QString errorString() const

Notifier 信号:

void errorChanged()

screen : QScreen*

该属性可保存用于捕捉的屏幕。

访问功能:

QScreen *screen() const
void setScreen(QScreen *screen)

Notifier 信号:

void screenChanged(QScreen *)

成员函数 文档

QMediaCaptureSession *QScreenCapture::captureSession() const

返回此QScreenCapture 所连接的捕获会话。

使用QMediaCaptureSession::setScreenCapture() 将摄像机连接到会话。

[signal] void QScreenCapture::errorOccurred(QScreenCapture::Error error, const QString &errorString)

error 时发出信号,同时发出errorString

[slot] void QScreenCapture::start()

开始屏幕截图。

[slot] void QScreenCapture::stop()

停止屏幕截图。

© 2025 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.