PDA

View Full Version : problem of customize the shape of QDialog



julianfox0119
28th December 2010, 05:35
56545654

hello everyone,

i want to customize the shape of the QDialog and found a example for test, the code is:

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.titleBarFlags = Qt::FramelessWindowHint;
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);
}
but there is a gray rect behind the round corner of the titlebar(see attachment), how can i remove(or hide) it?
thanks!

high_flyer
28th December 2010, 09:03
You can set a mask.
http://doc.trolltech.com/4.7/qwidget.html#setMask

julianfox0119
29th December 2010, 02:18
thanks a lot, that helps. but why there is a rect behind the titlebar? i don't really know what is it...
i use "setAttribute(Qt::WA_TranslucentBackground, true);" but failed.

high_flyer
29th December 2010, 08:48
Because QDialog is a top level widget - which means it has not parent widget in which it is embedded, so it has no information about what is behind it.

julianfox0119
30th December 2010, 01:41
aha, thanks for the reply.