PDA

View Full Version : How to create a Widget that is semi-transparent and on top pf all open Widgets



nernst
8th December 2015, 11:33
Hi all,

I have an application an want to have something like a semi-transparent Warning-Popup. Everytime the user clicks on a prohibited field, the popup shall come up. My screen has a resolution of 800x480px, but the Popup shall be about 300x400px in the middle of the screen. Everything around the Popup shall be grey and semi-transparent.
Now I have created a QWidget, that is 800x480px and within this Widget, I have a QFrame with the Warning-information. The QFrame shall not be transparent, but the Widget around must be.

Can anybody help me to realise that popup?


Thanks.

anda_skoa
8th December 2015, 12:11
My approach would be this:

- create a custom widget, let it be full sized
- in the paintEvent method, fill the whole widget with the transparent color
- the fill the message area with opaque color
- then draw the message text into the message area

Cheers,
_

nernst
8th December 2015, 13:26
Hi, thanks for your answer.

I tried to following your hint:

popup.h:


class Popup: public QDialog
{
Q_OBJECT

public:
explicit Popup(QWidget *parent = 0);
~Popup();

protected:
voi paintEvent(QPaintEvent *){
QPainter p(this);
p.fillRect(rect(),QColor(100,100,100,150));

};


popup.cpp:


Popup::Popup(QWidget *parent) :
QDialog(parent),
ui(new Ui::Popup)
{
ui->setupUi(this);
setAttribute(Qt::WA_TranslucentBackground);
setWindowFlags(Qt::FramelessWindowHint);

setStyleSheet("QWidget{background: rgb(100,100,100,150);}");
show();
}



The problem is, that I have a certain transparency, but it looks as if there would be a black background and my semi-transparent background over it.

look here. the right picture is without the popup, the left one with the popup:
1156411565