QLabel isnt transparent when rendered on item delegate (QStyledItemDelegate)



  • i have strange problem i have item delegate inherit from QStyledItemDelegate
    the background color is gradient color that looks like this :
    @void ItemDelegate::paintActiveOverlay( QPainter *painter, qreal x, qreal y, qreal w, qreal h ) const {

    QPalette palette;
    QColor highlightColor = palette.color(QPalette::Highlight);
    QColor backgroundColor = palette.color(QPalette::Base);
    const float animation = 0.25;
    const int gradientRange = 16;
    
    QColor color2 = QColor::fromHsv(
            highlightColor.hue(),
            (int) (backgroundColor.saturation() * (1.0f - animation) + highlightColor.saturation() * animation),
            (int) (backgroundColor.value() * (1.0f - animation) + highlightColor.value() * animation)
            );
    QColor color1 = QColor::fromHsv(
            color2.hue(),
            qMax(color2.saturation() - gradientRange, 0),
            qMin(color2.value() + gradientRange, 255)
            );
    QRect rect((int) x, (int) y, (int) w, (int) h);
    painter->save();
    painter->setPen(Qt::NoPen);
    QLinearGradient linearGradient(0, 0, 0, rect.height());
    linearGradient.setColorAt(0.0, color1);
    linearGradient.setColorAt(1.0, color2);
    painter->setBrush(linearGradient);
    painter->drawRect(rect);
    painter->restore();
    

    }

    @

    its called from the paint method
    also in the ItemDelegate constructor i have set QLable like this :

    @QRect rect(40, 30, 401, 31);
    Qt::TextInteractionFlags flags = Qt::LinksAccessibleByKeyboard|Qt::LinksAccessibleByMouse|Qt::TextBrowserInteraction|Qt::TextSelectableByKeyboard|Qt::TextSelectableByMouse;
    Qt::TextFormat txtFormat = Qt::PlainText;

    pTextEdit_title = new QLabel();
    pTextEdit_title->setTextFormat(txtFormat);
    pTextEdit_title->setTextInteractionFlags(flags);
    pTextEdit_title->setGeometry(rect);@

    and in the paint method of the ItemDelegate i set the Qlable to render like this :
    @pTextEdit_title->setText(Title);
    QRect TextEditRect(option.rect.x()+THUMB_WIDTH+THUMB_WIDTH+PADDING, option.rect.y() ,
    pTextEdit_title->width(), pTextEdit_title->height());
    QPixmap pixmap(pTextEdit_title->size());
    pTextEdit_title->render(&pixmap);
    painter->drawPixmap(TextEditRect,pixmap);
    @

    it render the QLable file , but the problem is that it has gray background and dosnt act as transparent background , my question is how to set the QLable background to be transparent?


Log in to reply