Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qscreencapture.cpp
Go to the documentation of this file.
1// Copyright (C) 2022 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
5
6#include <QtMultimedia/qmediacapturesession.h>
7#include <QtMultimedia/private/qplatformmediaintegration_p.h>
8#include <QtMultimedia/private/qplatformsurfacecapture_p.h>
9#include <QtMultimedia/private/qscreencapture_p.h>
10
12
13static QScreenCapture::Error toScreenCaptureError(QPlatformSurfaceCapture::Error error)
14{
15 return static_cast<QScreenCapture::Error>(error);
16}
17
18/*!
19 \class QScreenCapture
20 \inmodule QtMultimedia
21 \ingroup multimedia
22 \ingroup multimedia_video
23 \since 6.5
24
25 \brief This class is used for capturing a screen.
26
27 The class captures a screen. It is managed by
28 the \l QMediaCaptureSession class where the captured screen can be displayed
29 in a video preview object or recorded to a file.
30
31 The following snippet shows how to capture the primary screen and display
32 the result in a QVideoWidget:
33
34 \snippet multimedia-snippets/screencapturesnippets.cpp Basic setup
35
36 \include qscreencapture-limitations.qdocinc {content} {Q}
37
38 \sa QWindowCapture, QMediaCaptureSession
39*/
40/*!
41 \qmltype ScreenCapture
42 \nativetype QScreenCapture
43 \brief This type is used for capturing a screen.
44
45 \inqmlmodule QtMultimedia
46 \ingroup multimedia_qml
47 \ingroup multimedia_video_qml
48 \since 6.5
49
50 ScreenCapture captures a screen. It is managed by
51 \l{QtMultimedia::CaptureSession}{CaptureSession} where the captured
52 screen can be displayed in a video preview object or recorded to a
53 file.
54
55 The code below shows a simple capture session with ScreenCapture playing
56 back the captured primary screen view in \l {QtMultimedia::VideoOutput}{VideoOutput}.
57
58 \snippet multimedia-snippets/screencapturesnippets.qml Basic setup
59
60 \include qscreencapture-limitations.qdocinc {content} {}
61
62 \sa WindowCapture, CaptureSession
63*/
64
65QScreenCapture::QScreenCapture(QObject *parent)
66 : QObject(*new QScreenCapturePrivate, parent)
67{
68 Q_D(QScreenCapture);
69
70 auto platformCapture = QPlatformMediaIntegration::instance()->createScreenCapture(this);
71 if (platformCapture) {
72 connect(platformCapture, &QPlatformSurfaceCapture::activeChanged, this,
73 &QScreenCapture::activeChanged);
74 connect(platformCapture, &QPlatformSurfaceCapture::errorChanged, this,
75 &QScreenCapture::errorChanged);
76 connect(platformCapture, &QPlatformSurfaceCapture::errorOccurred, this,
77 [this](QPlatformSurfaceCapture::Error error, const QString &errorString) {
78 emit errorOccurred(toScreenCaptureError(error), errorString);
79 });
80
81 connect(platformCapture,
82 qOverload<QPlatformSurfaceCapture::ScreenSource>(
83 &QPlatformSurfaceCapture::sourceChanged),
84 this, &QScreenCapture::screenChanged);
85
86 connect(platformCapture, &QPlatformSurfaceCapture::frameRateChanged, this,
87 &QScreenCapture::maximumFrameRateChanged);
88
89 d->platformScreenCapture.reset(platformCapture);
90 }
91}
92
93QScreenCapture::~QScreenCapture()
94{
95 Q_D(QScreenCapture);
96
97 // Reset platformScreenCapture in the destructor to avoid having broken ref in the object.
98 d->platformScreenCapture.reset();
99
100 if (d->captureSession)
101 d->captureSession->setScreenCapture(nullptr);
102}
103
104/*!
105 \enum QScreenCapture::Error
106
107 Enumerates error codes that can be signaled by the QScreenCapture class.
108 errorString() provides detailed information about the error cause.
109
110 \value NoError No error
111 \value InternalError Internal screen capturing driver error
112 \value CapturingNotSupported Capturing is not supported
113 \value CaptureFailed Capturing screen failed
114 \value NotFound Selected screen not found
115*/
116
117/*!
118 Returns the capture session this QScreenCapture is connected to.
119
120 Use QMediaCaptureSession::setScreenCapture() to connect the screen capture
121 to a session.
122*/
123QMediaCaptureSession *QScreenCapture::captureSession() const
124{
125 Q_D(const QScreenCapture);
126
127 return d->captureSession;
128}
129
130/*!
131 \qmlproperty bool QtMultimedia::ScreenCapture::active
132 Describes whether the capturing is currently active.
133
134 \sa start(), stop()
135*/
136
137/*!
138 \property QScreenCapture::active
139 \brief whether the capturing is currently active.
140
141 \sa start(), stop()
142*/
143void QScreenCapture::setActive(bool active)
144{
145 Q_D(QScreenCapture);
146
147 if (d->platformScreenCapture)
148 d->platformScreenCapture->setActive(active);
149}
150
151bool QScreenCapture::isActive() const
152{
153 Q_D(const QScreenCapture);
154
155 return d->platformScreenCapture && d->platformScreenCapture->isActive();
156}
157
158/*!
159 \qmlproperty Screen QtMultimedia::ScreenCapture::screen
160 Describes the screen for capturing.
161
162 If null \c Screen is set, the primary screen will be selected
163 when the \c ScreenCapture instance gets activated.
164
165 \sa {QtQuick::Application::screens}{Application.screens}
166*/
167
168/*!
169 \property QScreenCapture::screen
170 \brief the screen for capturing.
171
172 If null \l QScreen is set, \l QGuiApplication::primaryScreen will be selected
173 when the \c QScreenCapture instance gets activated.
174
175 \sa QGuiApplication::screens(), QGuiApplication::primaryScreen()
176*/
177
178void QScreenCapture::setScreen(QScreen *screen)
179{
180 Q_D(QScreenCapture);
181
182 if (d->platformScreenCapture)
183 d->platformScreenCapture->setSource(QPlatformSurfaceCapture::ScreenSource(screen));
184}
185
186QScreen *QScreenCapture::screen() const
187{
188 Q_D(const QScreenCapture);
189
190 return d->platformScreenCapture
191 ? d->platformScreenCapture->source<QPlatformSurfaceCapture::ScreenSource>()
192 : nullptr;
193}
194
195/*!
196 \qmlsignal QtMultimedia::ScreenCapture::errorChanged()
197
198 This signal is emitted when the \l{error} or \l{errorString} properties are changed.
199
200 This signal is not emitted whenever multiple identical errors are raised. To track such
201 errors, use the signal \l errorOccurred.
202*/
203
204/*!
205 \fn void QScreenCapture::errorChanged()
206
207 This signal is emitted when the \l{error} or \l{errorString} properties are changed.
208
209 This signal is not emitted whenever multiple identical errors are raised. To track such
210 errors, use the signal \l errorOccurred.
211*/
212
213/*!
214 \qmlproperty enumeration QtMultimedia::ScreenCapture::error
215 Returns a code of the last error.
216
217 \qmlenumeratorsfrom QScreenCapture::Error
218*/
219
220/*!
221 \property QScreenCapture::error
222 \brief the code of the last error.
223*/
224QScreenCapture::Error QScreenCapture::error() const
225{
226 Q_D(const QScreenCapture);
227
228 return d->platformScreenCapture ? toScreenCaptureError(d->platformScreenCapture->error())
229 : CapturingNotSupported;
230}
231
232/*!
233 \qmlsignal QtMultimedia::ScreenCapture::errorOccurred(int error, string errorString)
234
235 Signals when an \a error occurs, along with the \a errorString.
236
237 For the error parameter, see the enumeration table in
238 \l {QtMultimedia::ScreenCapture::error}{error} for what values may
239 be passed.
240
241 \sa {QtMultimedia::ScreenCapture::error}{error}
242*/
243/*!
244 \fn void QScreenCapture::errorOccurred(QScreenCapture::Error error, const QString &errorString)
245
246 Signals when an \a error occurs, along with the \a errorString.
247*/
248/*!
249 \qmlproperty string QtMultimedia::ScreenCapture::errorString
250 Returns a human readable string describing the cause of error.
251*/
252
253/*!
254 \property QScreenCapture::errorString
255 \brief a human readable string describing the cause of error.
256*/
257QString QScreenCapture::errorString() const
258{
259 Q_D(const QScreenCapture);
260
261 return d->platformScreenCapture ? d->platformScreenCapture->errorString()
262 : QLatin1StringView("Capturing is not supported on this platform");
263}
264
265/*!
266 \since 6.12
267 \property QScreenCapture::maximumFrameRate
268 \brief The screen capture frame rate upper limit.
269
270 This can be set to override the capture frame rate used by default based on
271 e.g. display refresh rate, but only as an upper limit since screen capture
272 produces frames at a variable rate. Setting this higher than the display
273 refresh rate is not recommended and can cause errors.
274
275 Any changes to this property are applied the next time the QScreenCapture
276 goes active.
277*/
278void QScreenCapture::setMaximumFrameRate(std::optional<qreal> frameRate)
279{
280 Q_D(QScreenCapture);
281
282 if (d->platformScreenCapture)
283 d->platformScreenCapture->setFrameRate(frameRate);
284}
285
286std::optional<qreal> QScreenCapture::maximumFrameRate() const
287{
288 Q_D(const QScreenCapture);
289
290 return d->platformScreenCapture ? d->platformScreenCapture->frameRate() : std::nullopt;
291}
292
293/*!
294 \qmlmethod void QtMultimedia::ScreenCapture::start()
295
296 Starts capturing the \l screen.
297
298 This is equivalent to setting the \l active property to \c true.
299*/
300
301/*!
302 \fn void QScreenCapture::start()
303
304 Starts capturing the \l screen.
305
306 This is equivalent to setting the \l active property to true.
307*/
308
309/*!
310 \qmlmethod void QtMultimedia::ScreenCapture::stop()
311
312 Stops capturing.
313
314 This is equivalent to setting the \l active property to \c false.
315*/
316
317/*!
318 \fn void QScreenCapture::stop()
319
320 Stops capturing.
321
322 This is equivalent to setting the \l active property to false.
323*/
324/*!
325 \internal
326*/
327void QScreenCapture::setCaptureSession(QMediaCaptureSession *captureSession)
328{
329 Q_D(QScreenCapture);
330
331 d->captureSession = captureSession;
332}
333
334/*!
335 \internal
336*/
337class QPlatformSurfaceCapture *QScreenCapture::platformScreenCapture() const
338{
339 Q_D(const QScreenCapture);
340
341 return d->platformScreenCapture.get();
342}
343
344void QScreenCapturePrivate::setIgnoreCursor(bool ignore)
345{
346 if (!platformScreenCapture)
347 return;
348 platformScreenCapture->setIgnoreCursor(ignore);
349}
350
351QT_END_NAMESPACE
352
353#include "moc_qscreencapture.cpp"
Combined button and popup list for selecting options.
static QT_BEGIN_NAMESPACE QScreenCapture::Error toScreenCaptureError(QPlatformSurfaceCapture::Error error)