PDA

View Full Version : transparent background



fruzzo
8th March 2008, 16:51
I've a problem....I create myWidget (with a it's own paint Event) and now I want that its background is transparent so that it's background is the same of the portion of its parent widget displayed covered by myWidget. Any Idea?

marcel
8th March 2008, 20:41
You can set a mask(but you will get rough edges) or you can first fill the widget with a transparent color or transparent pixmap and then paint on it.

But this will only work as long as the widget''s position is restricted to its parent bounds(won't work for floating widgets).

fruzzo
9th March 2008, 11:47
You can set a mask(but you will get rough edges) or you can first fill the widget with a transparent color or transparent pixmap and then paint on it.

ok... an example of transparent color an one for transparent pixmap?


But this will only work as long as the widget''s position is restricted to its parent bounds(won't work for floating widgets).

perfect, this is what I want. ;)

momesana
9th March 2008, 13:19
ok... an example of transparent color an one for transparent pixmap?


Set the Brush to a QColor with an appropriate alpha value.
http://doc.trolltech.com/main-snapshot/qcolor.html#setAlpha
http://doc.trolltech.com/main-snapshot/qcolor.html#alpha-blended-drawing

wysota
9th March 2008, 14:02
Set the Brush to a QColor with an appropriate alpha value.
http://doc.trolltech.com/main-snapshot/qcolor.html#setAlpha
http://doc.trolltech.com/main-snapshot/qcolor.html#alpha-blended-drawing

We're talking about Qt3 here...

momesana
9th March 2008, 14:30
We're talking about Qt3 here...
Oops.
There is an artikel about transparency in QT-4.1 but it also discusses the ways transparency can be achieved with Qt3 without going too much into details. It's a good read.
http://www.google.de/url?sa=t&ct=res&cd=2&url=http%3A%2F%2Fdoc.trolltech.com%2Fqq%2Fqq16-background.html&ei=KO7TR4_AI5ja-ALo5rDTDw&usg=AFQjCNE0LeCd8iYWXQ5zSgr29AxWLiM13Q&sig2=FPd-gL9tq35D9U1yFV_Daw

fruzzo
12th March 2008, 19:50
I've solved the problem in this way....but using QBitmap I can draw only with white color, any idea how to modify the code to draw in other colors?



#include "azimutscale.h"
#include <qpainter.h>
#include <qbitmap.h>

AzimutScale::AzimutScale( QWidget *parent, const char *name)
: mapParent(parent), QWidget( parent, name )
{
resize(parent->width()*0.9, parent->height()*0.9);
}


void AzimutScale::paintEvent( QPaintEvent * )
{

QBitmap bm( size() );
bm.fill( color0 ); //transparent
QPainter paint;
paint.begin( &bm, this, false );
drawScale( &paint );
paint.end();
setMask( bm );

}

void AzimutScale::drawScale( QPainter *paint )
{
/*QColor mainTagsColor (127,0,127);
QColor tagsColor( 127,127,191);*/

QColor mainTagsColor (color1);
QColor tagsColor( color1);

paint->setWindow( -500,-500, 1000,1000 );

QRect v = paint->viewport();
int d = QMIN( v.width(), v.height() );
paint->setViewport( v.left() + (v.width()-d)/2,
v.top() + (v.height()-d)/2, d, d );


//Draw cross at center of scale
paint->setPen(QPen(mainTagsColor,1));
paint->drawLine( 0, -50, 0, 50 );
paint->drawLine( -50, 0, 50,0 );

//Draw main scale
QFont fontLabel ("times", 15, QFont::Bold);
QFontMetrics fm (fontLabel);

for ( int i=0; i<12; i++ ) {
//draw label
paint->setPen(mainTagsColor);
paint->setFont(fontLabel);
QString label = QString::number(i*30);
int pixWidth = fm.width(label);
paint->drawText(0-pixWidth/2, -480, label);
//draw tag
paint->setPen(QPen(mainTagsColor,1));
paint->drawLine( 0, -300, 0, -470 );
paint->rotate( 30 );
}

//Draw minus scale
paint->setPen(tagsColor);
for ( int j=0; j<60; j++ ) {
if ((j%5) !=0 )
paint->drawLine( 0, -450, 0, -470 );
paint->rotate( 6 );
}
}

wysota
12th March 2008, 20:16
Use its superclass - QPixmap.

fruzzo
13th March 2008, 19:44
Use its superclass - QPixmap.

mhmm I've just try to use it to but I don't undestand how to do the same thing! :confused:

wysota
13th March 2008, 20:51
Just looked at your code again. Try this:


void xxx::paintEvent(...){
QPainter p(this);
drawScale(&p);
// then set the mask as you did earlier
//...
}

fruzzo
15th March 2008, 15:53
I try this but It seems don't work.


void AzimutScale::paintEvent( QPaintEvent * ){
QPainter paint;
drawScale( &paint );
QBitmap bm( size() );
bm.fill( color0 );
setMask( bm );
}

wysota
15th March 2008, 15:56
Define "doesn't work". The mask is certainly invalid...

fruzzo
18th March 2008, 10:25
Define "doesn't work". The mask is certainly invalid...

"doesn't work" = the widget is filled with black color and don't draw anithing.

wysota
18th March 2008, 15:42
I suggest you take a look at some working paintEvent() implementation, because yours doesn't make any sense. Look how you created the painter object - you didn't tell it what to paint on.