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

相关文章

QT Creator配置Kit的实现示例

《QTCreator配置Kit的实现示例》本文主要介绍了使用Qt5.12.12与VS2022时,因MSVC编译器版本不匹配及WindowsSDK缺失导致配置错误的问题解决,感兴趣的可以了解一下... 目录0、背景:qt5.12.12+vs2022一、症状:二、原因:(可以跳过,直奔后面的解决方法)三、解决方

Python ORM神器之SQLAlchemy基本使用完全指南

《PythonORM神器之SQLAlchemy基本使用完全指南》SQLAlchemy是Python主流ORM框架,通过对象化方式简化数据库操作,支持多数据库,提供引擎、会话、模型等核心组件,实现事务... 目录一、什么是SQLAlchemy?二、安装SQLAlchemy三、核心概念1. Engine(引擎)

Python异步编程之await与asyncio基本用法详解

《Python异步编程之await与asyncio基本用法详解》在Python中,await和asyncio是异步编程的核心工具,用于高效处理I/O密集型任务(如网络请求、文件读写、数据库操作等),接... 目录一、核心概念二、使用场景三、基本用法1. 定义协程2. 运行协程3. 并发执行多个任务四、关键

Go语言连接MySQL数据库执行基本的增删改查

《Go语言连接MySQL数据库执行基本的增删改查》在后端开发中,MySQL是最常用的关系型数据库之一,本文主要为大家详细介绍了如何使用Go连接MySQL数据库并执行基本的增删改查吧... 目录Go语言连接mysql数据库准备工作安装 MySQL 驱动代码实现运行结果注意事项Go语言执行基本的增删改查准备工作

SQL Server 查询数据库及数据文件大小的方法

《SQLServer查询数据库及数据文件大小的方法》文章介绍了查询数据库大小的SQL方法及存储过程实现,涵盖当前数据库、所有数据库的总大小及文件明细,本文结合实例代码给大家介绍的非常详细,感兴趣的... 目录1. 直接使用SQL1.1 查询当前数据库大小1.2 查询所有数据库的大小1.3 查询每个数据库的详

DNS查询的利器! linux的dig命令基本用法详解

《DNS查询的利器!linux的dig命令基本用法详解》dig命令可以查询各种类型DNS记录信息,下面我们将通过实际示例和dig命令常用参数来详细说明如何使用dig实用程序... dig(Domain Information Groper)是一款功能强大的 linux 命令行实用程序,通过查询名称服务器并输

Qt中实现多线程导出数据功能的四种方式小结

《Qt中实现多线程导出数据功能的四种方式小结》在以往的项目开发中,在很多地方用到了多线程,本文将记录下在Qt开发中用到的多线程技术实现方法,以导出指定范围的数字到txt文件为例,展示多线程不同的实现方... 目录前言导出文件的示例工具类QThreadQObject的moveToThread方法实现多线程QC

把Python列表中的元素移动到开头的三种方法

《把Python列表中的元素移动到开头的三种方法》在Python编程中,我们经常需要对列表(list)进行操作,有时,我们希望将列表中的某个元素移动到最前面,使其成为第一项,本文给大家介绍了把Pyth... 目录一、查找删除插入法1. 找到元素的索引2. 移除元素3. 插入到列表开头二、使用列表切片(Lis

使用Python开发一个Ditto剪贴板数据导出工具

《使用Python开发一个Ditto剪贴板数据导出工具》在日常工作中,我们经常需要处理大量的剪贴板数据,下面将介绍如何使用Python的wxPython库开发一个图形化工具,实现从Ditto数据库中读... 目录前言运行结果项目需求分析技术选型核心功能实现1. Ditto数据库结构分析2. 数据库自动定位3

MySql基本查询之表的增删查改+聚合函数案例详解

《MySql基本查询之表的增删查改+聚合函数案例详解》本文详解SQL的CURD操作INSERT用于数据插入(单行/多行及冲突处理),SELECT实现数据检索(列选择、条件过滤、排序分页),UPDATE... 目录一、Create1.1 单行数据 + 全列插入1.2 多行数据 + 指定列插入1.3 插入否则更