zoukankan      html  css  js  c++  java
  • [转]

    截屏(screenshot),就是将屏幕上的东西拷贝下来存成图片文件。介绍的好像有点多余:(,那我们就直接切入正题。

    QPixmap提供了两个函数grabWidget和grabWindow可以将屏幕上的窗体存成一个QPixmap格式的图片,很容易再将QPixmap存成文件。函数使用很简单,两行代码就可以。
    QPixmap pixmap = QPixmap::grabWidget(this);//this是调用该函数的QWidget *指针
    pixmap.save(“widget.png”,”png”);

    这两个函数的区别,我个人理解是grabWindow可以抓取窗口管理器的内容,如果不是应用的顶层窗口,那么grabWidget和grabWindow没什么区别。下面是我的两个截图,一个带窗口管理器的,一个不带。

    下面是一个简单的源码演示,在blog页上点击按钮的时候调用grabWindow(),在bbs页上点击大按钮的时候去调用grabWidget().注意,我在第一个页里调用的是整个desktop的窗口id,然后根据坐标取到了当前窗口的截图。所以你也可以通过它去获取非Qt窗口的截图,当然你需要一点X11的知识设法搞到其它窗口的坐标。

    源码:

    #include <QApplication>
    #include <QDesktopWidget>
    #include <QTabWidget>
    #include <QPushButton>
    #include <QPixmap>

    class MyTabWidget:public QTabWidget{
    Q_OBJECT
    public slots:
    void snapshot1(){
    QPixmap pixmap = QPixmap::grabWindow(QApplication::desktop()->winId(),pos().x(),pos().y(),frameGeometry().width(),frameGeometry().height());
    pixmap.save(“blog.png”, “png”);
    }
    void snapshot2(){
    QPixmap pixmap = QPixmap::grabWidget(this);
    pixmap.save(“bbs.png”, “png”);
    }
    };

    int main(int argc,char *argv[]){
    QApplication app(argc,argv);
    MyTabWidget tabw;
    QPushButton *pb1=new QPushButton(“http://www.cuteqt.com/blog”,&tabw);
    QPushButton *pb2=new QPushButton(“http://www.c0teqt.com/bbs”,&tabw);
    tabw.addTab(pb1,”blog”);
    tabw.addTab(pb2,”bbs”);

    tabw.connect(pb1,SIGNAL(clicked()),&tabw,SLOT(snapshot1()));
    tabw.connect(pb2,SIGNAL(clicked()),&tabw,SLOT(snapshot2()));

    tabw.resize(300,200);
    tabw.show();
    return app.exec();
    }

    #include “main.moc”

    源码里用到的一点坐标知识,和Qt的窗口坐标有关,附图一张,胜过千言万语

  • 相关阅读:
    Cookie中文乱码问题
    [转]Resolving kernel symbols
    [转]Blocking Code Injection on iOS and OS X
    [转]About the security content of iOS 8
    [转]iOS: About diagnostic capabilities
    [转]iOS hacking resource collection
    [转]Patching the Mach-o Format the Simple and Easy Way
    [转]Use the IDA and LLDB explore WebCore C + + class inheritance
    [转]Avoiding GDB Signal Noise.
    [转]http://www.russbishop.net/xcode-exception-breakpoints
  • 原文地址:https://www.cnblogs.com/suanec/p/4170662.html
Copyright © 2011-2022 走看看