Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved] Qt5.1 QStyledItemDelegate problem with QIcon
Forum Updated to NodeBB v4.3 + New Features

[Solved] Qt5.1 QStyledItemDelegate problem with QIcon

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 2.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    nibbit
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    1
    • N Offline
      N Offline
      nibbit
      wrote on last edited by
      #2

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

      quick copy paste is bad idea sometimes.

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        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 :)

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved