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. How to set transparency effects with setMask (or other)
Forum Updated to NodeBB v4.3 + New Features

How to set transparency effects with setMask (or other)

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 630 Views 3 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.
  • G Offline
    G Offline
    giusdbg
    wrote on last edited by giusdbg
    #1

    Hi.

    I'm trying to set transparency effects, but I can't set a real transparency, texts and graphics in the foreground are removed.

    (sample images and code used below.)

    I need some suggestions and help.

    Screenshot_20230809_172737.png
    Screenshot_20230809_172842.png

        QPixmap px(this->size()); //Create pixmap with the same size of current widget
        px.fill(Qt::transparent); //Fill transparent
        QPainter p(&px);
        QBrush brush;
        brush.setStyle(Qt::SolidPattern); //For fill
        p.setBrush(brush);
        p.drawRoundedRect(this->rect(), 45.0, 45.0); //Draw filled rounded rectangle on pixmap
        this->setMask(px.mask()); //The the mask for current widget.
    

    This code is called in the constructor of an object derived from QFrame, and then the foreground parts are drawn.

    Pl45m4P 1 Reply Last reply
    0
    • G giusdbg

      Hi.

      I'm trying to set transparency effects, but I can't set a real transparency, texts and graphics in the foreground are removed.

      (sample images and code used below.)

      I need some suggestions and help.

      Screenshot_20230809_172737.png
      Screenshot_20230809_172842.png

          QPixmap px(this->size()); //Create pixmap with the same size of current widget
          px.fill(Qt::transparent); //Fill transparent
          QPainter p(&px);
          QBrush brush;
          brush.setStyle(Qt::SolidPattern); //For fill
          p.setBrush(brush);
          p.drawRoundedRect(this->rect(), 45.0, 45.0); //Draw filled rounded rectangle on pixmap
          this->setMask(px.mask()); //The the mask for current widget.
      

      This code is called in the constructor of an object derived from QFrame, and then the foreground parts are drawn.

      Pl45m4P Offline
      Pl45m4P Offline
      Pl45m4
      wrote on last edited by
      #2

      @giusdbg

      How should it look like?


      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      G 1 Reply Last reply
      0
      • Pl45m4P Pl45m4

        @giusdbg

        How should it look like?

        G Offline
        G Offline
        giusdbg
        wrote on last edited by
        #3

        @Pl45m4 Like this image below, but with the central part not transparent (like in the second image above).

        Screenshot_20230809_180507.png

        1 Reply Last reply
        0
        • KenAppleby 0K Offline
          KenAppleby 0K Offline
          KenAppleby 0
          wrote on last edited by
          #4

          The way I do this is to subclass QWidget, override the paintEvent() virtual function and draw the widget background using a QBrush set with a transparent colour.

          G 2 Replies Last reply
          0
          • KenAppleby 0K KenAppleby 0

            The way I do this is to subclass QWidget, override the paintEvent() virtual function and draw the widget background using a QBrush set with a transparent colour.

            G Offline
            G Offline
            giusdbg
            wrote on last edited by
            #5

            @KenAppleby-0 I state that my knowledge of Qt is poor.

            But in the paintEvent I can't get any result that comes close to the requested one (setMask is much closer).
            What I get is a black background or no background change.

            Perhaps it is due to the fact that in the QWidget parent of the QFrame the background is an image.

            1 Reply Last reply
            0
            • KenAppleby 0K KenAppleby 0

              The way I do this is to subclass QWidget, override the paintEvent() virtual function and draw the widget background using a QBrush set with a transparent colour.

              G Offline
              G Offline
              giusdbg
              wrote on last edited by giusdbg
              #6

              @KenAppleby-0 I think I found, the main thing was to turn off these calls buried in the code recesses

              setAutoFillBackground( true )
              

              And the management of the palette and the background image needs to be reviewed.

              But even if other things malfunction now, partial transparency works.

              I've made some changes, the graphic effect is better and the code is substantially definitive (if it can be useful to someone else).

              void Panel::paintEvent( QPaintEvent *e )
              {
                 Q_UNUSED(e);
                
                 QPainter p( this );   
                 QRect qrRect;
                 QColor qcRectColor;
              
                 qrRect = rect();
                 qrRect.adjust( 1, 1, -1, -1 );   
                 qcRectColor = palette().color( QPalette::Background );
                 qcRectColor.setAlphaF( 0.7 );      
                 
              //   p.setRenderHint ( QPainter::Antialiasing );
                 p.setPen( palette().color( QPalette::Background ) );
                 p.setBrush( qcRectColor );      
                 p.drawRoundedRect( qrRect, 45.0, 45.0);
              
                 QFrame::paintEvent( e ); 
              }
              

              Screenshot_20230810_150457.png

              1 Reply Last reply
              1
              • G giusdbg has marked this topic as solved on

              • Login

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