Setting QStyleOptionComboBox.currentText does not affect the drawn widget

I want to draw a QComboBox inside a delegate that works great, except that I cannot figure out how to draw inline text visible inside the combo box.

The documentation states that QStyleOptionComboBox.currentText contains: "text for the current element of the combo box." but setting a variable has no 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 the โ€œrealโ€ combined field is drawn, since currentText not used inside the drawComplexControl method. The only place it seems to be used for the Windows XP style is in qcommonstyle.cpp (line 2107, Qt 4.7.2), but I can't figure out how these two classes play together.

+4
source share
1 answer

It seems you also need to get Qt to draw a shortcut with the list in addition to the complex controls. Try the following:

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

If I read the documentation and the source correctly, to get QStyle to draw a shortcut with a list. It seems strange that you will need to specify both of them ... but I donโ€™t know much about how Qt styles draw themselves, to be honest.

+6
source

Source: https://habr.com/ru/post/1346697/


All Articles