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

相关文章

redis中使用lua脚本的原理与基本使用详解

《redis中使用lua脚本的原理与基本使用详解》在Redis中使用Lua脚本可以实现原子性操作、减少网络开销以及提高执行效率,下面小编就来和大家详细介绍一下在redis中使用lua脚本的原理... 目录Redis 执行 Lua 脚本的原理基本使用方法使用EVAL命令执行 Lua 脚本使用EVALSHA命令

Qt实现网络数据解析的方法总结

《Qt实现网络数据解析的方法总结》在Qt中解析网络数据通常涉及接收原始字节流,并将其转换为有意义的应用层数据,这篇文章为大家介绍了详细步骤和示例,感兴趣的小伙伴可以了解下... 目录1. 网络数据接收2. 缓冲区管理(处理粘包/拆包)3. 常见数据格式解析3.1 jsON解析3.2 XML解析3.3 自定义

C++如何通过Qt反射机制实现数据类序列化

《C++如何通过Qt反射机制实现数据类序列化》在C++工程中经常需要使用数据类,并对数据类进行存储、打印、调试等操作,所以本文就来聊聊C++如何通过Qt反射机制实现数据类序列化吧... 目录设计预期设计思路代码实现使用方法在 C++ 工程中经常需要使用数据类,并对数据类进行存储、打印、调试等操作。由于数据类

Qt中QGroupBox控件的实现

《Qt中QGroupBox控件的实现》QGroupBox是Qt框架中一个非常有用的控件,它主要用于组织和管理一组相关的控件,本文主要介绍了Qt中QGroupBox控件的实现,具有一定的参考价值,感兴趣... 目录引言一、基本属性二、常用方法2.1 构造函数 2.2 设置标题2.3 设置复选框模式2.4 是否

QT进行CSV文件初始化与读写操作

《QT进行CSV文件初始化与读写操作》这篇文章主要为大家详细介绍了在QT环境中如何进行CSV文件的初始化、写入和读取操作,本文为大家整理了相关的操作的多种方法,希望对大家有所帮助... 目录前言一、CSV文件初始化二、CSV写入三、CSV读取四、QT 逐行读取csv文件五、Qt如何将数据保存成CSV文件前言

Qt中QUndoView控件的具体使用

《Qt中QUndoView控件的具体使用》QUndoView是Qt框架中用于可视化显示QUndoStack内容的控件,本文主要介绍了Qt中QUndoView控件的具体使用,具有一定的参考价值,感兴趣的... 目录引言一、QUndoView 的用途二、工作原理三、 如何与 QUnDOStack 配合使用四、自

MySQL 中的 LIMIT 语句及基本用法

《MySQL中的LIMIT语句及基本用法》LIMIT语句用于限制查询返回的行数,常用于分页查询或取部分数据,提高查询效率,:本文主要介绍MySQL中的LIMIT语句,需要的朋友可以参考下... 目录mysql 中的 LIMIT 语句1. LIMIT 语法2. LIMIT 基本用法(1) 获取前 N 行数据(

Qt spdlog日志模块的使用详解

《Qtspdlog日志模块的使用详解》在Qt应用程序开发中,良好的日志系统至关重要,本文将介绍如何使用spdlog1.5.0创建满足以下要求的日志系统,感兴趣的朋友一起看看吧... 目录版本摘要例子logmanager.cpp文件main.cpp文件版本spdlog版本:1.5.0采用1.5.0版本主要

Pyserial设置缓冲区大小失败的问题解决

《Pyserial设置缓冲区大小失败的问题解决》本文主要介绍了Pyserial设置缓冲区大小失败的问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面... 目录问题描述原因分析解决方案问题描述使用set_buffer_size()设置缓冲区大小后,buf

MySQL更新某个字段拼接固定字符串的实现

《MySQL更新某个字段拼接固定字符串的实现》在MySQL中,我们经常需要对数据库中的某个字段进行更新操作,本文就来介绍一下MySQL更新某个字段拼接固定字符串的实现,感兴趣的可以了解一下... 目录1. 查看字段当前值2. 更新字段拼接固定字符串3. 验证更新结果mysql更新某个字段拼接固定字符串 -