qml截图

这篇博客介绍了如何在Qt环境中利用QML进行屏幕截图和图像展示。通过`PixmapImage`类实现了屏幕快照的捕获,并在QML中显示。同时,`ScreenShot`类提供了全屏截图和当前界面截图的功能。在`main.qml`中,展示了如何结合定时器和动画效果来动态改变图像的透明度和大小,实现了一种动态的屏幕截图展示效果。
摘要由CSDN通过智能技术生成

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);
            }
        }

//    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值