该窗口自定义阴影边框、自定义标题栏、可移动。
注:该窗口不可伸缩,目前个人也对无边框窗口使用过各种控制窗口伸缩的方法,和系统提供的窗口伸缩体验相差很多。窗口使用系统提供伸缩、自定义标题栏和可移动可以看另一篇文章 Qt UI设计之《自定义标题栏可移动拖拽窗口》
窗口分为两个控件:主窗口QWidget,标题栏QWidget
1 标题栏相关代码
#pragma once
#include <QWidget>
class UITitlebar : public QWidget
{
Q_OBJECT
public:
UITitlebar(QWidget *parent = Q_NULLPTR);
~UITitlebar();
private:
void initUI();
};
#include "stdafx.h"
#include "UITitlebar.h"
UITitlebar::UITitlebar(QWidget *parent)
: QWidget(parent)
{
this->setAutoFillBackground(true); // 自动刷新背景
QPalette palette(this->palette());
palette.setColor(QPalette::Background, RGB(248, 196, 75));
this->setPalette(palette);
initUI();
}
UITitlebar::~UITitlebar()
{
}
void UITitlebar::initUI()
{
setFixedHeight(40);
QHBoxLayout* _vlayoutMain = new QHBoxLayout(this);
QLabel* _lbeLogo = new QLabel(this);
_lbeLogo->setObjectName("logoLabel");
_lbeLogo->setFixedSize(30, 30);
QPushButton* _btnClose = new QPushButton(this);
_btnClose->setObjectName("closebutton");
_btnClose->setFixedSize(20, 20);
_vlayoutMain->addWidget(_lbeLogo);
_vlayoutMain->addStretch();
_vlayoutMain->addWidget(_btnClose);
_vlayoutMain->setContentsMargins(10, 0, 10, 0);
setLayout(_vlayoutMain);
}
2 主窗口相关代码
#pragma once
#include <QWidget>
class UITestWnd : public QWidget
{
Q_OBJECT
public:
UITestWnd(QWidget *parent = Q_NULLPTR);
~UITestWnd();
protected:
void mouseReleaseEvent(QMouseEvent* event);
void mousePressEvent(QMouseEvent* event);
void mouseMoveEvent(QMouseEvent* event);
void paintEvent(QPaintEvent* event);
private:
void initUI();
bool m_bPressed = true;
QPoint m_point;
bool m_bShadow = true; // 是否边框带阴影
int m_nShadowWidth = 10; // 边框阴影宽度
};
#include "stdafx.h"
#include "UITestWnd.h"
#include "UITitlebar.h"
UITestWnd::UITestWnd(QWidget *parent)
: QWidget(parent)
{
initUI();
}
UITestWnd::~UITestWnd()
{
}
void UITestWnd::initUI()
{
setFixedSize(800, 450);
this->setAttribute(Qt::WA_TranslucentBackground, true);
this->setWindowFlags(Qt::FramelessWindowHint);
// 总布局
QVBoxLayout* _vlayoutMain= new QVBoxLayout(this);
// 标题栏
UITitlebar* _uiTitleBar = new UITitlebar(this);
_vlayoutMain->addWidget(_uiTitleBar);
_vlayoutMain->addStretch();
_vlayoutMain->setContentsMargins(10, 10, 10, 10);
setLayout(_vlayoutMain);
}
void UITestWnd::mouseMoveEvent(QMouseEvent* event)
{
if (m_bPressed)
move(event->pos() - m_point + pos());
}
void UITestWnd::mouseReleaseEvent(QMouseEvent* event)
{
Q_UNUSED(event);
m_bPressed = false;
}
void UITestWnd::mousePressEvent(QMouseEvent* event)
{
if (event->button() == Qt::LeftButton)
{
m_bPressed = true;
m_point = event->pos();
}
}
void UITestWnd::paintEvent(QPaintEvent* event)
{
if (m_bShadow)
{
QPainterPath path;
path.setFillRule(Qt::WindingFill);
path.addRect(m_nShadowWidth, m_nShadowWidth, this->width() - m_nShadowWidth * 2, this->height() - m_nShadowWidth * 2);
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing, true);
painter.fillPath(path, QBrush(Qt::white));
QColor color(0, 128, 192, 50);
for (int i = 0; i < m_nShadowWidth; i++)
{
QPainterPath path;
path.setFillRule(Qt::WindingFill);
path.addRect(m_nShadowWidth - i, m_nShadowWidth - i, this->width() - (m_nShadowWidth - i) * 2, this->height() - (m_nShadowWidth - i) * 2);
color.setAlpha(100 - qSqrt(i) * 50);
painter.setPen(color);
painter.drawPath(path);
}
return;
}
else
return QWidget::paintEvent(event);
}