Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting QStyleOptionComboBox.currentText does not have any effect on the drawn widget

Tags:

qt

qcombobox

I want to draw a QComboBox inside a delegate, which works fine except that I can't figure out how to draw the inital text that's visible inside the combo box.

The documentation says that QStyleOptionComboBox.currentText holds: "the text for the current item of the combo box." but setting the variable does not have any effect.

This is my code:

void MyDelegate::paint(QPainter *painter,
    const QStyleOptionViewItem& option,
    const QModelIndex& index) const
{
    QStyleOptionComboBox comboBoxOption;
    comboBoxOption.rect = option.rect; 
    comboBoxOption.state = option.state;
    comboBoxOption.state |= QStyle::State_Enabled;
    comboBoxOption.editable = false;
    comboBoxOption.currentText = "CCC"; // This doesn't show up.

    QApplication::style()->drawComplexControl(QStyle::CC_ComboBox, &comboBoxOption, painter);
}

Looking at qwindowsxpstyle.cpp I don't see where the text of a "real" combo box is drawn since currentText is not used inside the drawComplexControl method. The only place where it seems to be used for Windows XP style is in qcommonstyle.cpp (Line 2107, Qt 4.7.2), but I can't figure out how those two classes play together.

like image 315
WolfgangP Avatar asked Apr 05 '11 09:04

WolfgangP


1 Answers

It seems you also need to force Qt to draw the combo box label, in addition to the complex control. Try this:

QApplication::style()->drawControl(QStyle::CE_ComboBoxLabel, &comboBoxOption, painter)

If I read the documentation, and source, correctly that might force QStyle to draw a combo box label. It seems odd that you'd have to specify both...but I don't know a whole lot about how Qt styles draw themselves, to be honest.

like image 175
Adam Avatar answered Oct 21 '22 12:10

Adam