行业知识
Qt-demo高级感无边框窗口、美化基础控件
2026-11-11 14:44:52
来源:互联网转载
展示:
代码实现:
// 鼠标按下事件(记录拉伸窗口或移动窗口时的起始坐标(左上角))void framelessWidget::mousePressEvent(QMouseEvent *event){ if(event->button() == Qt::LeftButton){ mousePressed = true;#if (QT_VERSION >= QT_VERSION_CHECK(6,0,0)) lastPos = event->globalPosition().toPoint() - this->frameGeometry().topLeft();#else lastPos = event->globalPos() - this->frameGeometry().topLeft();#endif }}// 鼠标移动事件void framelessWidget::mouseMoveEvent(QMouseEvent *event){ if(event->buttons() == Qt::NoButton) mousePressed = false; if(!mousePressed){ // 鼠标没按下移动,更新鼠标位置状态 mouseState = 0; if(!maximized && abs(event->pos().x() - ui->mainWidget->pos().x()) pos().y() - ui->mainWidget->pos().y()) pos().x() - ui->mainWidget->pos().x() - ui->mainWidget->width()) pos().y() - ui->mainWidget->pos().y() - ui->mainWidget->height()) pos().x() / (double)ui->mainWidget->width(); controlWindowScale();#if (QT_VERSION >= QT_VERSION_CHECK(6,0,0)) this->move(QPoint(event->globalPosition().x() - ui->mainWidget->width() * wRatio, -30));#else this->move(QPoint(event->globalPos().x() - ui->mainWidget->width() * wRatio, -30));#endif lastPos = QPoint(ui->mainWidget->width() * wRatio, event->pos().y()); } else#if (QT_VERSION >= QT_VERSION_CHECK(6,0,0)) this->move(event->globalPosition().toPoint() - lastPos);#else this->move(event->globalPos() - lastPos);#endif } else{#if (QT_VERSION >= QT_VERSION_CHECK(6,0,0)) QPoint d = event->globalPosition().toPoint() - frameGeometry().topLeft() - lastPos;#else QPoint d = event->globalPos() - frameGeometry().topLeft() - lastPos;#endif if(mouseState & AT_LEFT){ this->move(this->frameGeometry().x() + d.x(), this->frameGeometry().y()); this->resize(this->width() - d.x(), this->height()); } if(mouseState & AT_RIGHT){ this->resize(this->width() + d.x(), this->height()); } if(mouseState & AT_TOP){ this->move(this->frameGeometry().x(), this->frameGeometry().y() + d.y()); this->resize(this->width(), this->height() - d.y()); } if(mouseState & AT_BOTTOM){ this->resize(this->width(), this->height() + d.y()); } }#if (QT_VERSION >= QT_VERSION_CHECK(6,0,0)) lastPos = event->globalPosition().toPoint() - this->frameGeometry().topLeft();#else lastPos = event->globalPos() - this->frameGeometry().topLeft();#endif }}// 窗口变化事件void framelessWidget::resizeEvent(QResizeEvent *event){ //Resize border if(border) border->resize(ui->mainWidget->size() + QSize(2, 2)); //Resize mask QPainterPath path;#ifdef Q_OS_WINDOWS path.addRoundedRect(ui->mainWidget->rect(), cornerRadius - 1, cornerRadius - 1);#else path.addRect(ui->mainWidget->rect());#endif QRegion mask(path.toFillPolygon().toPolygon()); ui->mainWidget->setMask(mask); //Resize all pages// for(int i = 0; i resize(ui->mainWidget->width() * 0.4 preferWidth ? pageList[i]->preferWidth - 1 : ui->mainWidget->width() * 0.4 - 1, ui->mainWidget->height());// pageList[i]->resize(pageList[i]->width() + 1, pageList[i]->height());// }}//点击最大化按钮事件void framelessWidget::controlWindowScale(){#ifdef Q_OS_WINDOWS if(!maximized){ lastGeometry = this->frameGeometry(); // windowShadow->setEnabled(false); ui->verticalLayout->setContentsMargins(0, 0, 0, 0); border->hide(); QString mainStyle = "QWidget#mainWidget{background-color:rgb(251,251,251) border-radius:0px;}"; ui->mainWidget->setStyleSheet(mainStyle); this->showMaximized(); maximized = true; QPainterPath path; path.addRect(ui->mainWidget->rect()); QRegion mask(path.toFillPolygon().toPolygon()); ui->mainWidget->setMask(mask); } else{ ui->verticalLayout->setContentsMargins(30, 30, 30, 30); this->showNormal(); QString mainStyle = "QWidget#mainWidget{background-color:rgb(251,251,251)" + QString::asprintf(";border-radius:%dpx", cornerRadius) + "}"; ui->mainWidget->setStyleSheet(mainStyle); QPainterPath path; path.addRoundedRect(ui->mainWidget->rect(), cornerRadius - 1, cornerRadius - 1); QRegion mask(path.toFillPolygon().toPolygon()); ui->mainWidget->setMask(mask); border->show(); // windowShadow->setEnabled(true); this->resize(lastGeometry.width(), lastGeometry.height()); this->move(lastGeometry.x(), lastGeometry.y()); maximized = false; }#endif} 三连可分享全部源码哦
上一篇:C语言之字符数组
下一篇:ps怎么做出光照效果?