最近在尝试自定义窗体,在网上看了一个例子,决定自己试试.效果基本出来了.但是titlebar的圆角后面还有一个背景的矩形尖角不知道如何才能去掉,希望大家帮忙,谢谢.
代码如下:
void customDlg::paintEvent(QPaintEvent* event)
{
Q_UNUSED(event)
QPainter painter(this);
QStyle* style = this->style();
QRect active_area = this->rect();
qDebug() << active_area;
int titlebar_height = 0;
// Titlebar.
QStyleOptionTitleBar t_opt;
t_opt.initFrom(this);
titlebar_height = style->pixelMetric(QStyle::PM_TitleBarHeight, &t_opt, this);
qDebug() << titlebar_height;
t_opt.rect = QRect(0, 0, this->width(), titlebar_height);
t_opt.titleBarState = this->windowState();
t_opt.text = t_opt.fontMetrics.elidedText(this->windowTitle(), Qt::ElideRight, t_opt.rect.width());
style->drawComplexControl(QStyle::CC_TitleBar, &t_opt, &painter, this);
style->drawItemText(&painter, t_opt.rect, Qt::AlignCenter, t_opt.palette, true, t_opt.text, QPalette::ToolTipText);
// Background widget.
active_area.setTopLeft(QPoint(0, titlebar_height));
this->setContentsMargins(0, titlebar_height, 0, 0);
QStyleOption w_opt;
w_opt.initFrom(this);
w_opt.rect = active_area;
qDebug() << w_opt.rect;
style->drawPrimitive(QStyle::PE_Widget, &w_opt, &painter, this);
}
ps:效果如附件图片所示