[Solved] Qt5.1 QStyledItemDelegate problem with QIcon



  • Hello,

    I created item delegate with syntax highlighting (it itherits from QStyledItemDelegate):
    @void HihghlighterDelegate::paint ( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const
    {
    if (option.state & QStyle::State_Selected)
    {
    QLinearGradient gradient(option.rect.topLeft(), option.rect.bottomRight()); // diagonal gradient from top-left to bottom-right
    gradient.setColorAt(0, QColor(200, 200, 200, 255));
    gradient.setColorAt(1, QColor(225, 225, 225, 255));
    painter->fillRect(option.rect, gradient);
    }
    else if (option.state & QStyle::State_MouseOver)
    painter->fillRect(option.rect, option.palette.midlight());

    QStyleOptionViewItemV4 options = option;
    options.rect.setTop(options.rect.y()-4);
    
    painter->save();
    
    m_textDocument->setPlainText(index.data(Qt::DisplayRole).toString());
    
    painter->translate(options.rect.left(), options.rect.top());
    QRect clip(0, 0, options.rect.width(), options.rect.height());
    m_textDocument->drawContents(painter, clip);
    
    //QIcon icon = QIcon(":/icons/LED_OFF.png");
    QIcon icon = QIcon(qvariant_cast<QPixmap>(index.data(Qt::DecorationRole)));
    if (!icon.isNull())
        qDebug() << "icon detected";
    icon.paint(painter, QRect(0, 0, 16, 16), Qt::AlignVCenter|Qt::AlignLeft);
    
    painter->restore();
    

    }
    @

    The whole function works as I need but now I am trying to add Icon but:
    QIcon(qvariant_cast<QPixmap>(index.data(Qt::DecorationRole)));
    seems to doesn't work but commented QIcon from resources works ok. Do you have any ideas what I am doing wrong?

    When I am not using custom delegate then icons are visible.



  • Sorry I am blind
    I was making qvariant from QPixmap instead of QIcon :)

    quick copy paste is bad idea sometimes.


  • Lifetime Qt Champion

    Hi,

    Great you found out !

    Can you also update the thread title prepending [solved] ? So other forum users may know a solution has been found :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.