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

。_饼干妹妹 提交于 2019-12-22 05:59:26

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/5549577/setting-qstyleoptioncombobox-currenttext-does-not-have-any-effect-on-the-drawn-w

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!