2017-09-01 86 views
0

我有它的代理類和方法的油漆。我想顯示在QTreeWidget列的組合框,做它QTreeWidget不顯示QCombobox代理

ui->treeWidget->setItemDelegateForColumn(2, box); 

其中盒是我委託的對象

paint方法是

void ComboboxDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const 
{ 
    QStyleOptionComboBox box; 

    QString text = values[1]; 

    box.currentText = text; 
    box.rect = option.rect; 

    QApplication::style()->drawComplexControl(QStyle::CC_ComboBox, &box, painter, 0); 
    QApplication::style()->drawControl(QStyle::CE_ComboBoxLabel, &box, painter, 0); 
} 

值與QMAP兩個變量 - 「你好」和「再見」

但不是繪製組合框treewidget只顯示第二列中的「Hello」字符串 爲什麼? 我該如何解決它?

+0

我想你的代碼,它工作正常。也許顯示器的亮度有問題?你使用的是哪個版本的Qt? – olya

+0

查看https://doc.qt.io/qt-5/qtreewidget.html#setItemWidget - 這允許直接放置小部件而不是使用自定義委託,但僅適用於treewidgets – Felix

+0

謝謝大家答案,我猜這個問題開始於不正確的qss。但我不知道如何爲QStyleOptionComboBox或ComboboxDelegate編寫正確的qss。 –

回答

0

如果你有QSS文件,並希望自定義您的QComboBox代表你應該在paint()方法添加一些組合框,並替換:

QApplication::style()->drawComplexControl(QStyle::CC_ComboBox, &box, painter, 0); 

到:

QComboBox tmp; 
QApplication::style()->drawComplexControl(QStyle::CC_ComboBox, &box, painter, &tmp);