PDA

View Full Version : border-radius doesn't clip the background using QLabel::render()



mritalian
13th April 2011, 21:10
Hi,

I'm having trouble using border-radius, in that it doesn't clip the background.

I am using QLabel as a renderer onto a listview using a delegate:


void MyDelegate::paint( QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const {

const QWidget *widget = opt.widget;
QStyle *style = widget ? widget->style() : QApplication::style();
QRect textRect = style->subElementRect(QStyle::SE_ItemViewItemText, &opt);

QLabel lbl;
lbl.setObjectName("Godzilla");
painter->save();
painter->translate(textRect.topLeft());
lbl.setText("Godzilaaaaaaaaaaaaa");
lbl.render(painter);
painter->restore();

}

My stylesheet is:


#Godzilla {
border-radius: 5px;
background: rgba(255,0,0,80);
border: 2px solid red;
color: white;
font-weight: bold;
font-size: 10px;
}


The stylesheet is loaded using qApp->setStyleSheet(..);
The style applies (color, border, background), but, the background doesn't clip.

I've tried using


setAttribute(Qt::WA_NoSystemBackground,true);

but it just prevents the background and borders from drawing at all.

I am using Qt 4.7 on Windows XP.

TIA

mritalian
13th April 2011, 23:11
Found the cause!


lbl.render(painter);

The prototype for QWidget::render is:



void render(QPainter *painter, const QPoint &targetOffset = QPoint(),
const QRegion &sourceRegion = QRegion(),
RenderFlags renderFlags = DrawWindowBackground | DrawChildren));


So the fix is (obviously)


lbl.render(painter, QPoint(), QRegion(), 0);