PDA

View Full Version : QWidget, StackUnder and setWindowOpacity



sky
27th June 2011, 05:59
Hi All,

I am trying to create a few QWidgets (parent and a few siblings). The siblings overlap with each other and hence am using QWidget::stackUnder() to set the order. I also want to make some of these widgets translucent. So I need to use QWidget::setWindowOpacity(). But to use QWidget::setWindowOpacity() I have set the window flags to Qt::Window|Qt::FramelessWindowHint. And setting this flags break the stackUnder() order because stackUnder needs the widgets to be siblings. So as soon as I set the window flags to Qt::Window|Qt::FramelessWindowHint the z-index (stackUnder) ordering breaks.

Can anyone tell me how to break this and let me use both z-index ordering (stackUnder) and also use the setWindowOpacity to make the widgets translucent.

Rachol
27th June 2011, 14:12
Basically I am surprised that what you wanted to chieve didn't work out of the box.

Play with the example by removing calls to stackUnder. See that semitransparent backgrounds work and not transparent line is drawn on top of underlying widgets.


#include <QtGui>
#include <QWidget>

class TestWidget : public QWidget
{

public:
TestWidget(QColor color, QWidget *parent) : QWidget(parent),m_color(color) {}

protected:

void paintEvent(QPaintEvent *)
{
QPainter p(this);
QColor semiTrans = m_color;
semiTrans.setAlpha(50);
p.fillRect(rect(),semiTrans);
p.fillRect(0,height()/2 - 5,width(),5, m_color);
}
private:
QColor m_color;
};

int main(int argc, char * argv[])
{
QApplication app (argc, argv);

QWidget w;

TestWidget l1(Qt::black,&w);
l1.setGeometry(0,0,100,20);
TestWidget l2(Qt::red,&w);
l2.setGeometry(10,0,100,20);
TestWidget l3(Qt::green,&w);
l3.setGeometry(20,0,100,20);
TestWidget l4(Qt::yellow,&w);
l4.setGeometry(30,0,100,20);

l2.stackUnder(&l1);
l3.stackUnder(&l2);
l4.stackUnder(&l3);

w.resize(130, 40);
w.show();
return app.exec();
}

sky
28th June 2011, 10:07
Thank you Rachol. You code sure works. But my application is a bit complicated. All my widgets are Phonon::VideoPlayers and I cannot overload the paintEvent of the videoWidget in the videoPlayer.

Rachol
28th June 2011, 10:21
I am not sure what you are trying to achieve but have a look at void QWidget::setMask ( const QBitmap & bitmap ) method and QImage VideoWidget::snapshot () const. Combining these 2 might help you maybe.