QTreeWidget不显示QCombobox代理

问题描述:

我有它的代理类和方法的油漆。我想显示在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。 –

如果你有QSS文件,并希望自定义您的QComboBox代表你应该在paint()方法添加一些组合框,并替换:

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

到:

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