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 up QWidget only background transparency
QtWS25 Last Chance

How to set up QWidget only background transparency

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 1.9k Views
  • 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.
  • Qt_crazyerQ Offline
    Qt_crazyerQ Offline
    Qt_crazyer
    wrote on last edited by
    #1

    I want to set up QWidget only background transparency, and I will draw some text and some others by qpainter on the widget. Using the setWindowOpacity(0.2) will make all things, including the text, etc, transparent. How should I modify it?

    #include "widget.h"
    
    Widget::Widget(QWidget *parent)
        : QWidget(parent)
    {
        setWindowOpacity(0.2);                       
        resize(200, 200);
        setWindowFlag(Qt::FramelessWindowHint);     
        QBitmap bmp(this->size());
        bmp.fill();
        QPainter p(&bmp);
        p.setRenderHint(QPainter::Antialiasing);
        p.setPen(Qt::NoPen);
        p.setBrush(Qt::black);
        p.drawRoundedRect(bmp.rect(),20,20);
        setMask(bmp);
    }
    
    Widget::~Widget()
    {
    
    }
    
    void Widget::paintEvent(QPaintEvent *)
    {
        QPainter painter(this);
        painter.setPen(Qt::blue);
        painter.setFont(QFont("Arial", 30));
        painter.drawText(rect(), Qt::AlignCenter, "Qt");
    
        update();
    }
    
    

    Qt 5.9.1 on windows 10
    Thanks in advance.

    raven-worxR 1 Reply Last reply
    0
    • Qt_crazyerQ Qt_crazyer

      I want to set up QWidget only background transparency, and I will draw some text and some others by qpainter on the widget. Using the setWindowOpacity(0.2) will make all things, including the text, etc, transparent. How should I modify it?

      #include "widget.h"
      
      Widget::Widget(QWidget *parent)
          : QWidget(parent)
      {
          setWindowOpacity(0.2);                       
          resize(200, 200);
          setWindowFlag(Qt::FramelessWindowHint);     
          QBitmap bmp(this->size());
          bmp.fill();
          QPainter p(&bmp);
          p.setRenderHint(QPainter::Antialiasing);
          p.setPen(Qt::NoPen);
          p.setBrush(Qt::black);
          p.drawRoundedRect(bmp.rect(),20,20);
          setMask(bmp);
      }
      
      Widget::~Widget()
      {
      
      }
      
      void Widget::paintEvent(QPaintEvent *)
      {
          QPainter painter(this);
          painter.setPen(Qt::blue);
          painter.setFont(QFont("Arial", 30));
          painter.drawText(rect(), Qt::AlignCenter, "Qt");
      
          update();
      }
      
      

      Qt 5.9.1 on windows 10
      Thanks in advance.

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

      @Qt_crazyer
      try to set one (or all) of the following attributes (in conjunction to the already set Qt::FramelessWindowHint window flag):

      setAttribute(Qt::WA_NoSystemBackground);
      setAttribute(Qt::WA_TranslucentBackground);
      setAttribute(Qt::WA_PaintOnScreen);
      

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

      • Login

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