您好,欢迎访问三七文档
当前位置:首页 > 临时分类 > QDialog可移动可变化大小及背景设置
添加定义#definePADDING2enumDirection{UP=0,DOWN=1,LEFT,RIGHT,LEFTTOP,LEFTBOTTOM,RIGHTBOTTOM,RIGHTTOP,NONE};1、在头文件中类添加函数和变量voidmouseReleaseEvent(QMouseEvent*event);voidmouseMoveEvent(QMouseEvent*event);voidmousePressEvent(QMouseEvent*event);voidregion(constQPoint&cursorGlobalPoint);//鼠标全局位置boolisLeftPressDown;//判断左键是否按下QPointdragPosition;//窗口移动拖动时需要记住的点Directiondir;//窗口大小改变时,记录改变方向2、在main函数中添加this-setWindowFlags(Qt::FramelessWindowHint|Qt::WindowSystemMenuHint);//隐藏Dialog标题栏this-setStyleSheet(QDialog{background-color:#FFFFFF}QDialog{border:2pxgroovegray;border-radius:10px;padding:2px4px});//设置Dialog背景为白色,边框为圆角this-setMinimumHeight(480);this-setMinimumWidth(1250);isLeftPressDown=false;this-dir=NONE;this-setMouseTracking(true);//追踪鼠标3、实现方法voidQDataTableDlg::region(constQPoint&cursorGlobalPoint){QRectrect=this-rect();QPointtl=mapToGlobal(rect.topLeft());QPointrb=mapToGlobal(rect.bottomRight());intx=cursorGlobalPoint.x();inty=cursorGlobalPoint.y();if(tl.x()+PADDING=x&&tl.x()=x&&tl.y()+PADDING=y&&tl.y()=y){//左上角dir=LEFTTOP;this-setCursor(QCursor(Qt::SizeFDiagCursor));//设置鼠标形状}elseif(x=rb.x()-PADDING&&x=rb.x()&&y=rb.y()-PADDING&&y=rb.y()){//右下角dir=RIGHTBOTTOM;this-setCursor(QCursor(Qt::SizeFDiagCursor));}elseif(x=tl.x()+PADDING&&x=tl.x()&&y=rb.y()-PADDING&&y=rb.y()){//左下角dir=LEFTBOTTOM;this-setCursor(QCursor(Qt::SizeBDiagCursor));}elseif(x=rb.x()&&x=rb.x()-PADDING&&y=tl.y()&&y=tl.y()+PADDING){//右上角dir=RIGHTTOP;this-setCursor(QCursor(Qt::SizeBDiagCursor));}elseif(x=tl.x()+PADDING&&x=tl.x()){//左边dir=LEFT;this-setCursor(QCursor(Qt::SizeHorCursor));}elseif(x=rb.x()&&x=rb.x()-PADDING){//右边dir=RIGHT;this-setCursor(QCursor(Qt::SizeHorCursor));}elseif(y=tl.y()&&y=tl.y()+PADDING){//上边dir=UP;this-setCursor(QCursor(Qt::SizeVerCursor));}elseif(y=rb.y()&&y=rb.y()-PADDING){//下边dir=DOWN;this-setCursor(QCursor(Qt::SizeVerCursor));}else{//默认dir=NONE;this-setCursor(QCursor(Qt::ArrowCursor));}}voidQDataTableDlg::mouseReleaseEvent(QMouseEvent*event){if(event-button()==Qt::LeftButton){isLeftPressDown=false;if(dir!=NONE){this-releaseMouse();this-setCursor(QCursor(Qt::ArrowCursor));}}}voidQDataTableDlg::mousePressEvent(QMouseEvent*event){switch(event-button()){caseQt::LeftButton:isLeftPressDown=true;if(dir!=NONE){this-mouseGrabber();}else{dragPosition=event-globalPos()-this-frameGeometry().topLeft();}break;caseQt::RightButton://this-close();break;default:QDialog::mousePressEvent(event);}}voidQDataTableDlg::mouseMoveEvent(QMouseEvent*event){QPointgloPoint=event-globalPos();QRectrect=this-rect();QPointtl=mapToGlobal(rect.topLeft());QPointrb=mapToGlobal(rect.bottomRight());if(!isLeftPressDown){this-region(gloPoint);}else{if(dir!=NONE){QRectrMove(tl,rb);switch(dir){caseLEFT:if(rb.x()-gloPoint.x()=this-minimumWidth())rMove.setX(tl.x());elserMove.setX(gloPoint.x());break;caseRIGHT:rMove.setWidth(gloPoint.x()-tl.x());break;caseUP:if(rb.y()-gloPoint.y()=this-minimumHeight())rMove.setY(tl.y());elserMove.setY(gloPoint.y());break;caseDOWN:rMove.setHeight(gloPoint.y()-tl.y());break;caseLEFTTOP:if(rb.x()-gloPoint.x()=this-minimumWidth())rMove.setX(tl.x());elserMove.setX(gloPoint.x());if(rb.y()-gloPoint.y()=this-minimumHeight())rMove.setY(tl.y());elserMove.setY(gloPoint.y());break;caseRIGHTTOP:rMove.setWidth(gloPoint.x()-tl.x());rMove.setY(gloPoint.y());break;caseLEFTBOTTOM:rMove.setX(gloPoint.x());rMove.setHeight(gloPoint.y()-tl.y());break;caseRIGHTBOTTOM:rMove.setWidth(gloPoint.x()-tl.x());rMove.setHeight(gloPoint.y()-tl.y());break;default:break;}this-setGeometry(rMove);}else{move(event-globalPos()-dragPosition);event-accept();}}QDialog::mouseMoveEvent(event);}
本文标题:QDialog可移动可变化大小及背景设置
链接地址:https://www.777doc.com/doc-2854287 .html