2011-04-05 244 views
4

我想提請代表,其工作只是我無法弄清楚如何繪製inital的文字是這樣的組合框內可見細小內QComboBox任何影響。設置QStyleOptionComboBox.currentText不會對繪製控件

文檔說QStyleOptionComboBox.currentText認爲:「組合框的當前項目的文本。」但設置變量沒有任何作用。

這是我的代碼:

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); 
} 

看着qwindowsxpstyle.cpp我沒有看到一個「真實」的組合框的文本,因爲currentText畫是不是drawComplexControl方法內部使用。它似乎要用於Windows XP樣式的唯一地方是在qcommonstyle.cpp(線2107,Qt的4.7.2),但我無法弄清楚這兩個類是如何一起玩。

回答

6

看來你還需要給力的Qt繪製組合框的標籤,除了複雜的控制。試試這個:

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

如果我正確地閱讀了文檔和源代碼,可能會強制QStyle繪製組合框標籤。看起來很奇怪,你必須指定兩者......但我不太瞭解Qt風格如何繪製自己,說實話。

+0

沒問題!儘管如此,你知道這實際上不會給你一個可編輯的QComboBox,對吧?它只會在屏幕上繪製組合框的像素。如果你想要一個完全可編輯的組合框,你應該看看QItemDelegate :: createEditor()。我之前使用它來實現QTreeView中項目的內嵌編輯。它工作得很好。 – Adam 2011-04-06 11:24:59

+2

是的,我也使用'createEditor()'。我正在攻擊QComboBox應該始終可見的問題,不僅在編輯模式下。畫這不是最好的解決方案。 'QAbstractItemView :: openPersistentEditor()'完成這項工作! – WolfgangA 2011-04-06 17:35:44