2012-10-03 35 views
1

I SubClassed QComboBox以定製它。我編寫了下面的paintEvent()方法來測試,以在我開始定製之前看到我的樣式表正在被正確應用。但是,如果我使用這個paintEvent,選定的項目不會被顯示(已經選擇的項目 - 或者我新選擇的項目)。如何爲SubClassed QComboBox編寫paintEvent()

class MyComboBox: public QComboBox 
{ 
    Q_OBJECT 
public: 
    MyComboBox(QWidget *parent = 0): QComboBox(parent){} 

    virtual void paintEvent(QPaintEvent* pEvent) 
    { 
    QStylePainter painter(this); 
    QStyleOptionComboBox opt; 
    initStyleOption(&opt); 
    painter.drawComplexControl(QStyle::CC_ComboBox, opt); 
    QVariant itemData = this->itemData(this->currentIndex(), Qt::DisplayRole); 
    if(!itemData.isNull() && qVariantCanConvert<QPen>(itemData)) 
     style()->drawItemText(&painter,this->rect(),Qt::AlignLeft,(this->palette()), true, itemData.toString()); 
    } 
}; 

如何修復paintEvent?另外,是否有關於不同Qt控件的paintEvent文檔?

+1

嘗試添加'QComboBox :: paintEvent(pEvent)'到函數的末尾。 – hank

+0

@hank這會實現什麼? OP已經明確地繪製了組合框。 – cmannett85

+0

@ go4sri你能描述一下究竟是什麼_is_繪製? (甚至更好,張貼圖片)。 – cmannett85

回答

0

我認爲你的問題在於你重寫了基類方法,而不是先在你的派生類中調用它。你應該在你的派生類方法中調用它