QT 模仿QQ的截图框 支持基本的拖拉 移动 固定大小 可以复制到剪贴板和保存到文件中

本文主要是介绍QT 模仿QQ的截图框 支持基本的拖拉 移动 固定大小 可以复制到剪贴板和保存到文件中,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

#include "widget.h"
#include "InputDialog.h"
#include <QtGui>
Widget::Widget(QWidget *parent,QColor selectedColor,int quality): QDialog(parent),quality(quality),sColor(selectedColor)
{saveAt=new QAction(tr("保存到文件"),this);saveAt->setIcon(QIcon(":/image/save.png"));saveTob=new QAction(tr("保存到剪贴板"),this);saveTob->setIcon(QIcon(":/image/savetob.png"));closeAt=new QAction(tr("退出"),this);closeAt->setIcon(QIcon(":/image/quit.png"));helpAt=new QAction(tr("使用帮助"),this);helpAt->setIcon(QIcon(":/image/help.png"));fixedSizeAt=new QAction(tr("设置固定尺寸"),this);fixedSizeAt->setIcon(QIcon(":/image/fixedsize.png"));menu=new QMenu(tr("截图菜单"),this);menu->setStyleSheet("background-image:url(:/image/back.png);color:white");menu->addAction(saveAt);menu->addAction(saveTob);menu->addSeparator();menu->addAction(fixedSizeAt);menu->addSeparator();menu->addAction(helpAt);menu->addSeparator();menu->addAction(closeAt);connect(saveAt,SIGNAL(triggered()),this,SLOT(saveImage()));connect(saveTob,SIGNAL(triggered()),this,SLOT(saveBoard()));connect(closeAt,SIGNAL(triggered()),this,SLOT(close()));connect(helpAt,SIGNAL(triggered()),this,SLOT(emitHelp()));connect(fixedSizeAt,SIGNAL(triggered()),this,SLOT(input()));this->setMouseTracking(true);leftPressed=rightPressed=runOnce=done=pull=bresize=showm=false;fullSize=QApplication::desktop()->size();this->resize(fullSize);this->setWindowFlags(Qt::FramelessWindowHint|Qt::WindowStaysOnTopHint);//this->hide();//获取了当前屏幕并存储在pixmapscreen=new QPixmap(QPixmap::grabWindow(QApplication::desktop()->winId(),0,0,fullSize.width(),fullSize.height()));//新建一层朦胧屏幕fogPix=new QPixmap(*screen);QPixmap fog(fullSize.width(),fullSize.height());fog.fill(QColor(182,182,182,192));QPainter painter(fogPix);painter.drawPixmap(0,0,fog);painter.setPen(QPen(QColor(255,255,0)));painter.drawText(fullSize.width()/2-50,50,tr("如需使用帮助,请按F1"));
}void Widget::paintEvent(QPaintEvent *e)
{QPainter painter(this);QPen pen;pen.setColor(Qt::yellow);pen.setWidth(1);painter.setPen(pen);painter.drawPixmap(0,0,*fogPix);QPainter painterInfo(this);if(pos.tlp.x()!=pos.brp.x()&&pos.tlp.y()!=pos.brp.y()){QPoint zPoint(1,1);painterInfo.setPen(QPen(sColor));painterInfo.drawRect(QRect(pos.tlp-zPoint,pos.brp));painterInfo.drawRect(pos.tlp.x()-3,pos.tlp.y()-3,3,3);painterInfo.drawRect(pos.trp.x(),pos.trp.y()-3,3,3);painterInfo.drawRect(pos.blp.x()-3,pos.blp.y()+1,3,3);painterInfo.drawRect(pos.brp.x()+1,pos.brp.y()+1,3,3);painterInfo.drawRect((pos.tlp.x()+pos.trp.x())/2-2,(pos.tlp.y()-4),3,3);painterInfo.drawRect(pos.tlp.x()-4,(pos.tlp.y()+pos.blp.y())/2-2,3,3);painterInfo.drawRect(pos.trp.x()+1,(pos.trp.y()+pos.brp.y())/2-2,3,3);painterInfo.drawRect((pos.blp.x()+pos.brp.x())/2-2,pos.blp.y()+1,3,3);painterInfo.drawPixmap(pos.tlp,screen->copy(QRect(pos.tlp,pos.brp)));painterInfo.setBrush(QBrush(QColor(200,200,200,165)));painterInfo.setPen(QPen(QColor(200,200,200,165)));painterInfo.drawRect(pos.tlp.x(),pos.tlp.y()>=40?pos.tlp.y()-40:pos.tlp.y(),150,40);painterInfo.setPen(QPen(Qt::black));painterInfo.drawText(pos.tlp.x()+10,pos.tlp.y()>=40?pos.tlp.y()-25:pos.tlp.y()+15,tr("截图开始坐标(%1,%2)").arg(QString::number(pos.tlp.x()),QString::number(pos.tlp.y())));painterInfo.drawText(pos.tlp.x()+10,pos.tlp.y()>=40?pos.tlp.y()-10:pos.tlp.y()+30,tr("截图尺寸大小(%1,%2)").arg(QString::number(pos.brp.x()-pos.tlp.x()),QString::number(pos.brp.y()-pos.tlp.y())));}else{cursor.setShape(Qt::ArrowCursor);this->setCursor(cursor);}
}
void Widget::mousePressEvent(QMouseEvent *e)
{if(e->button()==Qt::LeftButton){leftPressed=true;if(!(e->globalPos().x()>pos.tlp.x()&&e->globalPos().y()>pos.tlp.y()&&e->globalPos().x()<pos.brp.x()&&e->globalPos().y()<pos.brp.y())&&!(e->globalPos().x()>=pos.tlp.x()-5&&e->globalPos().x()<=pos.tlp.x()-1&&e->globalPos().y()>=pos.tlp.y()-5&&e->globalPos().y()<=pos.tlp.y()-1)&&!(e->globalPos().x()>=pos.trp.x()+1&&e->globalPos().x()<=pos.trp.x()+5&&e->globalPos().y()<=pos.trp.y()+1&&e->globalPos().y()>=pos.trp.y()-5)&&!(e->globalPos().x()>=pos.blp.x()-5&&e->globalPos().x()<=pos.blp.x()-1&&e->globalPos().y()<=pos.blp.y()+5&&e->globalPos().y()>=pos.blp.y()+1)&&!(e->globalPos().x()>=pos.brp.x()+1&&e->globalPos().x()<=pos.brp.x()+5&&e->globalPos().y()>=pos.brp.y()+1&&e->globalPos().y()<=pos.brp.y()+5)&&!((e->globalPos().x()>=(pos.tlp.x()+pos.trp.x())/2-2&&e->globalPos().x()<=(pos.tlp.x()+pos.trp.x())/2+2&&e->globalPos().y()<=pos.tlp.y()-1&&e->globalPos().y()>=pos.tlp.y()-4))&&!(e->globalPos().x()>=pos.tlp.x()-4&&e->globalPos().x()<=pos.tlp.x()-1&&e->globalPos().y()>=(pos.tlp.y()+pos.blp.y())/2-2&&e->globalPos().y()<=(pos.tlp.y()+pos.blp.y())/2+2)&&!(e->globalPos().x()>=(pos.blp.x()+pos.brp.x())/2-2&&e->globalPos().x()<=(pos.blp.x()+pos.brp.x())/2+2&&e->globalPos().y()>=pos.blp.y()+1&&e->globalPos().y()<=pos.blp.y()+4)&&!(e->globalPos().x()>=pos.trp.x()+1&&e->globalPos().x()<=pos.trp.x()+4&&e->globalPos().y()>=(pos.trp+pos.brp).y()/2-2&&e->globalPos().y()<=(pos.trp+pos.brp).y()/2+2)) //不在所选的区域内{p1=e->globalPos();done=false;}if((e->globalPos().x()>pos.tlp.x()&&e->globalPos().y()>pos.tlp.y()&&e->globalPos().x()<pos.brp.x()&&e->globalPos().y()<pos.brp.y())){pull=true;}if(e->globalPos().x()>=pos.tlp.x()-5&&e->globalPos().x()<=pos.tlp.x()-1&&e->globalPos().y()>=pos.tlp.y()-5&&e->globalPos().y()<=pos.tlp.y()-1){bresize=true;moved=1;}if(e->globalPos().x()>=pos.trp.x()+1&&e->globalPos().x()<=pos.trp.x()+5&&e->globalPos().y()<=pos.trp.y()+1&&e->globalPos().y()>=pos.trp.y()-5){bresize=true;moved=2;}if(e->globalPos().x()>=pos.blp.x()-5&&e->globalPos().x()<=pos.blp.x()-1&&e->globalPos().y()<=pos.blp.y()+5&&e->globalPos().y()>=pos.blp.y()+1){bresize=true;moved=3;}if(e->globalPos().x()>=pos.brp.x()+1&&e->globalPos().x()<=pos.brp.x()+5&&e->globalPos().y()>=pos.brp.y()+1&&e->globalPos().y()<=pos.brp.y()+5){bresize=true;moved=4;}if(e->globalPos().x()>=(pos.tlp.x()+pos.trp.x())/2-2&&e->globalPos().x()<=(pos.tlp.x()+pos.trp.x())/2+2&&e->globalPos().y()<=pos.tlp.y()-1&&e->globalPos().y()>=pos.tlp.y()-4){bresize=true;moved=5;}if(e->globalPos().x()>=pos.tlp.x()-4&&e->globalPos().x()<=pos.tlp.x()-1&&e->globalPos().y()>=(pos.tlp.y()+pos.blp.y())/2-2&&e->globalPos().y()<=(pos.tlp.y()+pos.blp.y())/2+2){bresize=true;moved=6;}if(e->globalPos().x()>=(pos.blp.x()+pos.brp.x())/2-2&&e->globalPos().x()<=(pos.blp.x()+pos.brp.x())/2+2&&e->globalPos().y()>=pos.blp.y()+1&&e->globalPos().y()<=pos.blp.y()+4){bresize=true;moved=8;}if(e->globalPos().x()>=pos.trp.x()+1&&e->globalPos().x()<=pos.trp.x()+4&&e->globalPos().y()>=(pos.trp+pos.brp).y()/2-2&&e->globalPos().y()<=(pos.trp+pos.brp).y()/2+2){bresize=true;moved=7;}}else if(e->button()==Qt::RightButton){if(!(e->globalPos().x()>pos.tlp.x()&&e->globalPos().y()>pos.tlp.y()&&e->globalPos().x()<pos.brp.x()&&e->globalPos().y()<pos.brp.y())&&!(e->globalPos().x()>=pos.tlp.x()-5&&e->globalPos().x()<=pos.tlp.x()-1&&e->globalPos().y()>=pos.tlp.y()-5&&e->globalPos().y()<=pos.tlp.y()-1)&&!(e->globalPos().x()>=pos.trp.x()+1&&e->globalPos().x()<=pos.trp.x()+5&&e->globalPos().y()<=pos.trp.y()+1&&e->globalPos().y()>=pos.trp.y()-5)&&!(e->globalPos().x()>=pos.blp.x()-5&&e->globalPos().x()<=pos.blp.x()-1&&e->globalPos().y()<=pos.blp.y()+5&&e->globalPos().y()>=pos.blp.y()+1)&&!(e->globalPos().x()>=pos.brp.x()+1&&e->globalPos().x()<=pos.brp.x()+5&&e->globalPos().y()>=pos.brp.y()+1&&e->globalPos().y()<=pos.brp.y()+5)&&!(e->globalPos().x()>=(pos.tlp.x()+pos.trp.x())/2-2&&e->globalPos().x()<=(pos.tlp.x()+pos.trp.x())/2+2&&e->globalPos().y()<=pos.tlp.y()-1&&e->globalPos().y()>=pos.tlp.y()-4)&&!(e->globalPos().x()>=pos.tlp.x()-4&&e->globalPos().x()<=pos.tlp.x()-1&&e->globalPos().y()>=(pos.tlp.y()+pos.blp.y())/2-2&&e->globalPos().y()<=(pos.tlp.y()+pos.blp.y())/2+2)&&!(e->globalPos().x()>=(pos.blp.x()+pos.brp.x())/2-2&&e->globalPos().x()<=(pos.blp.x()+pos.brp.x())/2+2&&e->globalPos().y()>=pos.blp.y()+1&&e->globalPos().y()<=pos.blp.y()+4)&&!(e->globalPos().x()>=pos.trp.x()+1&&e->globalPos().x()<=pos.trp.x()+4&&e->globalPos().y()>=(pos.trp+pos.brp).y()/2-2&&e->globalPos().y()<=(pos.trp+pos.brp).y()/2+2)) //不在所选的区域内{this->reset();done=false;leftPressed=false;rightPressed=false;runOnce=false;bresize=false;pull=false;showm=false;this->update();}else{showm=true;}}
}
void Widget::mouseMoveEvent(QMouseEvent *e)
{if(leftPressed){if(!done){p2.setX(e->globalPos().x());p2.setY(e->globalPos().y());this->figPos(p1,p2);this->update();}if(bresize){if(moved==1){if(!runOnce){start=e->globalPos();runOnce=true;}if(pos.tlp.x()+(e->globalPos().x()-start.x())<pos.brp.x()&&pos.tlp.y()+(e->globalPos().y()-start.y())<pos.brp.y()){pos.tlp.setX(pos.tlp.x()+(e->globalPos().x()-start.x()));pos.tlp.setY(pos.tlp.y()+(e->globalPos().y()-start.y()));this->figPos(pos.tlp,pos.brp);}start=e->globalPos();this->update();}else if(moved==2){if(!runOnce){start=e->globalPos();runOnce=true;}if(pos.trp.x()+(e->globalPos().x()-start.x())>pos.blp.x()&&pos.trp.y()+(e->globalPos().y()-start.y())<pos.blp.y()){pos.trp.setX(pos.trp.x()+(e->globalPos().x()-start.x()));pos.trp.setY(pos.trp.y()+(e->globalPos().y()-start.y()));this->figPos(pos.trp,pos.blp);}start=e->globalPos();this->update();}else if(moved==3){if(!runOnce){start=e->globalPos();runOnce=true;}if(pos.blp.x()+(e->globalPos().x()-start.x())<pos.trp.x()&&pos.blp.y()+(e->globalPos().y()-start.y())>pos.trp.y()){pos.blp.setX(pos.blp.x()+(e->globalPos().x()-start.x()));pos.blp.setY(pos.blp.y()+(e->globalPos().y()-start.y()));this->figPos(pos.blp,pos.trp);}start=e->globalPos();this->update();}else if(moved==4){if(!runOnce){start=e->globalPos();runOnce=true;}if(pos.brp.x()+(e->globalPos().x()-start.x())>pos.tlp.x()&&pos.brp.y()+(e->globalPos().y()-start.y())>pos.tlp.y()){pos.brp.setX(pos.brp.x()+(e->globalPos().x()-start.x()));pos.brp.setY(pos.brp.y()+(e->globalPos().y()-start.y()));this->figPos(pos.brp,pos.tlp);}start=e->globalPos();this->update();}else if(moved==5){if(!runOnce){start=e->globalPos();runOnce=true;}if(pos.tlp.y()+(e->globalPos().y()-start.y())<pos.blp.y()){pos.tlp.setY(pos.tlp.y()+(e->globalPos().y()-start.y()));this->figPos(pos.tlp,pos.brp);}start=e->globalPos();this->update();}else if(moved==6){if(!runOnce){start=e->globalPos();runOnce=true;}if((pos.tlp.x()+(e->globalPos()-start).x())<pos.brp.x()){pos.tlp.setX(pos.tlp.x()+(e->globalPos().x()-start.x()));this->figPos(pos.tlp,pos.brp);}start=e->globalPos();this->update();}else if(moved==8){if(!runOnce){start=e->globalPos();runOnce=true;}if(pos.blp.y()+(e->globalPos().y()-start.y())>pos.tlp.y()){pos.blp.setY(pos.blp.y()+(e->globalPos().y()-start.y()));this->figPos(pos.blp,pos.trp);}start=e->globalPos();this->update();}else if(moved=7){if(!runOnce){start=e->globalPos();runOnce=true;}if((pos.trp.x()+(e->globalPos()-start).x())>pos.blp.x()){pos.trp.setX(pos.trp.x()+(e->globalPos().x()-start.x()));this->figPos(pos.trp,pos.blp);}start=e->globalPos();this->update();}}if(pull){if(!runOnce){start=e->globalPos();runOnce=true;}if(pos.tlp.x()+e->globalPos().x()-start.x()>0&&pos.tlp.x()+e->globalPos().x()-start.x()<=fullSize.width()&&pos.tlp.y()+e->globalPos().y()-start.y()>0&&pos.tlp.y()+e->globalPos().y()-start.y()<=fullSize.height()&&pos.brp.x()+e->globalPos().x()-start.x()>=0&&pos.brp.x()+e->globalPos().x()-start.x()<fullSize.width()&&pos.brp.y()+e->globalPos().y()-start.y()>=0&&pos.brp.y()+e->globalPos().y()-start.y()<=fullSize.height()){pos.tlp+=(e->globalPos()-start);pos.brp+=(e->globalPos()-start);this->figPos(pos.tlp,pos.brp);}else if(pos.tlp.x()+e->globalPos().x()-start.x()<=0&&pos.blp.y()+e->globalPos().y()-start.y()<=fullSize.height()&&pos.tlp.y()+e->globalPos().y()-start.y()>=0)//左边{pos.tlp.setX(0);pos.tlp.setY(pos.tlp.y()+e->globalPos().y()-start.y());pos.brp.setX(pos.brp.x()-pos.blp.x());pos.brp.setY(pos.brp.y()+e->globalPos().y()-start.y());this->figPos(pos.tlp,pos.brp);}else if(pos.tlp.y()+e->globalPos().y()-start.y()<=0&&pos.tlp.x()+e->globalPos().x()-start.x()>=0&&pos.trp.x()+e->globalPos().x()-start.x()<=fullSize.width())//上{pos.tlp.setY(0);pos.tlp.setX(pos.tlp.x()+e->globalPos().x()-start.x());pos.brp.setX(pos.brp.x()+e->globalPos().x()-start.x());pos.brp.setY(pos.tlp.y()+pos.blp.y()-pos.trp.y());this->figPos(pos.tlp,pos.brp);}else if(pos.brp.x()+e->globalPos().x()-start.x()>=fullSize.width()&&pos.trp.y()+e->globalPos().y()-start.y()>=0&&pos.brp.y()+e->globalPos().y()-start.y()<=fullSize.height())//右{pos.brp.setX(fullSize.width());pos.brp.setY(pos.brp.y()+e->globalPos().y()-start.y());pos.tlp.setX(fullSize.width()-(pos.trp.x()-pos.blp.x()));pos.tlp.setY(pos.tlp.y()+e->globalPos().y()-start.y());this->figPos(pos.tlp,pos.brp);}else if(pos.brp.y()+e->globalPos().y()-start.y()>=fullSize.height()&&pos.brp.x()+e->globalPos().x()-start.x()<=fullSize.width()&&pos.blp.x()+e->globalPos().x()-start.x()>=0)//下{pos.brp.setY(fullSize.height());pos.brp.setX(pos.brp.x()+e->globalPos().x()-start.x());pos.tlp.setX(pos.tlp.x()+e->globalPos().x()-start.x());pos.tlp.setY(pos.brp.y()-(pos.blp.y()-pos.trp.y()));this->figPos(pos.tlp,pos.brp);}start=e->globalPos();this->update();}}else{if(e->globalPos().x()>=pos.tlp.x()-5&&e->globalPos().x()<=pos.tlp.x()-1&&e->globalPos().y()>=pos.tlp.y()-5&&e->globalPos().y()<=pos.tlp.y()-1){cursor.setShape(Qt::SizeFDiagCursor);this->setCursor(cursor);}else if((e->globalPos().x()>pos.tlp.x()&&e->globalPos().y()>pos.tlp.y()&&e->globalPos().x()<pos.brp.x()&&e->globalPos().y()<pos.brp.y())){cursor.setShape(Qt::SizeAllCursor);this->setCursor(cursor);}else if(e->globalPos().x()>=pos.trp.x()+1&&e->globalPos().x()<=pos.trp.x()+5&&e->globalPos().y()<=pos.trp.y()+1&&e->globalPos().y()>=pos.trp.y()-5){cursor.setShape(Qt::SizeBDiagCursor);this->setCursor(cursor);}else if(e->globalPos().x()>=pos.blp.x()-5&&e->globalPos().x()<=pos.blp.x()-1&&e->globalPos().y()<=pos.blp.y()+5&&e->globalPos().y()>=pos.blp.y()+1){cursor.setShape(Qt::SizeBDiagCursor);this->setCursor(cursor);}else if(e->globalPos().x()>=pos.brp.x()+1&&e->globalPos().x()<=pos.brp.x()+5&&e->globalPos().y()>=pos.brp.y()+1&&e->globalPos().y()<=pos.brp.y()+5){cursor.setShape(Qt::SizeFDiagCursor);this->setCursor(cursor);}else if(e->globalPos().x()>=(pos.tlp.x()+pos.trp.x())/2-2&&e->globalPos().x()<=(pos.tlp.x()+pos.trp.x())/2+2&&e->globalPos().y()<=pos.tlp.y()-1&&e->globalPos().y()>=pos.tlp.y()-4){cursor.setShape(Qt::SizeVerCursor);this->setCursor(cursor);}else if(e->globalPos().x()>=pos.tlp.x()-4&&e->globalPos().x()<=pos.tlp.x()-1&&e->globalPos().y()>=(pos.tlp.y()+pos.blp.y())/2-2&&e->globalPos().y()<=(pos.tlp.y()+pos.blp.y())/2+2){cursor.setShape(Qt::SizeHorCursor);this->setCursor(cursor);}else if(e->globalPos().x()>=(pos.blp.x()+pos.brp.x())/2-2&&e->globalPos().x()<=(pos.blp.x()+pos.brp.x())/2+2&&e->globalPos().y()>=pos.blp.y()+1&&e->globalPos().y()<=pos.blp.y()+4){cursor.setShape(Qt::SizeVerCursor);this->setCursor(cursor);}else if(e->globalPos().x()>=pos.trp.x()+1&&e->globalPos().x()<=pos.trp.x()+4&&e->globalPos().y()>=(pos.trp+pos.brp).y()/2-2&&e->globalPos().y()<=(pos.trp+pos.brp).y()/2+2){cursor.setShape(Qt::SizeHorCursor);this->setCursor(cursor);}else{QCursor cursor;cursor.setShape(Qt::ArrowCursor);this->setCursor(cursor);}}if(rightPressed){}
}
void Widget::mouseReleaseEvent(QMouseEvent *e)
{leftPressed=false;runOnce=false;done=true;bresize=false;pull=false;if(e->button()==Qt::RightButton){if(!(e->globalPos().x()>pos.tlp.x()&&e->globalPos().y()>pos.tlp.y()&&e->globalPos().x()<pos.brp.x()&&e->globalPos().y()<pos.brp.y())&&!(e->globalPos().x()>=pos.tlp.x()-5&&e->globalPos().x()<=pos.tlp.x()-1&&e->globalPos().y()>=pos.tlp.y()-5&&e->globalPos().y()<=pos.tlp.y()-1)&&!(e->globalPos().x()>=pos.trp.x()+1&&e->globalPos().x()<=pos.trp.x()+5&&e->globalPos().y()<=pos.trp.y()+1&&e->globalPos().y()>=pos.trp.y()-5)&&!(e->globalPos().x()>=pos.blp.x()-5&&e->globalPos().x()<=pos.blp.x()-1&&e->globalPos().y()<=pos.blp.y()+5&&e->globalPos().y()>=pos.blp.y()+1)&&!(e->globalPos().x()>=pos.brp.x()+1&&e->globalPos().x()<=pos.brp.x()+5&&e->globalPos().y()>=pos.brp.y()+1&&e->globalPos().y()<=pos.brp.y()+5)){}else{if(showm){menu->exec(e->globalPos());}}}rightPressed=false;showm=false;//cursor.setShape(Qt::ArrowCursor);//this->setCursor(cursor);
}
void Widget::keyPressEvent(QKeyEvent *e)
{switch(e->key()){case Qt::Key_Left:if(pos.tlp.x()-1>=0){pos.tlp.setX(pos.tlp.x()-1);pos.brp.setX(pos.brp.x()-1);}break;case Qt::Key_Right:if(pos.blp.x()+1<=fullSize.width()){pos.tlp.setX(pos.tlp.x()+1);pos.brp.setX(pos.brp.x()+1);this->figPos(pos.tlp,pos.brp);}break;case Qt::Key_Up:if(pos.tlp.y()-1>=0){pos.tlp.setY(pos.tlp.y()-1);pos.brp.setY(pos.brp.y()-1);this->figPos(pos.tlp,pos.brp);}break;case Qt::Key_Down:if(pos.brp.y()+1<=fullSize.height()){pos.tlp.setY(pos.tlp.y()+1);pos.brp.setY(pos.brp.y()+1);this->figPos(pos.tlp,pos.brp);}break;case Qt::Key_Enter:this->saveImage();break;case Qt::Key_Escape:this->close();this->destroy();emit closed();break;case Qt::Key_Space:this->saveBoard();default:break;}this->update();
}
void Widget::saveImage()
{QString slcStr;QPoint end=pos.brp;end.setX(end.x()-1);end.setY(end.y()-1);fileName=QFileDialog::getSaveFileName(this,tr("保存截图"),"c:/untitled.png",tr("PNG(*.png);;JPG(*.jpg);;BMP(*.bmp)"),&slcStr);if(slcStr.left(3)=="JPG"){if(fileName.right(3)!="jpg"){fileName+=".jpg";}}if(slcStr.left(3)=="PNG"){if(fileName.right(3)!="png"){fileName+=".png";}}if(slcStr.left(3)=="BMP"){if(fileName.right(3)!="bmp"){fileName+=".bmp";}}if(!fileName.isEmpty()){file=new QFile(fileName);}elsereturn;QFileInfo fileInfo(fileName);QString ext=fileInfo.suffix();if(file->open(QIODevice::WriteOnly)){screen->copy(QRect(pos.tlp,end)).save(file,ext.toStdString().c_str(),quality);qDebug()<<ext;file->close();}delete file;this->close();
}
void Widget::saveBoard()
{QApplication::clipboard()->setImage(screen->copy(QRect(pos.tlp,pos.brp)).toImage());this->close();
}
void Widget::reset()
{QPoint zeroP(0,0);pos.blp=zeroP;pos.brp=zeroP;pos.tlp=zeroP;pos.trp=zeroP;
}
void Widget::figPos(const QPoint& p1,const QPoint& p2)
{if(p1.x()<p2.x()&&p1.y()<p2.y()){pos.tlp=p1;pos.trp.setX(p2.x());pos.trp.setY(p1.y());pos.blp.setX(p1.x());pos.blp.setY(p2.y());pos.brp=p2;}else if(p1.x()>p2.x()&&p1.y()<p2.y()){pos.tlp.setX(p2.x());pos.tlp.setY(p1.y());pos.trp=p1;pos.blp=p2;pos.brp.setX(p1.x());pos.brp.setY(p2.y());}else if(p1.x()<p2.x()&&p1.y()>p2.y()){pos.tlp.setX(p1.x());pos.tlp.setY(p2.y());pos.trp=p2;pos.brp=p1;pos.brp.setX(p2.x());pos.brp.setY(p1.y());}else if(p1.x()>p2.x()&&p1.y()>p2.y()){pos.tlp=p2;pos.trp.setX(p1.x());pos.trp.setY(p2.y());pos.blp.setX(p2.x());pos.blp.setY(p1.y());pos.brp=p1;}
}
void Widget::mouseDoubleClickEvent(QMouseEvent *e)
{if(e->button()==Qt::LeftButton&&(e->globalPos().x()>pos.tlp.x()&&e->globalPos().y()>pos.tlp.y()&&e->globalPos().x()<pos.brp.x()&&e->globalPos().y()<pos.brp.y())){this->saveBoard();}
}
void Widget::emitHelp()
{emit showHelp();
}
void Widget::keyReleaseEvent(QKeyEvent *e)
{switch(e->key()){case Qt::Key_F1:emit emitHelp();break;default:QDialog::keyReleaseEvent(e);break;}
}
void Widget::setMySize(int n1, int n2)
{if(n1!=0){if(pos.tlp.x()+n1>=fullSize.width()){pos.brp.setX(fullSize.width());}else{pos.brp.setX(pos.tlp.x()+n1);}this->figPos(pos.tlp,pos.brp);this->update();}if(n2!=0){if(pos.tlp.y()+n2>=fullSize.height()){pos.brp.setY(fullSize.height());}else{pos.brp.setY(pos.tlp.y()+n2);}this->figPos(pos.tlp,pos.brp);this->update();}
}
void Widget::input()
{InputDialog id(0);connect(&id,SIGNAL(input(int,int)),this,SLOT(setMySize(int,int)));id.exec();
}

 

#ifndef WIDGET_H
#define WIDGET_H#include <QtGui/QDialog>
#include <QSize>
#include <QPoint>
#include <QCursor>
#include <QColor>
class QFile;
class QPixmap;
class QMenu;
class QAction;
class Widget : public QDialog
{Q_OBJECTpublic:struct posInfo{QPoint tlp,trp,blp,brp;};QPoint p1,p2;Widget(QWidget *parent = 0,QColor selectedColor=QColor(0,0,255),int quality=-1);~Widget(){delete screen;delete fogPix;}void reset();
private:QPixmap *screen,*fogPix;posInfo pos;bool rightPressed,leftPressed,done,pull;int moved;bool runOnce;bool bresize;bool showm;QColor sColor;QPoint start;QString fileName;QFile *file;QSize fullSize;QCursor cursor;QMenu *menu;QAction *saveAt,*closeAt,*saveTob,*helpAt,*fixedSizeAt;int quality;void figPos(const QPoint&,const QPoint&);
protected:void paintEvent(QPaintEvent *);void mousePressEvent(QMouseEvent *);void mouseMoveEvent(QMouseEvent *);void mouseReleaseEvent(QMouseEvent *);void keyPressEvent(QKeyEvent *);void mouseDoubleClickEvent(QMouseEvent *);void keyReleaseEvent(QKeyEvent *);
private slots:void saveImage();void saveBoard();void emitHelp();void setMySize(int,int);void input();
signals:void closed();void showHelp();
};#endif // WIDGET_H



其中其它的对话框可以都删掉

 

效果图如下

这篇关于QT 模仿QQ的截图框 支持基本的拖拉 移动 固定大小 可以复制到剪贴板和保存到文件中的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/654642

相关文章

SQL BETWEEN 语句的基本用法详解

《SQLBETWEEN语句的基本用法详解》SQLBETWEEN语句是一个用于在SQL查询中指定查询条件的重要工具,它允许用户指定一个范围,用于筛选符合特定条件的记录,本文将详细介绍BETWEEN语... 目录概述BETWEEN 语句的基本用法BETWEEN 语句的示例示例 1:查询年龄在 20 到 30 岁

mysql中insert into的基本用法和一些示例

《mysql中insertinto的基本用法和一些示例》INSERTINTO用于向MySQL表插入新行,支持单行/多行及部分列插入,下面给大家介绍mysql中insertinto的基本用法和一些示例... 目录基本语法插入单行数据插入多行数据插入部分列的数据插入默认值注意事项在mysql中,INSERT I

mapstruct中的@Mapper注解的基本用法

《mapstruct中的@Mapper注解的基本用法》在MapStruct中,@Mapper注解是核心注解之一,用于标记一个接口或抽象类为MapStruct的映射器(Mapper),本文给大家介绍ma... 目录1. 基本用法2. 常用属性3. 高级用法4. 注意事项5. 总结6. 编译异常处理在MapSt

Java使用HttpClient实现图片下载与本地保存功能

《Java使用HttpClient实现图片下载与本地保存功能》在当今数字化时代,网络资源的获取与处理已成为软件开发中的常见需求,其中,图片作为网络上最常见的资源之一,其下载与保存功能在许多应用场景中都... 目录引言一、Apache HttpClient简介二、技术栈与环境准备三、实现图片下载与保存功能1.

MyBatis ResultMap 的基本用法示例详解

《MyBatisResultMap的基本用法示例详解》在MyBatis中,resultMap用于定义数据库查询结果到Java对象属性的映射关系,本文给大家介绍MyBatisResultMap的基本... 目录MyBATis 中的 resultMap1. resultMap 的基本语法2. 简单的 resul

华为鸿蒙HarmonyOS 5.1官宣7月开启升级! 首批支持名单公布

《华为鸿蒙HarmonyOS5.1官宣7月开启升级!首批支持名单公布》在刚刚结束的华为Pura80系列及全场景新品发布会上,除了众多新品的发布,还有一个消息也点燃了所有鸿蒙用户的期待,那就是Ha... 在今日的华为 Pura 80 系列及全场景新品发布会上,华为宣布鸿蒙 HarmonyOS 5.1 将于 7

Java 枚举的基本使用方法及实际使用场景

《Java枚举的基本使用方法及实际使用场景》枚举是Java中一种特殊的类,用于定义一组固定的常量,枚举类型提供了更好的类型安全性和可读性,适用于需要定义一组有限且固定的值的场景,本文给大家介绍Jav... 目录一、什么是枚举?二、枚举的基本使用方法定义枚举三、实际使用场景代替常量状态机四、更多用法1.实现接

git stash命令基本用法详解

《gitstash命令基本用法详解》gitstash是Git中一个非常有用的命令,它可以临时保存当前工作区的修改,让你可以切换到其他分支或者处理其他任务,而不需要提交这些还未完成的修改,这篇文章主要... 目录一、基本用法1. 保存当前修改(包括暂存区和工作区的内容)2. 查看保存了哪些 stash3. 恢

VS配置好Qt环境之后但无法打开ui界面的问题解决

《VS配置好Qt环境之后但无法打开ui界面的问题解决》本文主要介绍了VS配置好Qt环境之后但无法打开ui界面的问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要... 目UKeLvb录找到Qt安装目录中designer.UKeLvBexe的路径找到vs中的解决方案资源

MySQL基本查询示例总结

《MySQL基本查询示例总结》:本文主要介绍MySQL基本查询示例总结,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录Create插入替换Retrieve(读取)select(确定列)where条件(确定行)null查询order by语句li