用Qt实现类似QQ截图的工具

移动开发
以前我们介绍过在Symbian应用程序Widget开发使用Qt加载和缩放图片,今天我们来讲讲用Qt实现类似QQ截图的工具。

以前我们介绍过在Symbian应用程序Widget开发使用Qt加载和缩放图片,今天我们来讲讲用Qt实现类似QQ截图的工具。首先我们先讲一下Qt Widget。

widget.cpp

  1. #include "widget.h"  
  2. #include "ui_widget.h"  
  3. #include <QtGui>  
  4.  
  5. Widget::Widget(QWidget *parent) :  
  6.     QWidget(parent),  
  7.     ui(new Ui::Widget)  
  8. {  
  9.     ui->setupUi(this);  
  10.     createWidgets();  
  11.     createConnects();  
  12.     createEventFilter();  
  13. }  
  14.  
  15. Widget::~Widget()  
  16. {  
  17.     delete ui;  
  18.  
  19.     delete quit;  
  20.     delete mini;  
  21.     delete restore;  
  22.     delete menu;  
  23.     delete trayIcon;  
  24.  
  25.     delete fullScreenLabel;  
  26.     delete shotScreenLabel;  
  27. }  
  28.  
  29. bool Widget::eventFilter(QObject *o, QEvent *e)  
  30. {  
  31.     if (o != fullScreenLabel)  
  32.     {  
  33.         return Widget::eventFilter(o, e);  
  34.     }  
  35.  
  36.     QMouseEvent *mouseEvent = static_cast<QMouseEvent*> (e);  

true 鼠标左键按下且按键还未弹起.

  1. if ((mouseEvent->button() == Qt::LeftButton)  
  2.       && (mouseEvent->type() == QEvent::MouseButtonPress)) 

鼠标左键标志位按下

  1. leftMousePress = true

获取鼠标点

  1. origin = mouseEvent->pos();   
  2.       
  3.         if (!rubberBand)      
  4.         {     
  5.             rubberBand = new QRubberBand(QRubberBand::Rectangle, fullScreenLabel);    
  6.         }     
  7.       
  8.         rubberBand->setGeometry(QRect(origin,QSize()));   
  9.         rubberBand->show();   
  10.       
  11.         return true;      
  12.     }    

true 鼠标左键按下并拖动

  1.     if ((mouseEvent->type() == QEvent::MouseMove)  
  2.         && (leftMousePress))  
  3.     {  
  4.         if (rubberBand)  
  5.         {  
  6.             rubberBand->setGeometry(QRect(origin, mouseEvent->pos()).normalized());  
  7.         }  
  8.  
  9.         return true;  
  10.     }  

