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. Transparent color in Qt
Forum Updated to NodeBB v4.3 + New Features

Transparent color in Qt

Scheduled Pinned Locked Moved General and Desktop
21 Posts 3 Posters 12.7k Views 2 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.
  • Q Qt Enthusiast

    @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?

    raven-worxR Offline
    raven-worxR Offline
    raven-worx
    Moderators
    wrote on last edited by
    #4

    @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.

    --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
    If you have a question please use the forum so others can benefit from the solution in the future

    1 Reply Last reply
    3
    • Q Offline
      Q Offline
      Qt Enthusiast
      wrote on last edited by
      #5

      @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

      raven-worxR 1 Reply Last reply
      0
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #6

        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. ?

        1 Reply Last reply
        0
        • Q Offline
          Q Offline
          Qt Enthusiast
          wrote on last edited by Qt Enthusiast
          #7

          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

          1 Reply Last reply
          0
          • mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #8

            @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

            1 Reply Last reply
            1
            • Q Offline
              Q Offline
              Qt Enthusiast
              wrote on last edited by
              #9

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

              1 Reply Last reply
              0
              • Q Qt Enthusiast

                @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

                raven-worxR Offline
                raven-worxR Offline
                raven-worx
                Moderators
                wrote on last edited by
                #10

                @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.

                --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                If you have a question please use the forum so others can benefit from the solution in the future

                1 Reply Last reply
                2
                • Q Offline
                  Q Offline
                  Qt Enthusiast
                  wrote on last edited by Qt Enthusiast
                  #11

                  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

                  1 Reply Last reply
                  0
                  • Q Offline
                    Q Offline
                    Qt Enthusiast
                    wrote on last edited by Qt Enthusiast
                    #12

                    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

                    1 Reply Last reply
                    0
                    • mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by
                      #13

                      so what do u get?

                      1 Reply Last reply
                      0
                      • Q Offline
                        Q Offline
                        Qt Enthusiast
                        wrote on last edited by
                        #14

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

                        1 Reply Last reply
                        0
                        • Q Offline
                          Q Offline
                          Qt Enthusiast
                          wrote on last edited by
                          #15

                          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

                          1 Reply Last reply
                          0
                          • mrjjM Offline
                            mrjjM Offline
                            mrjj
                            Lifetime Qt Champion
                            wrote on last edited by mrjj
                            #16

                            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

                            1 Reply Last reply
                            1
                            • Q Offline
                              Q Offline
                              Qt Enthusiast
                              wrote on last edited by
                              #17

                              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

                              1 Reply Last reply
                              0
                              • mrjjM Offline
                                mrjjM Offline
                                mrjj
                                Lifetime Qt Champion
                                wrote on last edited by
                                #18

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

                                mrjjM 1 Reply Last reply
                                1
                                • mrjjM mrjj

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

                                  mrjjM Offline
                                  mrjjM Offline
                                  mrjj
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #19
                                  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)

                                  1 Reply Last reply
                                  1
                                  • Q Offline
                                    Q Offline
                                    Qt Enthusiast
                                    wrote on last edited by Qt Enthusiast
                                    #20

                                    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

                                    mrjjM 1 Reply Last reply
                                    0
                                    • Q Qt Enthusiast

                                      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

                                      mrjjM Offline
                                      mrjjM Offline
                                      mrjj
                                      Lifetime Qt Champion
                                      wrote on last edited by
                                      #21

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

                                      gives me

                                      1 Reply Last reply
                                      3

                                      • Login

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