Option One
Realized by code, first remove the window frame, and then draw on PaintEvent.
MainWidget::MainWidget(QWidget *parent)
: QWidget(parent)
{
resize(600, 400);
setAttribute(Qt::WA_TranslucentBackground); //设置窗口背景透明
setWindowFlags(Qt::FramelessWindowHint | Qt::WindowMinMaxButtonsHint); //去掉窗口边框
}
void MainWidget::paintEvent(QPaintEvent* event)
{
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing); // 反锯齿;
painter.setBrush(QBrush(QColor(255, 255, 255)));
painter.setPen(Qt::transparent);
QRect rect = this->rect();
painter.drawRoundedRect(rect, 15, 15); //设置窗口圆角 15px
}
Option II
Realized by code, first remove the window border, complete by setting the style, and then also draw on PaintEvent.
MainWidget::MainWidget(QWidget *parent)
: QWidget(parent)
{
setAttribute(Qt::WA_TranslucentBackground); //设置窗口背景透明
setWindowFlags(Qt::FramelessWindowHint | Qt::WindowMinMaxButtonsHint); //去掉窗口边框
//this->setStyleSheet("QWidget{background-color:#FFFFFF;border-radius:30px;}");
this->setStyleSheet("QWidget{background-color:#FFFFFF; \
border-top-left-radius:15px; \
border-bottom-right-radius:15px; \
}");
}
void MainWidget::paintEvent(QPaintEvent*)
{
QStyleOption opt;
opt.init(this);
QPainter p(this);
style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}
Well, the above are the two ways we implemented, everyone can use it according to their own situation.