鼠标左键松开

  1. if ((mouseEvent->button() == Qt::LeftButton)  
  2.  
  3.        && (mouseEvent->type() == QEvent::MouseButtonRelease))  
  4.  
  5.    {  

鼠标标志位弹起

  1. leftMousePress = false;  
  2.    if (rubberBand)  
  3.       {  

获取橡皮筋框的终止坐标

  1. termination = mouseEvent->pos();  
  2.             QRect rect = QRect(origin, termination);  

根据橡皮筋框截取全屏上的信息,并将其放入shotScreenLabel

  1.             shotScreenLabel->setPixmap(fullScreenPixmap.grabWidget(fullScreenLabel,  
  2.                                                                    rect.x(),  
  3.                                                                    rect.y(),  
  4.                                                                    rect.width(),  
  5.                                                                    rect.height()));  

将shotScreenLabel的用户区大小固定为所截图片大小

  1.             shotScreenLabel->setPixmap(fullScreenPixmap.grabWidget(fullScreenLabel,  
  2.                                                                    rect.x(),  
  3.                                                                    rect.y(),  
  4.                                                                    rect.width(),  
  5.                                                                    rect.height()));  
  6.  
  7. shotScreenLabel->setFixedSize(rect.width(), rect.height());  
  8.             shotScreenLabel->show();  
  9.  
  10.             rubberBand->hide();  
  11.             fullScreenLabel->hide();  
  12.         }  
  13.  
  14.         return true;  
  15.     }  
  16.  
  17.     return false;  
  18. }  
  19.  
  20. /**  
  21.   descr:实例化控件  
  22. */ 
  23. void Widget::createWidgets()  
  24. {  

两个QLabel的父控件不能为this,否则截图信息会现在是主窗口中,无法正确显示

  1.     fullScreenLabel = new QLabel();  
  2.     shotScreenLabel = new QLabel();  
  3.  
  4.     rubberBand = new QRubberBand(QRubberBand::Rectangle, fullScreenLabel);  
  5.  
  6.     leftMousePress = false

初始化托盘控件并组装

  1.     trayIcon = new QSystemTrayIcon(QIcon(tr(":/images/heart.svg")), this);  
  2.     menu = new QMenu(this);  
  3.     restore = new QAction(tr("Restore"), this);  
  4.     mini = new QAction(tr("Mini"), this);  
  5.     quit = new QAction(tr("Quit"), this);  
  6.  
  7.     menu->addAction(restore);  
  8.     menu->addAction(mini);  
  9.     menu->addAction(quit);  
  10.     trayIcon->setContextMenu(menu); 

将托盘显示

  1. trayIcon->show(); 

初始化托盘控件并组装

  1.     savePixmap = new QAction(tr("save"), shotScreenLabel);  
  2.  
  3.     shotScreenLabel->addAction(savePixmap);  
  4.     shotScreenLabel->setContextMenuPolicy(Qt::ActionsContextMenu);  
  5. }  
  6.  
  7. void Widget::createConnects()  
  8. {  

主窗口信号槽

  1. connect(ui->pbtnShot, SIGNAL(clicked()), this, SLOT(grapWindowScreen()));  
  2. connect(ui->pbtnShotAndMin, SIGNAL(clicked()), this, SLOT(miniWindows()));  
  3. connect(ui->pbtnMin, SIGNAL(clicked()), this, SLOT(miniWindows()));  
  4.  
  5. connect(savePixmap, SIGNAL(triggered()), this, SLOT(saveShotPixmap()));  

主窗口信号槽

托盘信号槽

  1.     connect(restore, SIGNAL(triggered()), this, SLOT(restoreWindows()));  
  2.     connect(mini, SIGNAL(triggered()), this, SLOT(miniWindows()));  
  3.     connect(quit, SIGNAL(triggered()), this, SLOT(quitApplication()));  
  4.  
  5. }  
  6.  
  7. void Widget::createEventFilter()  
  8. {  
  9.     fullScreenLabel->installEventFilter(this);  
  10. }  
  11.  
  12. QString Widget::getSaveShotPixmap()  
  13. {  
  14.     return QFileDialog::getSaveFileName(shotScreenLabel,  
  15.                                         tr("Open Image"),  
  16.                                         ".",  
  17.                                         tr("Image Files(.JPG .PNG)"));  
  18. }  
  19.  
  20. void Widget::grapWindowScreen()  
  21. {  
  22.     if (!fullScreenLabel)  
  23.     {  
  24.         fullScreenLabel = new QLabel();  
  25.     }  

获取全屏截图fullScreenPixmap,并将其放入fullScreenLabel

  1. fullScreenPixmap = QPixmap::grabWindow(QApplication::desktop()->winId());  
  2. fullScreenLabel->setPixmap(fullScreenPixmap); 

label全屏显示

  1.     fullScreenLabel->showFullScreen();  
  2. }  
  3.  
  4. void Widget::miniWindows()  
  5. {  
  6.     showMinimized();  
  7.     grapWindowScreen();  
  8. }  
  9.  
  10. void Widget::restoreWindows()  
  11. {  
  12.     showNormal();  
  13. }  
  14.  
  15. void Widget::quitApplication()  
  16. {  
  17.     qApp->quit();  
  18. }  
  19.  
  20. void Widget::saveShotPixmap()  
  21. {  
  22.     QString fileName = getSaveShotPixmap();  
  23.  
  24.     if (!fileName.isNull())  
  25.     {  
  26.         fullScreenPixmap.save(fileName);  
  27.     }  
  28.  
  29. }  


【编辑推荐】

  1. Qt 4使用MySQL的中文问题解决方法
  2. QML教程:构建和安装QtComponents
  3. QML教程:Qt-Quick六大开源组件
  4. QTreeWidget设计解决没有拖动项问题
  5. 在Symbian应用程序Widget开发使用Qt加载和缩放图片

 

责任编辑:佚名
相关推荐

2009-12-29 09:15:05

2023-02-06 09:42:51

GNOME截图工具

2017-09-01 15:42:00

MySQLOracledblink功能

2010-06-04 18:24:37

Linux uml工具

2011-03-18 19:37:38

Eventable接口QtWidget

2018-04-11 09:00:41

2022-07-27 10:39:27

Python打包工具

2011-06-28 15:47:13

Qt 信号

2015-07-20 15:24:44

Linux工具截图

2012-09-25 14:06:28

C#网络协议

2023-03-23 21:32:38

微软Windows

2019-05-20 14:21:34

QQ截图微信截图Windows截图

2012-06-25 12:33:12

Java日志切割

2012-02-13 13:36:27

Java

2021-02-23 09:50:42

PythonJSWeb SSH

2018-01-18 16:10:42

数据库MySQLOracle

2013-06-20 10:40:32

Objective-C实现截图

2010-04-09 17:53:56

Black Berry

2019-03-14 14:41:21

QQ扩列漂流瓶

2011-08-31 16:34:36

windows7截图工具
点赞
收藏

51CTO技术栈公众号