0

我在显示列表视图时遇到问题。我们为此列表有一个自定义的项目委托,我们在其中覆盖了绘制事件。问题是加载时列表不显示在 UI 上。当我们在列表中生成鼠标按下事件时显示列表。请给出您的想法和解决这个问题的建议。

我正在添加自定义委托代码,以便您更好地理解。

    void CItemDelegate::paint( QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index ) const
{
    painter->save();

    if( option.state & QStyle::State_Sunken )
    {
        painter->fillRect( option.rect, option.palette.highlight() );
    }
    if( m_connectionStatus && ( m_selectedIndex == index.row() ) )
    {
        QPixmap selectionIndicator = icons[10];
        painter->drawPixmap( option.rect.width() - selectionIndicator.width() - 10, option.rect.top() + option.rect.height() / 2 - selectionIndicator.height() / 2, selectionIndicator );
    }

    // Draw the icon
    QVariantList fields = index.data().toList();
    QPixmap  icon = icons[fields[IconIndex].toInt()];

    painter->drawPixmap( 14, option.rect.top() + option.rect.height() / 2 - icon.height() / 2, icon );

    QFont f( painter->font() );
    f.setPixelSize( 23 ) ;
    f.setBold( true );
    painter->setFont( f );

    // Draw the error description text, truncated if necessary
    static const int TextLeftMargin = 80;
    static const int TextTopBottomMargin = 25;

    QString elidedDescription = painter->fontMetrics().elidedText( fields[Description].toString(), Qt::ElideRight, 352 );
    painter->drawText( option.rect.adjusted( TextLeftMargin, TextTopBottomMargin, 0, 0 ),  Qt::AlignLeft, elidedDescription );

    // Divider
    QRect dividerRect = option.rect;
    dividerRect.setTop( dividerRect.bottom() );
    QLinearGradient gradient( 0, 0, option.rect.width() / 2, 0 );
    gradient.setColorAt( 0, QColor( 144, 144, 144, 0 ) );
    gradient.setColorAt( 1, QColor( 144, 144, 144, 255 ) );
    gradient.setSpread( QGradient::ReflectSpread );
    painter->fillRect( dividerRect, gradient );

    painter->restore();


}
4

0 回答 0