2012-03-17 83 views
0

伙计们我根据QStyledItemDelegate实现了我的委托类,我遇到的问题是它不显示listView中显示的文本旁边的复选框。我的代表不显示复选框

在我使用我的委托之前,我在listView中显示了这些复选框,所以我知道这个问题存在于这个委托类中。
有什么想法?

EDIT

void Display_Delegate::paint(QPainter* painter, 
           const QStyleOptionViewItem& option, 
           const QModelIndex &index) const 
{ 
    QString model_data = index.data().toString(); 
    QFontMetrics metrics = view_->fontMetrics(); 
    int view_width = view_->width(); 
    auto modified_str = adjust_text(metrics,model_data,view_width);//this just makes the string to fit into view, don't bother about it. 
    QStyleOptionViewItemV4 style_option = option; 
    initStyleOption(&style_option,index); 
    QPalette::ColorGroup color_group = style_option.state & QStyle::State_Selected ? QPalette::Active : QPalette::Inactive; 
    if (style_option.state & QStyle::State_Selected) 
    { 
     // painter->setPen(style_option.palette.color(color_group, QPalette::Highlight)); 
     painter->setBackgroundMode(Qt::OpaqueMode); 

     QColor color(148,231,245,100); 
     painter->setBackground(QBrush(color)); 
    } 
    else 
    { 
     painter->setPen(style_option.palette.color(color_group, QPalette::Text)); 
    } 

    painter->drawText(option.rect,modified_str); 
} 
+3

我正在考虑一个高大的香蕉代基里。你自己呢?哦,也许是一个有编程问题的人发布代码的梦想世界。 – 2012-03-17 14:08:18

+0

@KerrekSB好的,我会在我的委托中发布paint fnc的代码,没问题。你可能会认为模型和代理模型是正确实现的(它们是,它们和dflt委托一起工作) – smallB 2012-03-17 14:27:48

+0

@KerrekSB有趣的是人们如何快速给予+1,即使这个+1是在我实际编辑我的OP之后给出的,你觉得呢? – smallB 2012-03-17 14:55:52

回答

1
Qt::CheckState QStyleOptionViewItemV4::checkState 

If this view item is checkable, i.e., ViewItemFeature::HasCheckIndicator is true, checkState 
is true if the item is checked; otherwise, it is false. 

我的方法中发现了这个相当晦涩参考具有检查指示器。它说,如果你想让这个项目“可检查”,那么设置这个style option。所以尝试像这样:

style_option.ViewItemFeatures = QStyleOptionViewItemV2::HasCheckIndicator;