[代码] widget.cpp view sourceprint? 001 #include "widget.h" 002 #include "ui_widget.h" 003 #include <QtGui> 004 005 Widget::Widget(QWidget *parent) : 006 QWidget(parent), 007 ui(new Ui::Widget) 008 { 009 ui->setupUi(this); 010 createWidgets(); 011 createConnects(); 012 createEventFilter(); 013 } 014 015 Widget::~Widget() 016 { 017 delete ui; 018 019 delete quit; 020 delete mini; 021 delete restore; 022 delete menu; 023 delete trayIcon; 024 025 delete fullScreenLabel; 026 delete shotScreenLabel; 027 } 028 029 bool Widget::eventFilter(QObject *o, QEvent *e) 030 { 031 if (o != fullScreenLabel) 032 { 033 return Widget::eventFilter(o, e); 034 } 035 036 QMouseEvent *mouseEvent = static_cast<QMouseEvent*> (e); 037 038 //true 鼠标左键按下且按键还未弹起 039 if ((mouseEvent->button() == Qt::LeftButton) 040 && (mouseEvent->type() == QEvent::MouseButtonPress)) 041 { 042 //鼠标左键标志位按下 043 leftMousePress = true; 044 045 //获取鼠标点 046 origin = mouseEvent->pos(); 047 048 if (!rubberBand) 049 { 050 rubberBand = new QRubberBand(QRubberBand::Rectangle, fullScreenLabel); 051 } 052 053 rubberBand->setGeometry(QRect(origin,QSize())); 054 rubberBand->show(); 055 056 return true; 057 } 058 059 //true 鼠标左键按下并拖动 060 if ((mouseEvent->type() == QEvent::MouseMove) 061 && (leftMousePress)) 062 { 063 if (rubberBand) 064 { 065 rubberBand->setGeometry(QRect(origin, mouseEvent->pos()).normalized()); 066 } 067 068 return true; 069 } 070 071 //鼠标左键松开 072 if ((mouseEvent->button() == Qt::LeftButton) 073 && (mouseEvent->type() == QEvent::MouseButtonRelease)) 074 { 075 //鼠标标志位弹起 076 leftMousePress = false; 077 078 if (rubberBand) 079 { 080 //获取橡皮筋框的终止坐标 081 termination = mouseEvent->pos(); 082 QRect rect = QRect(origin, termination); 083 084 //根据橡皮筋框截取全屏上的信息,并将其放入shotScreenLabel 085 shotScreenLabel->setPixmap(fullScreenPixmap.grabWidget(fullScreenLabel, 086 rect.x(), 087 rect.y(), 088 rect.width(), 089 rect.height())); 090 091 //将shotScreenLabel的用户区大小固定为所截图片大小 092 shotScreenLabel->setFixedSize(rect.width(), rect.height()); 093 shotScreenLabel->show(); 094 095 rubberBand->hide(); 096 fullScreenLabel->hide(); 097 } 098 099 return true; 100 } 101 102 return false; 103 } 104 105 /** 106 descr:实例化控件 107 */ 108 void Widget::createWidgets() 109 { 110 //两个QLabel的父控件不能为this,否则截图信息会现在是主窗口中,无法正确显示 111 fullScreenLabel = new QLabel(); 112 shotScreenLabel = new QLabel(); 113 114 rubberBand = new QRubberBand(QRubberBand::Rectangle, fullScreenLabel); 115 116 leftMousePress = false; 117 118 //初始化托盘控件并组装************************************************************** 119 120 trayIcon = new QSystemTrayIcon(QIcon(tr(":/images/heart.svg")), this); 121 menu = new QMenu(this); 122 restore = new QAction(tr("Restore"), this); 123 mini = new QAction(tr("Mini"), this); 124 quit = new QAction(tr("Quit"), this); 125 126 menu->addAction(restore); 127 menu->addAction(mini); 128 menu->addAction(quit); 129 trayIcon->setContextMenu(menu); 130 131 //将托盘显示 132 trayIcon->show(); 133 134 //初始化托盘控件并组装************************************************************** 135 136 savePixmap = new QAction(tr("save"), shotScreenLabel); 137 138 shotScreenLabel->addAction(savePixmap); 139 shotScreenLabel->setContextMenuPolicy(Qt::ActionsContextMenu); 140 } 141 142 void Widget::createConnects() 143 { 144 //主窗口信号槽***************************************************************** 145 146 connect(ui->pbtnShot, SIGNAL(clicked()), this, SLOT(grapWindowScreen())); 147 connect(ui->pbtnShotAndMin, SIGNAL(clicked()), this, SLOT(miniWindows())); 148 connect(ui->pbtnMin, SIGNAL(clicked()), this, SLOT(miniWindows())); 149 150 connect(savePixmap, SIGNAL(triggered()), this, SLOT(saveShotPixmap())); 151 152 //主窗口信号槽***************************************************************** 153 154 //托盘信号槽******************************************************************* 155 156 connect(restore, SIGNAL(triggered()), this, SLOT(restoreWindows())); 157 connect(mini, SIGNAL(triggered()), this, SLOT(miniWindows())); 158 connect(quit, SIGNAL(triggered()), this, SLOT(quitApplication())); 159 160 //托盘信号槽******************************************************************* 161 } 162 163 void Widget::createEventFilter() 164 { 165 fullScreenLabel->installEventFilter(this); 166 } 167 168 QString Widget::getSaveShotPixmap() 169 { 170 return QFileDialog::getSaveFileName(shotScreenLabel, 171 tr("Open Image"), 172 ".", 173 tr("Image Files(*.JPG *.PNG)")); 174 } 175 176 void Widget::grapWindowScreen() 177 { 178 if (!fullScreenLabel) 179 { 180 fullScreenLabel = new QLabel(); 181 } 182 183 //获取全屏截图fullScreenPixmap,并将其放入fullScreenLabel 184 fullScreenPixmap = QPixmap::grabWindow(QApplication::desktop()->winId()); 185 fullScreenLabel->setPixmap(fullScreenPixmap); 186 187 //label全屏显示 188 fullScreenLabel->showFullScreen(); 189 } 190 191 void Widget::miniWindows() 192 { 193 showMinimized(); 194 grapWindowScreen(); 195 } 196 197 void Widget::restoreWindows() 198 { 199 showNormal(); 200 } 201 202 void Widget::quitApplication() 203 { 204 qApp->quit(); 205 } 206 207 void Widget::saveShotPixmap() 208 { 209 QString fileName = getSaveShotPixmap(); 210 211 if (!fileName.isNull()) 212 { 213 fullScreenPixmap.save(fileName); 214 } 215 216 }