Results 1 to 10 of 10

Thread: transparency problem. pls help.

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Apr 2011
    Posts
    58
    Thanks
    1

    Default transparency problem. pls help.

    Here's my codes.
    Attached is the result of the image.

    You can see that the parts that are transparent ends up with black color.
    What gives? I like it to be the background color of whatever that is underneath this rainbow drawing. How do I do that?

    Many thanks!

    Qt Code:
    1. mypainter->setPen(Qt::NoPen);
    2.  
    3. QLinearGradient rect_gradient(0, 0, 0, height());
    4. rect_gradient.setColorAt(0, Qt::red);
    5. rect_gradient.setColorAt(.17, Qt::yellow);
    6. rect_gradient.setColorAt(.33, Qt::green);
    7. rect_gradient.setColorAt(.50, Qt::cyan);
    8. rect_gradient.setColorAt(.66, Qt::blue);
    9. rect_gradient.setColorAt(.81, Qt::magenta);
    10. rect_gradient.setColorAt(1, Qt::red);
    11. mypainter->setBrush(rect_gradient);
    12. mypainter->drawRect(width() / 2, 0, width() / 2, height());
    13.  
    14. QLinearGradient alpha_gradient(0, 0, width(), 0);
    15. alpha_gradient.setColorAt(0, Qt::white);
    16. alpha_gradient.setColorAt(0.2, Qt::white);
    17. alpha_gradient.setColorAt(0.5, Qt::transparent);
    18. alpha_gradient.setColorAt(0.8, Qt::white);
    19. alpha_gradient.setColorAt(1, Qt::white);
    20.  
    21. mypainter->setCompositionMode(QPainter::CompositionMode_DestinationIn);
    22. mypainter->setBrush(alpha_gradient);
    23. mypainter->drawRect(0, 0, width(), height());
    24.  
    25. mypainter->setCompositionMode(QPainter::CompositionMode_DestinationOver);
    26.  
    27. mypainter->setPen(Qt::NoPen);
    28. mypainter->setRenderHint(QPainter::SmoothPixmapTransform);
    To copy to clipboard, switch view to plain text mode 
    Attached Images Attached Images

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: transparency problem. pls help.

    Hi,

    see QWidget::setAutoFillBackground(). Also have a look at the windget attribute Qt::WA_NoSystemBackground.

  3. #3
    Join Date
    Apr 2011
    Posts
    58
    Thanks
    1

    Default Re: transparency problem. pls help.

    I tried that actually. They seem to have no effect over the results I'm getting.
    Any clue??

  4. #4
    Join Date
    Apr 2011
    Posts
    58
    Thanks
    1

    Default Re: transparency problem. pls help.

    Could someone please help me on this?
    I've tried all kind of compostion mode and the suggestions. None of them work.
    And I've been stuck on this for hours and hours. A seemingly simple task of drawing a partial transparent stuff yet it seems impossible to do...

  5. #5
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: transparency problem. pls help.

    Ehm, yes, why do you alter the composition mode?
    Qt Code:
    1. #include <QtGui>
    2.  
    3. class test : public QWidget
    4. {
    5. public:
    6. test(QWidget* parent) : QWidget(parent)
    7. {
    8. }
    9.  
    10. protected:
    11. void paintEvent(QPaintEvent*)
    12. {
    13. QPainter mypainter(this);
    14. mypainter.fillRect(0, 0, width(), height(), Qt::transparent);
    15. mypainter.setPen(Qt::NoPen);
    16.  
    17. QLinearGradient rect_gradient(0, 0, 0, height());
    18. rect_gradient.setColorAt(0, Qt::red);
    19. rect_gradient.setColorAt(.17, Qt::yellow);
    20. rect_gradient.setColorAt(.33, Qt::green);
    21. rect_gradient.setColorAt(.50, Qt::cyan);
    22. rect_gradient.setColorAt(.66, Qt::blue);
    23. rect_gradient.setColorAt(.81, Qt::magenta);
    24. rect_gradient.setColorAt(1, Qt::red);
    25. mypainter.setBrush(rect_gradient);
    26. mypainter.drawRect(width() / 2, 0, width() / 2, height());
    27.  
    28. QLinearGradient alpha_gradient(0, 0, width(), 0);
    29. alpha_gradient.setColorAt(0, Qt::white);
    30. alpha_gradient.setColorAt(0.2, Qt::white);
    31. alpha_gradient.setColorAt(0.5, Qt::transparent);
    32. alpha_gradient.setColorAt(0.8, Qt::white);
    33. alpha_gradient.setColorAt(1, Qt::white);
    34.  
    35. // mypainter.setCompositionMode(QPainter::CompositionMode_DestinationIn);
    36. mypainter.setBrush(alpha_gradient);
    37. mypainter.drawRect(0, 0, width(), height());
    38.  
    39. mypainter.setCompositionMode(QPainter::CompositionMode_DestinationOver);
    40.  
    41. mypainter.setPen(Qt::NoPen);
    42. mypainter.setRenderHint(QPainter::SmoothPixmapTransform);
    43. }
    44. };
    45.  
    46. int main(int argc, char* argv[])
    47. {
    48. QApplication a(argc, argv);
    49.  
    50. QWidget parent;
    51. parent.setBackgroundRole(QPalette::ToolTipBase);
    52. test t(&parent);
    53. parent.show();
    54.  
    55. return a.exec();
    56. }
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Apr 2011
    Posts
    58
    Thanks
    1

    Default Re: transparency problem. pls help.

    Thanks. I comment out the codes you suggested.
    and ended up with the attached picture.
    It still does not make sense 'cause the transparent part should be
    somewhere in the middle of the picture since that's where I
    set the 'transparent'. yet in the picture, it is the right side that
    seems to be getting the transparent. Beats me!? I just cant figure
    out...
    thank you for any pointer.
    Attached Images Attached Images

  7. #7
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: transparency problem. pls help.

    ? no it is white at left and right. Only in the middle it is transparent:
    Qt Code:
    1. alpha_gradient.setColorAt(0.5, Qt::transparent);
    To copy to clipboard, switch view to plain text mode 
    And keep in mind, you are drawing with a transparent pen, you are not making all pixel in that area transparent.

  8. #8
    Join Date
    Apr 2011
    Posts
    58
    Thanks
    1

    Default Re: transparency problem. pls help.

    Sorry. I must be missing something terribly...
    The expected results I thought I should get is as shown in the attached file.
    (ignore the checkboard patten in the background. main idea is the transparency, where does it start)

    So, this expected result has the transparency starts from the middle of the window and then slowly goes back to the "rainbow" color. But the actual result, after commenting out the code you suggested ends up the opposite.
    I really dont get it. What really am I wrong in understanding what I should get?
    Thanks once again.
    Attached Images Attached Images

  9. #9
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: transparency problem. pls help.

    Well for that you have to cache the result in a pixmap or image:
    Qt Code:
    1. #include <QtGui>
    2.  
    3. class test : public QWidget
    4. {
    5. public:
    6. test(QWidget* parent) : QWidget(parent)
    7. {
    8. }
    9.  
    10. protected:
    11. void paintEvent(QPaintEvent*)
    12. {
    13.  
    14. QPixmap pix(width(), height());
    15. pix.fill(Qt::transparent);
    16.  
    17. QPainter mypainter(&pix);
    18. mypainter.setPen(Qt::NoPen);
    19.  
    20. QLinearGradient alpha_gradient(0, 0, width(), 0);
    21. alpha_gradient.setColorAt(0, Qt::white);
    22. alpha_gradient.setColorAt(0.2, Qt::white);
    23. alpha_gradient.setColorAt(0.5, Qt::transparent);
    24. alpha_gradient.setColorAt(0.8, Qt::white);
    25. alpha_gradient.setColorAt(1, Qt::white);
    26. mypainter.setBrush(alpha_gradient);
    27. mypainter.drawRect(0, 0, width(), height());
    28.  
    29. mypainter.setCompositionMode(QPainter::CompositionMode_SourceIn);
    30.  
    31. QLinearGradient rect_gradient(0, 0, 0, height());
    32. rect_gradient.setColorAt(0, Qt::red);
    33. rect_gradient.setColorAt(.17, Qt::yellow);
    34. rect_gradient.setColorAt(.33, Qt::green);
    35. rect_gradient.setColorAt(.50, Qt::cyan);
    36. rect_gradient.setColorAt(.66, Qt::blue);
    37. rect_gradient.setColorAt(.81, Qt::magenta);
    38. rect_gradient.setColorAt(1, Qt::red);
    39. mypainter.setBrush(rect_gradient);
    40. mypainter.drawRect(width() / 2, 0, width() / 2, height());
    41.  
    42. QPainter p(this);
    43. p.drawPixmap(0,0,pix);
    44.  
    45. }
    46. };
    47.  
    48. int main(int argc, char* argv[])
    49. {
    50. QApplication a(argc, argv);
    51.  
    52. QWidget parent;
    53. parent.setBackgroundRole(QPalette::ToolTipBase);
    54. test t(&parent);
    55. parent.show();
    56.  
    57. return a.exec();
    58. }
    To copy to clipboard, switch view to plain text mode 

  10. #10
    Join Date
    Apr 2011
    Posts
    58
    Thanks
    1

    Default Re: transparency problem. pls help.

    You are the man!!! THANK YOU!!

Similar Threads

  1. QX11EmbedContainer transparency problem
    By nalexvs in forum Qt Programming
    Replies: 0
    Last Post: 2nd December 2010, 18:38
  2. Unsetting transparency
    By Pavel Abrosimov in forum Qt Programming
    Replies: 2
    Last Post: 8th October 2009, 12:14
  3. Problem with a dialog transparency
    By ManicQin in forum Qt Programming
    Replies: 0
    Last Post: 27th May 2009, 09:11
  4. Transparency ... Again
    By EricF in forum Qt Programming
    Replies: 4
    Last Post: 1st December 2007, 19:52
  5. transparency
    By roms18 in forum Qt Programming
    Replies: 2
    Last Post: 16th February 2006, 19:38

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
  •  
Qt is a trademark of The Qt Company.