Qt on Android: App screenshot

Source: Internet
Author: User

On the desktop platform, Qscreen::grabwindow can take screenshots of your app, which is no use on Android, but there are workarounds.

In two cases, Qt Widgets and Qt Quick.

In-stream ads, please pay attention to my subscription number "program Horizon", scan the following QR code:


The program Horizon updates one to two programmer-related articles a week, starting from the heart, talking about programmers in the eyes of the world and the world.

Qt Widgets

The key is a function: Qwidget::render, this method can be a qwidget (including its children) to render the content of a qpixmap, and then we use the Qpixmap save method can be saved.

Here is the key code:

void Widget::ongrab () {    Qpixmap *pixmap = new Qpixmap (size ());    Render (PIXMAP);    QString Savedpath = qstandardpaths::writablelocation (qstandardpaths::P ictureslocation);    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 lines of the Ongrab () method create a Qpixmap with the size of the widget, and then call the Render method. The next code is to save the image to the default image directory of your Android device.

Qt Quick

Qt Quick application, there will be a qquickwindow, and Qquickwindow has a Grabwindow method, you can apply the current window to save the content as a picture.

This involves mixing QML with C + + programming, referring to myqt Quick Core programming , or "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::P ictureslocation);    if (Savedpath.isempty ())    {        Savedpath = Qdir::currentpath ();    }    Savedpath + = "/grabqml.png";    BOOL ret = Image.Save (savedpath);    if (ret)    {        return savedpath;    }    return QString ();}

It is also necessary to set a Qmlcontext property in the main () function, which is the following code:

    Grabber *grabber = new Grabber;    Engine.rootcontext ()->setcontextproperty ("grabber", grabber);

Then we can use Grabber in the Qml file.

QML Code

My test QML documentation is MAIN.QML 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 () within the onclicked signal processor of the grab button to complete the operation and display the path successfully.

OK, so much more.

To learn more about Qt on Android, you can refer to "Qt on Androidcore programming " or "Qt on Android column" For more QT Quick (QML) content, refer toqt Quick Core programming "or" Qt quick column ".

Qt on Android: App screenshot

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.