PDA

View Full Version : stylesheet to QDialog Title Bar



srinirc
24th November 2009, 13:18
Hi
How to apply stylesheet to QDialog Title Bar?

yogeshgokul
24th November 2009, 13:57
Hi
How to apply stylesheet to QDialog Title Bar?
You cant do it, using style sheet. Use QStyle instead.

zuck
24th November 2009, 15:32
You must create a custom class which inherits by QDialog and then re-implement the "paintEvent" method with something similar to this:



MyDialog::MyDialog(QWidget* parent) : QDialog(parent, Qt::FramelessWindowHint) {}

// ...

void MyDialog::paintEvent(QPaintEvent* event)
{
Q_UNUSED(event)

QPainter p(this);
QStyle* style = this->style();
QRect active_area = this->rect();
int titlebar_height = 0;

// Titlebar.
QStyleOptionTitleBar t_opt;
t_opt.initFrom(this);

titlebar_height = style->pixelMetric(QStyle::PM_TitleBarHeight, &t_opt, this);

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, &p, this);
style->drawItemText(&p, 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;
style->drawPrimitive(QStyle::PE_Widget, &w_opt, &p, this);
}


Now you can style it with a stylesheet:



MyDialog::title {
height: 24px;
font-weight: bold;
color: #000000;
background: #ffffff;
}