this->setWindowFlags(Qt::FramelessWindowHint);//去除Qt自带的标题栏
去除标题栏后为了实现窗体的窗口最大化、最小化、关闭以及移动事件,需要自定义以下操作:
自制QPushButton按钮,连接到showMax()事件上
void QiXin_companyItemClass::showMax()
{
int deskWidth = QApplication::desktop()->availableGeometry().width();
int deskHeight = QApplication::desktop()->availableGeometry().height();//除去任务栏后高度
this->move(QApplication::desktop()->availableGeometry().x(), QApplication::desktop()->availableGeometry().y());
this->resize(deskWidth, deskHeight);
ui->pushButton_2->disconnect();
connect(ui->pushButton_2, SIGNAL(clicked()), this, SLOT(showNorma()));
}
void showNorma()
{
QDesktopWidget* desktopWidget = QApplication::desktop();
int deskWidth = QApplication::desktop()->availableGeometry().width();
int deskHeight = QApplication::desktop()->availableGeometry().height();//除去任务栏后高度
this->resize(deskWidth * 2 / 3, deskWidth * 2*950 / 3/1400);
this->move((desktopWidget->width() - this->width()) / 2, (desktopWidget->height() - this->height()) / 2);
ui->pushButton_2->disconnect();
connect(ui->pushButton_2, SIGNAL(clicked()), this, SLOT(showMax()));
}
自制QPushButton按钮,连接到showMin()事件上
void showMin()
{
this->showMinimized();
}
自制QPushButton按钮,连接到onButtonCloseClicked()事件上
void onButtonCloseClicked()
{
close();
}
重载 mousePressEvent、mouseMoveEvent、mouseReleaseEvent
void mousePressEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event);
void mouseReleaseEvent(QMouseEvent *event);
bool m_isPressed;
QPoint m_startMovePos;
void mousePressEvent(QMouseEvent *event)
{
int x = event->pos().x();
int y = event->pos().y();
if (x >= 0 && x <= ui.widget->width())
{
if (y >= 0 && y <= ui.widget->height())
{
m_isPressed = true;
m_startMovePos = event->pos();
}
}
return QWidget::mousePressEvent(event);
}
void mouseMoveEvent(QMouseEvent *event)
{
if (m_isPressed)
{
QPoint movePoint = event->globalPos();
//m_startMovePos = event->globalPos();
this->move(movePoint.x() - m_startMovePos.x(), movePoint.y() - m_startMovePos.y());
}
return QWidget::mouseMoveEvent(event);
}
void mouseReleaseEvent(QMouseEvent *event)
{
m_isPressed = false;
return QWidget::mouseReleaseEvent(event);
}
this->setAttribute(Qt::WA_TranslucentBackground);//设置窗口背景透明
重载paintEvent,窗体要在四周留出10的间距,其窗体要设置圆角样式 border-radius:5px;
void paintEvent(QPaintEvent *event)
{
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing); // 反锯齿;
QColor tem;
tem.setRgb(47, 47, 47);
painter.setPen(Qt::transparent);
QRect rect = ui.widget_2->rect();
rect.setWidth(rect.width() - 1);
rect.setHeight(rect.height() - 1);
painter.drawRoundedRect(rect, 10, 10);//绘制圆角
//绘制阴影
QPainterPath path;
path.setFillRule(Qt::WindingFill);
path.addRect(10, 10, this->width() - 20, this->height() - 20);
painter.setRenderHint(QPainter::Antialiasing, true);
painter.fillPath(path, QBrush(QColor(45, 45, 45)));
QColor color(0, 0, 0, 50);
for (int i = 0; i<10; i++)
{
QPainterPath path;
path.setFillRule(Qt::WindingFill);
path.addRect(10 - i, 10 - i, this->width() - (10 - i) * 2, this->height() - (10 - i) * 2);
color.setAlpha(150 - sqrt(i) * 50);
painter.setPen(color);
painter.drawPath(path);
}
QWidget::paintEvent(event);
}