PDA

View Full Version : QComboBox text misaligned



jiveaxe
28th March 2013, 14:53
Hi, I just noticed what I consider a sort of bug. I attached an image showing the problem: a label beside a highlighted combobox. As you can see the two text elements aren't aligned, in particular combobox' text is few pixels above the correct position. I'm using qt 4.8.4 on linux.

Can I fix this?

ChrisW67
28th March 2013, 23:45
That would only be a bug if anybody promised that the fine details of unrelated widgets would line up. Qt doesn't make any such promise that I am aware of. It's a mostly impossible promise in the face of user selectable themes and arbitrary programmer style sheets.

You should be able to adjust the location of text inside the label by playing with the top/bottom padding in the style sheet for that widget. You could move the whole label up with bottom margin. You should also check what alignment the model underlying the combo box is returning. It looks like align top left and you might want to try align vcenter left.

jiveaxe
29th March 2013, 11:14
I've solved subclassing QComboBox and reimplementing paintEvent():


void ComboBox::paintEvent(QPaintEvent *e)
{
QVariant itemData = this->itemData( this->currentIndex(), Qt::DisplayRole);

if(itemData.isValid()) {
QStylePainter p(this);
p.setPen(palette().color(QPalette::Text));

QStyleOptionComboBox opt;
initStyleOption(&opt);
p.drawComplexControl(QStyle::CC_ComboBox, opt);

QPainter painter(this);
painter.save();
QRect rect = this->rect();
rect.adjust(5,0,-5,0);

painter.drawText(rect, Qt::AlignLeft | Qt::AlignVCenter, itemData.toString());
painter.restore();
} else {
QComboBox::paintEvent(e);
}
}