PDA

View Full Version : transpaent widgets



elcuco
24th June 2007, 20:48
Hi,

I need to put a widget on top of a QMainWindow (outside of the layout), and I would like the widget to be semi transparent. That widget will NOT BE a top modal window, so it should work everywhere.

How can I do this? (styles maybe...?

marcel
24th June 2007, 20:54
No, this should be a combination of setGeometry and setWindowOpacity/QPainer::setOpacity( for the child widget ).

As long as you don't want the child widget to get out of the boundaries of the main window, then this will work.

Composition works just fine when the backdrop is a widget( here a main window ).

Regards

elcuco
24th June 2007, 21:09
I am running these commands on the constructor of some widget, to test this code.



QPushButton *b = new QPushButton( this );
QRect r( 50, 100, 100, 25 );
b->setGeometry( r );
b->setWindowOpacity( 0.7 );
//b->paintEngine()->painter()->setOpacity( 0.7 );


When I uncomment the last line, the application will die. Any hints...?

marcel
24th June 2007, 21:11
twidget::twidget(QWidget *parent, Qt::WFlags flags)
: QMainWindow(parent, flags)
{
ui.setupUi(this);
mTranspW = new QWidget();
QPalette p = mTranspW->palette();
p.setColor(QPalette::Window, Qt::blue);
mTranspW->show();
mTranspW->setPalette(p);
mTranspW->setWindowOpacity(0.5);
mTranspW->setFixedSize(200, 200);
mTranspW->move(100, 100);
}


It turns out that in 4.3 the the widget is composed even outside the parent widget bounds.

In my example I did not set any window flags for the child. You might want it to be frameless, etc.

Regards

marcel
24th June 2007, 21:14
I am running these commands on the constructor of some widget, to test this code.



QPushButton *b = new QPushButton( this );
QRect r( 50, 100, 100, 25 );
b->setGeometry( r );
b->setWindowOpacity( 0.7 );
//b->paintEngine()->painter()->setOpacity( 0.7 );
When I uncomment the last line, the application will die. Any hints...?

Well, you can't just access a painter that way. The painter will be valid only in paint event, otherwise it is null.

Regards

marcel
24th June 2007, 21:33
Here's even a better one. It allows you to drag the transparent widget around inside the main window.




#include "twidget.h"
#include <QPainter>
#include <QMouseEvent>

twidget::twidget(QWidget *parent, Qt::WFlags flags)
: QMainWindow(parent, flags)
{
ui.setupUi(this);
mTranspW = new xwww(this);
QPalette p = mTranspW->palette();
mTranspW->setVisible(true);
mTranspW->setFixedSize(200, 200);
mTranspW->move(0, 0);
}

twidget::~twidget()
{
}

void twidget::mousePressEvent(QMouseEvent *e)
{

}

void twidget::mouseMoveEvent(QMouseEvent *e)
{
if( (e->buttons()&Qt::LeftButton))
{
QPoint pos = e->globalPos();
QPoint topLeft = mapToGlobal(mTranspW->rect().topLeft());
mTranspW->move(pos - topLeft);
}
}

xwww::xwww(QWidget* p)
:QWidget(p, Qt::FramelessWindowHint | Qt::Widget)
{
}

xwww::~xwww()
{
}

void xwww::paintEvent(QPaintEvent* e)
{
QPainter p( this );
p.setOpacity(0.5);
p.fillRect(rect(), Qt::blue);
}


The header:


#ifndef TWIDGET_H
#define TWIDGET_H

#include <QtGui/QMainWindow>
#include "ui_twidget.h"

class xwww : public QWidget
{
Q_OBJECT

public:
xwww(QWidget*);
~xwww();
protected:
virtual void paintEvent(QPaintEvent*);
};


class twidget : public QMainWindow
{
Q_OBJECT

public:
twidget(QWidget *parent = 0, Qt::WFlags flags = 0);
~twidget();

protected:
virtual void mousePressEvent(QMouseEvent *e);
virtual void mouseMoveEvent(QMouseEvent *e);

private:
Ui::twidgetClass ui;
xwww* mTranspW;
};
#endif // TWIDGET_H


Regards