Qt on Android: Application screenshot, qtandroid

Source: Internet
Author: User

Qt on Android: Application screenshot, qtandroid

On the desktop platform, QScreen: grabWindow can be used to take screenshots of your applications. This is useless on the Android platform, but there are alternative methods.

In two cases, Qt Widgets and Qt Quick.

Insert an advertisement. Please pay attention to the "program horizon" of my subscription number and scan the QR code below:


The program vision updates one or two articles related to programmers every week, starting from the bottom of my heart to talk about programmers in the world and in the world.

Qt Widgets

The most important thing is a function: QWidget: render. This method can render the content of a QWidget (including its children) to a QPixmap, then we can save it by using the save method of QPixmap.

The following is the key code:

void Widget::onGrab(){    QPixmap *pixmap = new QPixmap(size());    render(pixmap);    QString savedPath = QStandardPaths::writableLocation(QStandardPaths::PicturesLocation);    if(savedPath.isEmpty())    {        savedPath = QDir::currentPath();    }    m_savedPathLabel->setText(savedPath);    m_savedPathLabel->adjustSize();    savedPath += "/grabWidgets.png";    bool ret = pixmap->save(savedPath);    if(ret)    {        m_savedPathLabel->setText("OK."+savedPath);    }    else    {        m_savedPathLabel->setText("Failed."+savedPath);    }}

The first two rows of the onGrab () method create a QPixmap with the widget size, and then call the render method. The code below is to save the image to the default image directory of the Android device.

Qt Quick

The Qt Quick application has a QQuickWindow, and QQuickWindow has a grabWindow method. You can save the content of the current window as an image.

This involves QML and C ++ mixed programming. For details, refer to my Qt Quick core programming or "how to explain Qt Quick QML and C ++ mixed programming ".

C ++ code

Header file grabber. h:

class Grabber : public QObject{    Q_OBJECTpublic:    Grabber(QObject *parent = 0);    Q_INVOKABLE QString grab(QQuickWindow *w);};

Key code in the source file grabber. cpp:

QString Grabber::grab(QQuickWindow *w){    QImage image = w->grabWindow();    if(image.isNull()) return QString();    QString savedPath = QStandardPaths::writableLocation(QStandardPaths::PicturesLocation);    if(savedPath.isEmpty())    {        savedPath = QDir::currentPath();    }    savedPath += "/grabQML.png";    bool ret = image.save(savedPath);    if(ret)    {        return savedPath;    }    return QString();}

In addition, you need to set a QmlContext attribute in the main () function. The Code is as follows:

    Grabber *grabber = new Grabber;    engine.rootContext()->setContextProperty("grabber", grabber);

Then we can use Grabber in the Qml file.

QML code

My test QML document main. qml is as follows:

import QtQuick 2.2import QtQuick.Window 2.0import QtQuick.Controls 1.1Window {    id: rootWin;    visible: true;    title: qsTr("Hello Grab QML");    objectName: "rootWin";    Text {        text: qsTr("Hello Grab QML");        color: "blue";        font.pointSize: 16;        anchors.centerIn: parent;    }    Rectangle {        anchors.top: parent.top;        anchors.horizontalCenter: parent.horizontalCenter;        width: 120;        height: 120;        color: "blue";    }    Text {        id: savedPath;        anchors.left: parent.left;        anchors.bottom: parent.bottom;        anchors.bottomMargin: 4;        color: "red";        font.pointSize: 12;    }    Button {        id: grab;        anchors.bottom: savedPath.top;        anchors.bottomMargin: 4;        anchors.horizontalCenter: parent.horizontalCenter;        text: "Grab";        onClicked: {            var saved = grabber.grab(rootWin);            if(saved.length == 0){                savedPath.text = "Failed!";            }else{                savedPath.text= "Ok -" + saved;            }        }    }}

I call grabber. grab () in the onClicked signal processor of the grab button to complete the operation. When the operation succeeds, the path is displayed.

 

OK, that's all.

For more information about Qt on Android, see Qt on Android core programming or the Qt on Android column. For more information about Qt Quick (QML, you can refer to Qt Quick core programming or Qt Quick column ".

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.