@@ -12,8 +12,26 @@ void ItemImageGrab::grab(QQuickItem* target, const QUrl& path) {
12
12
}
13
13
14
14
void ItemImageGrab::grab (QQuickItem* target, const QUrl& path, const QSize& targetSize) {
15
+ this ->cropAndGrab (target, path, QRect (), targetSize);
16
+ }
17
+
18
+ void ItemImageGrab::cropAndGrab (QQuickItem* target, const QUrl& path, const QRect& rect) {
19
+ this ->cropAndGrab (target, path, rect, QSize ());
20
+ }
21
+
22
+ void ItemImageGrab::cropAndGrab (
23
+ QQuickItem* target,
24
+ const QUrl& path,
25
+ const QRect& rect,
26
+ const QSize& targetSize
27
+ ) {
15
28
if (!target) {
16
- qWarning () << " ItemImageGrab::grab: a target is required" ;
29
+ qWarning () << " ItemImageGrab: a target is required" ;
30
+ return ;
31
+ }
32
+
33
+ if (!path.isLocalFile ()) {
34
+ qWarning () << " ItemImageGrab: can only save to a file on the local filesystem" ;
17
35
return ;
18
36
}
19
37
@@ -28,10 +46,18 @@ void ItemImageGrab::grab(QQuickItem* target, const QUrl& path, const QSize& targ
28
46
grabResult.data (),
29
47
&QQuickItemGrabResult::ready,
30
48
this ,
31
- [grabResult, path, this ]() {
32
- QThreadPool::globalInstance ()->start ([grabResult, path, this ] {
33
- if (grabResult->saveToFile (path)) {
34
- emit this ->saved (path);
49
+ [grabResult, rect, path, this ]() {
50
+ QThreadPool::globalInstance ()->start ([grabResult, rect, path, this ] {
51
+ QImage image = grabResult->image ();
52
+
53
+ if (!rect.isEmpty ()) {
54
+ image = image.copy (rect);
55
+ }
56
+
57
+ const QString localFile = path.toLocalFile ();
58
+
59
+ if (image.save (localFile)) {
60
+ emit this ->saved (localFile);
35
61
} else {
36
62
emit this ->failed (path);
37
63
}
0 commit comments