This commit is contained in:
朱子楚\zhuzi 2023-08-17 19:09:56 +08:00
parent 06c9b4e382
commit c0726d89f0
2 changed files with 5 additions and 8 deletions

View File

@ -42,28 +42,24 @@ ScreenshotBackground::ScreenshotBackground(QQuickItem* parent) : QQuickPaintedIt
void ScreenshotBackground::paint(QPainter* painter) void ScreenshotBackground::paint(QPainter* painter)
{ {
painter->save(); painter->save();
QPixmap sourcePixmap; _sourcePixmap = QPixmap(QSize(_desktopGeometry.width(),_desktopGeometry.height()));
QPainter p(&sourcePixmap); QPainter p(&_sourcePixmap);
p.drawPixmap(_desktopGeometry,_desktopPixmap); p.drawPixmap(_desktopGeometry,_desktopPixmap);
painter->drawPixmap(_desktopGeometry,sourcePixmap); painter->drawPixmap(_desktopGeometry,_sourcePixmap);
painter->restore(); painter->restore();
} }
void ScreenshotBackground::capture(const QPoint& start,const QPoint& end){ void ScreenshotBackground::capture(const QPoint& start,const QPoint& end){
qDebug()<<start;
qDebug()<<end;
_grabResult = grabToImage(); _grabResult = grabToImage();
auto x = qMin(start.x(),end.x()); auto x = qMin(start.x(),end.x());
auto y = qMin(start.y(),end.y()); auto y = qMin(start.y(),end.y());
auto w = qAbs(end.x()-start.x()); auto w = qAbs(end.x()-start.x());
auto h = qAbs(end.y()-start.y()); auto h = qAbs(end.y()-start.y());
_captureRect = QRect(x,y,w,h); _captureRect = QRect(x,y,w,h);
qDebug()<<_captureRect;
connect(_grabResult.data(), &QQuickItemGrabResult::ready, this, &ScreenshotBackground::handleGrabResult); connect(_grabResult.data(), &QQuickItemGrabResult::ready, this, &ScreenshotBackground::handleGrabResult);
} }
void ScreenshotBackground::handleGrabResult(){ void ScreenshotBackground::handleGrabResult(){
_grabResult.data()->image().copy(_captureRect).save("aaa.png"); _sourcePixmap.copy(_captureRect).save("aaa.png");
} }

View File

@ -19,6 +19,7 @@ public:
private: private:
QRect _desktopGeometry; QRect _desktopGeometry;
QPixmap _desktopPixmap; QPixmap _desktopPixmap;
QPixmap _sourcePixmap;
qreal _devicePixelRatio; qreal _devicePixelRatio;
QSharedPointer<QQuickItemGrabResult> _grabResult; QSharedPointer<QQuickItemGrabResult> _grabResult;
QRect _captureRect; QRect _captureRect;