Results 1 to 14 of 14

Thread: transparent background

  1. #1
    Join Date
    Dec 2007
    Posts
    119
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default transparent background

    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?

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: transparent background

    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).

  3. #3
    Join Date
    Dec 2007
    Posts
    119
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: transparent background

    Quote Originally Posted by marcel View Post
    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?

    Quote Originally Posted by marcel View Post
    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.

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    258
    Thanks
    22
    Thanked 19 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: transparent background

    Quote Originally Posted by fruzzo View Post
    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-snapsh....html#setAlpha
    http://doc.trolltech.com/main-snapsh...lended-drawing

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: transparent background

    Quote Originally Posted by momesana View Post
    We're talking about Qt3 here...

  6. #6
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    258
    Thanks
    22
    Thanked 19 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: transparent background

    Quote Originally Posted by wysota View Post
    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...q35D9U1yFV_Daw
    Last edited by momesana; 9th March 2008 at 14:04. Reason: had forgotton to include link

  7. #7
    Join Date
    Dec 2007
    Posts
    119
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: transparent background

    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?

    Qt Code:
    1. #include "azimutscale.h"
    2. #include <qpainter.h>
    3. #include <qbitmap.h>
    4.  
    5. AzimutScale::AzimutScale( QWidget *parent, const char *name)
    6. : mapParent(parent), QWidget( parent, name )
    7. {
    8. resize(parent->width()*0.9, parent->height()*0.9);
    9. }
    10.  
    11.  
    12. void AzimutScale::paintEvent( QPaintEvent * )
    13. {
    14.  
    15. QBitmap bm( size() );
    16. bm.fill( color0 ); //transparent
    17. QPainter paint;
    18. paint.begin( &bm, this, false );
    19. drawScale( &paint );
    20. paint.end();
    21. setMask( bm );
    22.  
    23. }
    24.  
    25. void AzimutScale::drawScale( QPainter *paint )
    26. {
    27. /*QColor mainTagsColor (127,0,127);
    28.   QColor tagsColor( 127,127,191);*/
    29.  
    30. QColor mainTagsColor (color1);
    31. QColor tagsColor( color1);
    32.  
    33. paint->setWindow( -500,-500, 1000,1000 );
    34.  
    35. QRect v = paint->viewport();
    36. int d = QMIN( v.width(), v.height() );
    37. paint->setViewport( v.left() + (v.width()-d)/2,
    38. v.top() + (v.height()-d)/2, d, d );
    39.  
    40.  
    41. //Draw cross at center of scale
    42. paint->setPen(QPen(mainTagsColor,1));
    43. paint->drawLine( 0, -50, 0, 50 );
    44. paint->drawLine( -50, 0, 50,0 );
    45.  
    46. //Draw main scale
    47. QFont fontLabel ("times", 15, QFont::Bold);
    48. QFontMetrics fm (fontLabel);
    49.  
    50. for ( int i=0; i<12; i++ ) {
    51. //draw label
    52. paint->setPen(mainTagsColor);
    53. paint->setFont(fontLabel);
    54. QString label = QString::number(i*30);
    55. int pixWidth = fm.width(label);
    56. paint->drawText(0-pixWidth/2, -480, label);
    57. //draw tag
    58. paint->setPen(QPen(mainTagsColor,1));
    59. paint->drawLine( 0, -300, 0, -470 );
    60. paint->rotate( 30 );
    61. }
    62.  
    63. //Draw minus scale
    64. paint->setPen(tagsColor);
    65. for ( int j=0; j<60; j++ ) {
    66. if ((j%5) !=0 )
    67. paint->drawLine( 0, -450, 0, -470 );
    68. paint->rotate( 6 );
    69. }
    70. }
    To copy to clipboard, switch view to plain text mode 

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: transparent background

    Use its superclass - QPixmap.

  9. #9
    Join Date
    Dec 2007
    Posts
    119
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: transparent background

    Quote Originally Posted by wysota View Post
    Use its superclass - QPixmap.
    mhmm I've just try to use it to but I don't undestand how to do the same thing!

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: transparent background

    Just looked at your code again. Try this:

    Qt Code:
    1. void xxx::paintEvent(...){
    2. QPainter p(this);
    3. drawScale(&p);
    4. // then set the mask as you did earlier
    5. //...
    6. }
    To copy to clipboard, switch view to plain text mode 

  11. #11
    Join Date
    Dec 2007
    Posts
    119
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: transparent background

    I try this but It seems don't work.

    Qt Code:
    1. void AzimutScale::paintEvent( QPaintEvent * ){
    2. QPainter paint;
    3. drawScale( &paint );
    4. QBitmap bm( size() );
    5. bm.fill( color0 );
    6. setMask( bm );
    7. }
    To copy to clipboard, switch view to plain text mode 

  12. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: transparent background

    Define "doesn't work". The mask is certainly invalid...

  13. #13
    Join Date
    Dec 2007
    Posts
    119
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: transparent background

    Quote Originally Posted by wysota View Post
    Define "doesn't work". The mask is certainly invalid...
    "doesn't work" = the widget is filled with black color and don't draw anithing.

  14. #14
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: transparent background

    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.

Similar Threads

  1. transparent background of the main widget
    By nagpalma in forum Qt Programming
    Replies: 2
    Last Post: 4th May 2007, 17:52
  2. Replies: 3
    Last Post: 8th December 2006, 18:51
  3. QDialog w/ transparent background
    By LarryDobson in forum Qt Programming
    Replies: 6
    Last Post: 26th September 2006, 19:26
  4. Transparent background Style
    By Lele in forum Qt Programming
    Replies: 5
    Last Post: 17th July 2006, 12:02
  5. Replies: 1
    Last Post: 5th April 2006, 16:44

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.