Please nominate your Qt Champions for 2021!

[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;
    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>(;
    if (!icon.isNull())
        qDebug() << "icon detected";
    icon.paint(painter, QRect(0, 0, 16, 16), Qt::AlignVCenter|Qt::AlignLeft);


    The whole function works as I need but now I am trying to add Icon but:
    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


    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