pixmapimage.h
#ifndef PIXMAPIMAGE_H
#define PIXMAPIMAGE_H
#include <QQuickItem>
#include <QQuickPaintedItem>
#include <QPainter>
#include <QString>
#include "screenshot.h"
class PixmapImage : public QQuickPaintedItem
{
Q_OBJECT
public:
PixmapImage();
Q_INVOKABLE void paint(QPainter *painter) override;
Q_INVOKABLE void getscreen();
Q_INVOKABLE QString getimg();
private:
QScreen *screen;
bool flag=false;
signals:
public slots:
};
#endif // PIXMAPIMAGE_H
pixmapimage.cpp
#include "pixmapimage.h"
#include <QScreen>
#include <QGuiApplication>
#include <QPixmap>
#include <QThread>
#include <QSettings>
#include <QGSettings>
PixmapImage::PixmapImage()
{
}
void PixmapImage::getscreen()
{
screen = QGuiApplication::primaryScreen();
flag = true;
update();
}
void PixmapImage::paint(QPainter *painter)
{
painter->setRenderHint(QPainter::Antialiasing,true);
if(flag) {
painter->drawPixmap(0, 0, width(), height(),screen->grabWindow(0));
}
}
//获取桌面背景图片
QString PixmapImage::getimg()
{
QGSettings* val = new QGSettings("org.mate.background");
QString aurl = val->get("picture-filename").toString();
return aurl;
}
screenshot.h
#ifndef SCREENSHOT_H
#define SCREENSHOT_H
#include <QObject>
#include <QQuickWindow>
#include <QImage>
class ScreenShot : public QObject
{
Q_OBJECT
public:
explicit ScreenShot(QObject *parent = nullptr);
Q_INVOKABLE QObject* fullshot();
Q_INVOKABLE void curscreenshot(QQuickWindow *rootWindow);
//Q_INVOKABLE void curwidgetshot(QObject *itemObj);
signals:
};
#endif // SCREENSHOT_H
screenshot.cpp
#include "screenshot.h"
#include <QScreen>
#include <QGuiApplication>
#include <QDateTime>
#include <QPixmap>
#include <QQuickItem>
#include <QQuickItemGrabResult>
ScreenShot::ScreenShot(QObject *parent) : QObject(parent)
{
}
QObject* ScreenShot::fullshot()
{
QScreen *screen = QGuiApplication::primaryScreen();
// QString filePathName = "/home/zkx/photo/";
// filePathName += QDateTime::currentDateTime().toString("yyyy-MM-dd hh-mm-ss");
// filePathName += ".jpg";
// if(screen->grabWindow(0).save(filePathName,"jpg")) {
// qDebug("save fullshot sucessfully!");
// }
return screen;
}
//截图保存当前界面(在qml中传入当前界面的id)
void ScreenShot::curscreenshot(QQuickWindow *rootWindow)
{
QImage image = rootWindow->grabWindow();
QString filePathName = "/home/zkx/photo/";
filePathName += QDateTime::currentDateTime().toString("yyyy-MM-dd hh-mm-ss");
filePathName += ".jpg";
if(image.save(filePathName,"JPG")) {
qDebug("save curscreenshot sucessfully!");
}
}
//void ScreenShot::curwidgetshot(QObject *itemObj)
//{
// QQuickItem* grabItem = qobject_cast<QQuickItem*>(itemObj);
// QSharedPointer<QQuickItemGrabResult> grabResult = grabItem->grabToImage();
// QQuickItemGrabResult *grabResultData = grabResult.data();
// grabResultData->ready();
// QString filePathName = "/home/zkx/photo/";
// filePathName += QDateTime::currentDateTime().toString("yyyy-MM-dd hh-mm-ss");
// filePathName += QString(".jpg");
// QImage img = grabResult->image();
// if(img.save(filePathName)){
// qDebug("save curwidgetshot sucessfully!");
// }
//}
main.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQuickWindow>
#include <QQmlContext>
#include "screenshot.h"
#include "pixmapimage.h"
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
const QUrl url(QStringLiteral("qrc:/main.qml"));
qmlRegisterType<PixmapImage>("io.qt.forum", 1, 0, "PixmapImage");
//PixmapImage pixmap;
//engine.rootContext()->setContextProperty("pixmap",&pixmap);
//ScreenShot screenshot;
//engine.rootContext()->setContextProperty("screenshot",&screenshot);
engine.load(url);
QObject *topLevel = engine.rootObjects().value(0);
//提供用于显示图形QML场景的窗口
QQuickWindow *window = qobject_cast<QQuickWindow *>(topLevel);
window->show();
window->showFullScreen();
return app.exec();
}
main.qml
import QtQuick 2.6
import QtQuick.Window 2.2
import QtQuick.Controls 2.0
import io.qt.forum 1.0
Window {
id: root;
visible: true;
width: 1920;
height: 1080;
title: qsTr("Hello World");
color: "transparent";
flags: Qt.FramelessWindowHint;
property string ul: pix.getimg();
Rectangle {
id: rect;
anchors.fill: parent;
Image {
scale: 0.5;
source: "file://" + ul;
anchors.centerIn: parent;
}
opacity: 0;
}
PixmapImage {
id: pix;
width: 1920;
height: 1080;
}
MouseArea {
anchors.fill: parent;
onDoubleClicked: Qt.quit();
onClicked: {
countdown.start();
count.start();
count1.start();
}
}
Timer {
id: countdown;
interval: 10;
onTriggered: {
pix.getscreen();
}
}
Timer {
id: count;
interval: 50;
onTriggered: {
rect.opacity = 1;
}
}
Timer {
id: count1;
interval: 100;
onTriggered: {
a.start();
}
}
SequentialAnimation {
id: a
ParallelAnimation {
ScaleAnimator {
target: pix
from: 1
to: 0.85
duration: 500
easing.type: Easing.OutQuad
}
OpacityAnimator {
target: pix
from: 1
to: 0
duration: 500
easing.type: Easing.OutQuad
}
}
ParallelAnimation {
ScaleAnimator {
target: pix
from: 0.85
to: 1
duration: 1200
easing.type: Easing.OutQuad
}
OpacityAnimator {
target: pix
from: 0
to: 1
duration: 1200
easing.type: Easing.OutQuad
}
}
PauseAnimation {
duration: 400
}
}
}
// Row {
// spacing: 10;
// Button {
// width: 100;
// height: 30;
// text: "全屏截图";
// onClicked: {
// }
// }
Button {
width: 100;
height: 30;
text: "当前界面截图";
onClicked: {
screenshot.curscreenshot(root);
}
}
// }