Results 1 to 3 of 3

Thread: Alpha mask on QScrollArea

  1. #1
    Join Date
    Feb 2010
    Posts
    18
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Alpha mask on QScrollArea

    Hi,
    I've got a vertical QScrollArea which I add custom widgets to which sits on a semi transparent QFrame and the user can scroll this up and down. If there are lots of widgets in the scroll area they obviously don't get rendered outside the scroll area's region but this causes a 'hard' cut-off and I'd like the widgets to fade in as the use scrolls up and down.

    Is there any way to do this using an alpha mask? I've tried using stylesheets but that just paints the background of the QScrollArea and not the widgets themselves.

    Any ideas?

    I suppose this is similar to the end credits of a film where instead of the credits disappearing off the top of the screen they fade out with ever decreasing opacity.

  2. #2
    Join Date
    Sep 2011
    Location
    Manchester
    Posts
    538
    Thanks
    3
    Thanked 106 Times in 103 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Alpha mask on QScrollArea

    I don't know about alpha mask, but it can be easily done using simple widget with reimplemented paintEvent.

    Something like that:
    Qt Code:
    1. // gradient widget
    2. MyGradient::MyGradient( QWidget* parent )
    3. :
    4. QWidget( parent )
    5. {
    6. parent->installEventFilter( this );
    7. this->setAttribute( Qt::WA_TransparentForMouseEvents, true ); // important
    8. }
    9.  
    10. bool MyGradient::eventFilter( QObject* o, QEvent* e )
    11. {
    12. if( o == this->parentWidget() && e->type() == QEvent::Resize )
    13. {
    14. this->setGeometry( this->parentWidget()->rect() );
    15. }
    16.  
    17. return false;
    18. }
    19.  
    20. void MyGradient::paintEvent( QPaintEvent* e )
    21. {
    22. int fade_out_length = 100;
    23. QColor startColor( 0, 0, 0, 0 );
    24. QColor endColor( 0, 0, 0, 255 );
    25. QRect r = this->rect();
    26.  
    27. QLinearGradient gradient_top( r.width()/2, fade_out_length, r.width()/2, 0 );
    28. gradient_top.setColorAt( 0.0, startColor );
    29. gradient_top.setColorAt( 1.0, endColor );
    30.  
    31. QLinearGradient gradient_bottom( r.width()/2, r.height() - fade_out_length, r.width()/2, r.bottom() );
    32. gradient_bottom.setColorAt( 0.0, startColor );
    33. gradient_bottom.setColorAt( 1.0, endColor );
    34.  
    35. QPainter p( this );
    36. p.setBrush( gradient_top );
    37. p.drawRect( this->rect() );
    38.  
    39. p.setBrush( gradient_bottom );
    40. p.drawRect( this->rect() );
    41. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. // how to use it
    2. MainWindow::MainWindow( QWidget* parent )
    3. : QMainWindow( parent )
    4. {
    5. QLabel* label = new QLabel();
    6. label->setPixmap( QPixmap( "C:\\Users\\rob\\Documents\\startrek.jpg" ) );
    7.  
    8. QScrollArea* scrollArea = new QScrollArea();
    9. scrollArea->setHorizontalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
    10. scrollArea->setWidget( label );
    11.  
    12. new MyGradient( scrollArea->viewport() ); // the interesting part
    13.  
    14. this->setCentralWidget( scrollArea );
    15. }
    To copy to clipboard, switch view to plain text mode 
    I hope this helps.
    Last edited by Spitfire; 14th February 2012 at 10:46.

  3. The following user says thank you to Spitfire for this useful post:

    JonnyJP (16th February 2012)

  4. #3
    Join Date
    Feb 2010
    Posts
    18
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Alpha mask on QScrollArea

    Thanks.

    I'll try it out and see how I get on...

Similar Threads

  1. How it create a true alpha mask from a QImage?
    By nokkie in forum Qt Programming
    Replies: 1
    Last Post: 3rd May 2010, 21:07
  2. QTermWidget alpha
    By un-defined in forum Qt-based Software
    Replies: 0
    Last Post: 20th May 2008, 21:52
  3. Replies: 2
    Last Post: 10th March 2008, 20:16
  4. Alpha blending..
    By chethana in forum Qt Programming
    Replies: 1
    Last Post: 9th October 2007, 10:15
  5. image alpha
    By murko81 in forum Qt Programming
    Replies: 13
    Last Post: 28th October 2006, 19:31

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.