用Qt實現(xiàn)類似QQ截圖的工具
以前我們介紹過在Symbian應用程序Widget開發(fā)使用Qt加載和縮放圖片,今天我們來講講用Qt實現(xiàn)類似QQ截圖的工具。首先我們先講一下Qt Widget。
widget.cpp
- #include "widget.h"
- #include "ui_widget.h"
- #include <QtGui>
- Widget::Widget(QWidget *parent) :
- QWidget(parent),
- ui(new Ui::Widget)
- {
- ui->setupUi(this);
- createWidgets();
- createConnects();
- createEventFilter();
- }
- Widget::~Widget()
- {
- delete ui;
- delete quit;
- delete mini;
- delete restore;
- delete menu;
- delete trayIcon;
- delete fullScreenLabel;
- delete shotScreenLabel;
- }
- bool Widget::eventFilter(QObject *o, QEvent *e)
- {
- if (o != fullScreenLabel)
- {
- return Widget::eventFilter(o, e);
- }
- QMouseEvent *mouseEvent = static_cast<QMouseEvent*> (e);
true 鼠標左鍵按下且按鍵還未彈起.
- if ((mouseEvent->button() == Qt::LeftButton)
- && (mouseEvent->type() == QEvent::MouseButtonPress))
鼠標左鍵標志位按下
- leftMousePress = true;
獲取鼠標點
- origin = mouseEvent->pos();
- if (!rubberBand)
- {
- rubberBand = new QRubberBand(QRubberBand::Rectangle, fullScreenLabel);
- }
- rubberBand->setGeometry(QRect(origin,QSize()));
- rubberBand->show();
- return true;
- }
true 鼠標左鍵按下并拖動
- if ((mouseEvent->type() == QEvent::MouseMove)
- && (leftMousePress))
- {
- if (rubberBand)
- {
- rubberBand->setGeometry(QRect(origin, mouseEvent->pos()).normalized());
- }
- return true;
- }
鼠標左鍵松開
- if ((mouseEvent->button() == Qt::LeftButton)
- && (mouseEvent->type() == QEvent::MouseButtonRelease))
- {
鼠標標志位彈起
- leftMousePress = false;
- if (rubberBand)
- {
獲取橡皮筋框的終止坐標
- termination = mouseEvent->pos();
- QRect rect = QRect(origin, termination);
根據(jù)橡皮筋框截取全屏上的信息,并將其放入shotScreenLabel
- shotScreenLabel->setPixmap(fullScreenPixmap.grabWidget(fullScreenLabel,
- rect.x(),
- rect.y(),
- rect.width(),
- rect.height()));
將shotScreenLabel的用戶區(qū)大小固定為所截圖片大小
- shotScreenLabel->setPixmap(fullScreenPixmap.grabWidget(fullScreenLabel,
- rect.x(),
- rect.y(),
- rect.width(),
- rect.height()));
- shotScreenLabel->setFixedSize(rect.width(), rect.height());
- shotScreenLabel->show();
- rubberBand->hide();
- fullScreenLabel->hide();
- }
- return true;
- }
- return false;
- }
- /**
- descr:實例化控件
- */
- void Widget::createWidgets()
- {
兩個QLabel的父控件不能為this,否則截圖信息會現(xiàn)在是主窗口中,無法正確顯示
- fullScreenLabel = new QLabel();
- shotScreenLabel = new QLabel();
- rubberBand = new QRubberBand(QRubberBand::Rectangle, fullScreenLabel);
- leftMousePress = false;
初始化托盤控件并組裝
- trayIcon = new QSystemTrayIcon(QIcon(tr(":/images/heart.svg")), this);
- menu = new QMenu(this);
- restore = new QAction(tr("Restore"), this);
- mini = new QAction(tr("Mini"), this);
- quit = new QAction(tr("Quit"), this);
- menu->addAction(restore);
- menu->addAction(mini);
- menu->addAction(quit);
- trayIcon->setContextMenu(menu);
將托盤顯示
- trayIcon->show();
初始化托盤控件并組裝
- savePixmap = new QAction(tr("save"), shotScreenLabel);
- shotScreenLabel->addAction(savePixmap);
- shotScreenLabel->setContextMenuPolicy(Qt::ActionsContextMenu);
- }
- void Widget::createConnects()
- {
主窗口信號槽
- connect(ui->pbtnShot, SIGNAL(clicked()), this, SLOT(grapWindowScreen()));
- connect(ui->pbtnShotAndMin, SIGNAL(clicked()), this, SLOT(miniWindows()));
- connect(ui->pbtnMin, SIGNAL(clicked()), this, SLOT(miniWindows()));
- connect(savePixmap, SIGNAL(triggered()), this, SLOT(saveShotPixmap()));
主窗口信號槽
托盤信號槽
- connect(restore, SIGNAL(triggered()), this, SLOT(restoreWindows()));
- connect(mini, SIGNAL(triggered()), this, SLOT(miniWindows()));
- connect(quit, SIGNAL(triggered()), this, SLOT(quitApplication()));
- }
- void Widget::createEventFilter()
- {
- fullScreenLabel->installEventFilter(this);
- }
- QString Widget::getSaveShotPixmap()
- {
- return QFileDialog::getSaveFileName(shotScreenLabel,
- tr("Open Image"),
- ".",
- tr("Image Files(.JPG .PNG)"));
- }
- void Widget::grapWindowScreen()
- {
- if (!fullScreenLabel)
- {
- fullScreenLabel = new QLabel();
- }
獲取全屏截圖fullScreenPixmap,并將其放入fullScreenLabel
- fullScreenPixmap = QPixmap::grabWindow(QApplication::desktop()->winId());
- fullScreenLabel->setPixmap(fullScreenPixmap);
label全屏顯示
- fullScreenLabel->showFullScreen();
- }
- void Widget::miniWindows()
- {
- showMinimized();
- grapWindowScreen();
- }
- void Widget::restoreWindows()
- {
- showNormal();
- }
- void Widget::quitApplication()
- {
- qApp->quit();
- }
- void Widget::saveShotPixmap()
- {
- QString fileName = getSaveShotPixmap();
- if (!fileName.isNull())
- {
- fullScreenPixmap.save(fileName);
- }
- }
【編輯推薦】
- Qt 4使用MySQL的中文問題解決方法
- QML教程:構建和安裝QtComponents
- QML教程:Qt-Quick六大開源組件
- QTreeWidget設計解決沒有拖動項問題
- 在Symbian應用程序Widget開發(fā)使用Qt加載和縮放圖片