Transparent color in Qt



  • I have a requirement in which I have to show a transparent colored rectangle in Qt for QtreeView can some one guide me for that


  • Moderators

    @Qt-Enthusiast
    sorry, but you need to be more specific.
    You mean you want to paint a transparent filled rect (with border) on your tree view?



  • @raven-worx said:

    ou mean you want to paint a transparent filled rect (with border) on your tree view?

    Yes I mean I want to show a rectangle with transparent filled rect (with border) in one of the columns of my QTreeView tree view?


  • Moderators

    @Qt-Enthusiast
    implement a custom QStyledItemDelegate subclass and reimplement the paint method:

    void MyItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
    {
        QStyledItemDelegate::paint(painter, option, index);
    
        if( index == mySpecialIndex )
        {
                 painter->saveState();
                 painter->setPen( QPen( Qt::red, 1.0, Qt::SolidLine ) );
                 painter->drawRect( option.rect );
                 painter->restoreState();
        }
    }
    

    and set this delegate to your tree view widget.



  • @raven-worx said:

    void MyItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
    {
    QStyledItemDelegate::paint(painter, option, index);

    if( index == mySpecialIndex )
    {
             painter->saveState();
             painter->setPen( QPen( Qt::red, 1.0, Qt::SolidLine ) );
             painter->drawRect( option.rect );
             painter->restoreState();
    }
    

    }

    is there any other way to do that


  • Qt Champions 2017

    Hi
    "is there any other way to do that"

    Maybe you could tell why you want another way
    as a delegate is the way in most cases. ?



  • Hi this is my code

    QVariant myModel::data(const QModelIndex& index, int role) const
    {
    switch (role) {
    case Qt::DecorationRole:
    if (index.column() == 1) {
    QColor sc ;
    if (NInst inst = instance(index)) {
    sc = getColorForInstance(index,inst);
    if(sc.isValid()) {
    QPixmap cellPixmap(QSize(10,10));
    cellPixmap.fill(sc);
    QIcon icon(cellPixmap);
    return icon;
    }
    }
    }
    break;
    }

    QColor myModel::getColorForInstance(const QModelIndex& index ,NInst inst) const
    {
    QColor sc;
    if (condition1) {
    // transparent color
    return sc;
    }

    if(condition2) {
       sc = QColor(Qt::red)
       return sc
    

    }

    the color will be transparent in one condition and red color in other. So I want to know how to handle such case


  • Qt Champions 2017

    @Qt-Enthusiast said:

    getColorForInstance

    You are showing your model.

    but you still need the delegate to paint the rect?

    You could create a pixmap and return that as you do for DecorationRole



  • Could you please tell me the sample example or code for the same


  • Moderators

    @Qt-Enthusiast said:

    is there any other way to do that

    Whats the problem with this way?
    If you have special requirements then you need to tell us. But an item delegate is the most cleanest way to do this.



  • I am just asking . This is my legacy code

    QVariant myModel::data(const QModelIndex& index, int role) const {
    switch (role) {
    case Qt::DecorationRole:
    if (index.column() == 1) {
    myInst inst = getInstance(index);
    QColor sc = getColorForInstance(index,inst);;
    if(sc.isValid()) {
    QPixmap cellPixmap(QSize(10,10));
    cellPixmap.fill(sc);
    QIcon icon(cellPixmap);
    return icon;
    }
    }
    break;
    }

    QColor myModel::getColorForInstance(const QModelIndex& index ,Myinst inst) const {
    QColor sc;
    i f (condition1) {
    // transparent color
    return sc;
    }

       if(condition2) {
            sc = QColor(Qt::red)
          return sc
        }
    

    }

    there alpha option in QColor constructor for transparency , is there any way I can use the same in above code



  • Hi
    We tried to create a transparent image , but it is not giving me proper results .

    QPixmap pix(500,500);
    QPainter *paint = new QPainter(&pix);
    paint->setPen(QColor(255,34,255,255));
    paint->setBrush(QColor(255,34,255,50));
    paint->drawRect(15,15,100,100);
    delete paint;

    Can some one guide me for that


  • Qt Champions 2017

    so what do u get?



  • I am some black image . I want a trasparent color image with a boundary colored



  • I tried this also

    QPixmap cellPixmap(QSize(10,10));
    QPainter *paint = new QPainter(&cellPixmap);
    paint->setPen(Qt::transparent);
    paint->setBrush(Qt::transparent);
    paint->drawRect(10,10,10,10);
    delete paint;
    QIcon icon(cellPixmap);
    return icon;
    I am still getting black image .

    What is purpose of setPen and setBrush


  • Qt Champions 2017

    hi
    can you try
    paint->setPen(QColor(255,0,0));
    paint->setBrush(QColor(255,0,0));
    and tell me if its red.
    Else something else is wrong.

    also u say
    QPixmap cellPixmap(QSize(10,10));
    paint->drawRect(10,10,10,10);

    which means u draw OUTSIDE your image.

    the image is only 10x10.
    and u tell rect to start at 10,10



  • With this code

      QPixmap cellPixmap(QSize(10,10));
    

    QPainter *paint = new QPainter(&cellPixmap);
    paint->setPen(QColor(255,0,0));
    paint->setBrush(QColor(255,0,0));
    paint->drawRect(0,0,10,10);
    delete paint;
    QIcon icon(cellPixmap);

    I cannot see any thing . Could you correct my code


  • Qt Champions 2017

    it looks fine except no need to new it

    QPainter paint(&cellPixmap);
    paint.setPen(QColor(255,0,0));
    paint.setBrush(QColor(255,0,0));
    paint.drawRect(0,0,10,10);


  • Qt Champions 2017

    void MainWindow::on_pushButton_3_released()
    {
        QPixmap pixmap(100,100);
        pixmap.fill(QColor("transparent"));
    
        QPainter paint(&pixmap);
        paint.setPen(QColor(255,0,0));
        paint.setBrush(QColor(255,0,0));
        paint.drawRect(0,0,10,10);
    
        ui->label->setPixmap(pixmap);
    }
    

    This does produce red rect for me.
    (testing with QLabel)



  • but how to get transparent color with a boundary without any solid fill . Let me know

    [link text](link url http://doc.qt.io/qt-4.8/qpainter.html#drawRect-3 , image with square with no fill just the boundary


  • Qt Champions 2017

    @Qt-Enthusiast
    paint.setBrush(QColor("transparent"));

    gives me


Log in to reply
 

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