Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make QWidget transparent

Tags:

I have a QWidget-based overlay widget which should paint some text and take place over the central widget of my application. The problem is that I can't set background of overlay widget to be transparent. What I already tried:

  1. setPalette(Qt::transparent);
  2. setAttribute( Qt::WA_TranslucentBackground, true );
  3. setAttribute( Qt::WA_OpaquePaintEvent, true );
  4. setAutoFillBackground(false);
  5. setStyleSheet("QWidget{background-color: transparent;}");
  6. setAttribute(Qt::WA_NoSystemBackground);
like image 877
sorush-r Avatar asked Aug 23 '14 20:08

sorush-r


People also ask

How do I make a Qt widget transparent?

In case you are facing the same dilemma, here's the code you need to use: theWidget->setAttribute(Qt::WA_TransparentForMouseEvents); Any widget with this attribute will become “transparent” to all mouse events as the name suggests, and yes, that is how I came up with this title.

How to make PyQt5 window transparent?

When we design an application in PyQt5, the main window is used to appear by default the window is opaque, but we can make it transparent as well. we can do this by using setWindowOpacity() method which belongs to the QWidget class .

What is Qtwidgets?

Widgets are the primary elements for creating user interfaces in Qt. Widgets can display data and status information, receive user input, and provide a container for other widgets that should be grouped together. A widget that is not embedded in a parent widget is called a window.


2 Answers

My best guess to show an overlay widget, is convert the widget to a window, resize it to it's contents and move them to the desired position manually.

MainWindow Example, showing the overlay widget in the center of the video widget:

Mwindow::Mwindow() {     widget = new Widget(this); }  void Mwindow::widgetSizeMove() {     if (widget->width() <= videoWidget->width() && widget->height() <= videoWidget->height())     {         widget->setWindowOpacity(1); // Show the widget         QPoint p = videoWidget->mapToGlobal(videoWidget->pos());         int x = p.x() + (videoWidget->width() - widget->width()) / 2;         int y = p.y() + (videoWidget->height() - widget->height()) / 2;         widget->move(x, y);         widget->raise();     }     else     {         widget->setWindowOpacity(0); // Hide the widget     } }  bool Mwindow::event(QEvent *event) {     switch (event->type())     {     case QEvent::Show:         widget->show();         QTimer::singleShot(50, this, SLOT(widgetSizeMove()));          //Wait until the Main Window be shown         break;     case QEvent::WindowActivate:     case QEvent::Resize:     case QEvent::Move:         widgetSizeMove();         break;     default:         break;     }      return QMainWindow::event(event); } 

Widget Example:

Widget::Widget(QWidget *parent) : QWidget(parent) {     setWindowFlags(Qt::Window | Qt::FramelessWindowHint);      setAttribute(Qt::WA_NoSystemBackground);     setAttribute(Qt::WA_TranslucentBackground);     setAttribute(Qt::WA_PaintOnScreen);      setAttribute(Qt::WA_TransparentForMouseEvents); }  void Widget::paintEvent(QPaintEvent*) {     QPainter p(this);     QString text = "Some foo goes here";     QFontMetrics metrics(p.font());     resize(metrics.size(0, text));     p.drawText(rect(), Qt::AlignCenter, text); } 

Example when showing a video with LibVLC:

VLC based player

like image 186
Antonio Dias Avatar answered Sep 30 '22 08:09

Antonio Dias


The best solution is provided by Gökmen Göksel in one of the comments of this article

setStyleSheet("background-color: rgba(0,0,0,0)"); 
like image 22
Slawek Rewaj Avatar answered Sep 30 '22 08:09

Slawek Rewaj