View Full Version : Transparent widgets

20th March 2006, 21:35
I have read some on this subject in the forum, but for me this is still very unsure regarding implementation.

I want my pushbutton to be like a chameleon and would have for background the zone over which it is disposed. So in fact they would look transparent.

The the question is which function should I call to get the QPixmap occupied my the background image and give it as initial value to the widget constructor ?

Hope you see why I try to do : use backgrounds and have transparent button disposed covering the menu labels for instance. It fills like you clicked on the baground image.

20th March 2006, 21:43

20th March 2006, 21:52
Does a transparent widget's class exist for pushbutton ?
Where can I find sample code doing transparent pushbotton ?
I this cannot grasp all of this theory and I like examples as distributed by the open source edition of QT4.1.1. They explain much of the theory.
Anyway they should even be part of QT. It could be also handled by the QT about the background resize means.

20th March 2006, 22:03
Did you try something like:
QPalette p( button->palette() );
p.setBrush( QPalette::Button, Qt::NoBrush );
button->setPalette( p );

20th March 2006, 22:10
// destinataires push button
pbDestin = new QPushButton( "Destinataires", this);
pbDestin->resize( QSize( 80, 20 ) );
pbDestin->move( 15, 90 );
QPalette p( pbDestin->palette() );
p.setBrush( QPalette::Button, Qt::NoBrush );
pbDestin->setPalette( p );

This won't work. What am I doing wrong ?

20th March 2006, 22:29
This won't work.
How does "this won't work"?

What am I doing wrong ?
Well... I didn't say that it will, but you should start from the simplest solutions ;)

20th March 2006, 22:33
It shows my pushbutton + pushbutton text value + button shaPe.
It shows nothing of the background.

I agree we should try simplest things first.

21st March 2006, 14:21
Either this is a wrong approach or it's the QPalette bug (http://www.qtcentre.org/forum/showthread.php?t=1240). Try to subclass QAbstractButton and draw only text in paintEvent.

21st March 2006, 18:32
Actually I would like to see nothing from my button, not even the text : 100% transparent.
But subclassing as you propose and using paintEvent is beyond my knowledge. But thank you for helping out. I hope I can find some information about this subclassing in the QT3 book.

21st March 2006, 18:40
I hope I can find some information about this subclassing in the QT3 book.
You should rather look for a book about programming in C++.


class Button : public QAbstractButton
Button( QWidget *parent = 0 ) : QAbstractButton( parent ) {}
void paintEvent( QPaintEvent * ) {}

21st March 2006, 19:01
I am more impressed by your skills than by my lack of knowledge (which is huge!!).
Of course the piece of code you gave me works 100%.

Thank you very much.