İbrahim
İbrahim

Reputation: 1021

Qt custom widget background transparent but I want that we can use objects that behind of the widget

I created a CustomWidget class:
CustomWidget.h:

#include <QWidget>
#include <QPainter>

class CustomWidget : public QWidget
{
  Q_OBJECT
public:
  explicit CustomWidget(QWidget *parent = nullptr);

  void paintEvent(QPaintEvent* event);

signals:

public slots:
};

CustomWidget.cpp:

#include "customwidget.h"

CustomWidget::CustomWidget(QWidget *parent) : QWidget(parent)
{
  this->setWindowFlag(Qt::FramelessWindowHint);
}

void CustomWidget::paintEvent(QPaintEvent* event)
{
  if (this->isActiveWindow())
  {
    QPainter painter(this);
    painter.setPen(Qt::green);
    painter.setBrush(QBrush(QColor(69, 232, 32, 100)));
    painter.drawRect(rect());
  }
}

When I compile and run this codes on Linux (KDE Neon), I see correct widget with color (color is green = 69, 232, 32) but opacity (alpha = 100) is not working. Here is picture:
This is not correct widget background.
How can I create transparent or opacity widgets? Is this a bug?
Also, I can see transparent widgets if I can create opacity / transparent widget but can't use background (I mean I can't intervene objects that behind of the widget. You know that screencast applications records all screen and we can use screen). Can I create widgets like this? Thanks.

Upvotes: 1

Views: 288

Answers (1)

Vaidotas Strazdas
Vaidotas Strazdas

Reputation: 716

Well, I think you have forgotten to put the following line. Below:

this->setWindowFlag(Qt::FramelessWindowHint);

Put the following line (i.e. in constructor of your CustomWidget):

this->setAttribute(Qt::WA_TranslucentBackground);

Then, you should be able to have transparent background you want based on the opacity you set. Hopefully, this helps in solving your problem.

Upvotes: 1

Related Questions