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?
Printable View
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?
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).
Set the Brush to a QColor with an appropriate alpha value.
http://doc.trolltech.com/main-snapsh....html#setAlpha
http://doc.trolltech.com/main-snapsh...lended-drawing
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...q35D9U1yFV_Daw
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?
Code:
#include "azimutscale.h" #include <qpainter.h> #include <qbitmap.h> { resize(parent->width()*0.9, parent->height()*0.9); } { bm.fill( color0 ); //transparent QPainter paint; paint.begin( &bm, this, false ); drawScale( &paint ); paint.end(); setMask( bm ); } { /*QColor mainTagsColor (127,0,127); QColor tagsColor( 127,127,191);*/ paint->setWindow( -500,-500, 1000,1000 ); 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->drawLine( 0, -50, 0, 50 ); paint->drawLine( -50, 0, 50,0 ); //Draw main scale for ( int i=0; i<12; i++ ) { //draw label paint->setPen(mainTagsColor); paint->setFont(fontLabel); int pixWidth = fm.width(label); paint->drawText(0-pixWidth/2, -480, label); //draw tag 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 ); } }
Use its superclass - QPixmap.
Just looked at your code again. Try this:
Code:
void xxx::paintEvent(...){ drawScale(&p); // then set the mask as you did earlier //... }
I try this but It seems don't work.
Define "doesn't work". The mask is certainly invalid...
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.