Results 1 to 4 of 4

Thread: Implementing fade-in / fade-out for a modal QDialog

  1. #1
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Implementing fade-in / fade-out for a modal QDialog

    Here's a summary of today's adventures in Qt5. Maybe this will be of some help to others.

    I would like to implement a fade-in / fade-out feature for modal dialogs derived from QDialog. I did this for a QML-based modal Window and liked the effect, using a QML PropertyAnimation on the opacity property.

    It should be possible to duplicate this behavior with QDialog using a QPropertyAnimation on the QWidget::windowOpacity property. I display the dialog using QDialog::exec().

    I created two QPropertyAnimation instances, one for fade-in, one for fade-out as members of my dialog class.

    I implemented the fade-in feature by overriding the showEvent() and starting the fade-in animation there. This works fine - the dialog slowly fades into view as expected.

    I had trouble with the fade-out implementation. I tried overriding the hideEvent(), but this is too late. Qt has already hidden the window by the time this event occurs, so the fade out animation does nothing. I've also tried it in the closeEvent(), but this method is not called - hideEvent() is called instead. I assume that this is because my QDialog is not a top-level widget, but a child of the QMainWindow.

    The hideEvent() also occurs before QDialog:exec() returns, so it can't be trapped there either without re-implementing that method.

    So what I finally hit on was to override the QDialog::done() slot. I also added a new slot, onFadeOutFinished() and connected this to the QPropertyAnimation::finished() signal for the fade-out instance. In my done() slot, I start the fade-out animation, and in the finished() signal handler slot, I call the actual QDialog::done() slot.

    The code looks like this:

    Qt Code:
    1. FaderDialog::FaderDialog( QWidget * parent ) : QDialog( parent ), mResult( 0 )
    2. {
    3. // Create the fade-in / fade-out animators
    4.  
    5. mpFadeIn = new QPropertyAnimation( this, "windowOpacity" );
    6. mpFadeIn->setDuration( 150 );
    7. mpFadeIn->setStartValue( 0.0 );
    8. mpFadeIn->setEndValue( 1.0 );
    9.  
    10. mpFadeOut = new QPropertyAnimation( this, "windowOpacity" );
    11. mpFadeOut->setDuration( 500 );
    12. mpFadeOut->setStartValue( 1.0 );
    13. mpFadeOut->setEndValue( 0.0 );
    14.  
    15. connect( mpFadeOut, SIGNAL( finished() ), this, SLOT( onFadeOutFinished() ) );
    16. }
    17.  
    18. void FaderDialog::showEvent( QShowEvent * )
    19. {
    20. mpFadeIn->start();
    21. }
    22.  
    23. void FaderDialog::done( int result )
    24. {
    25. mResult = result; // remember the result in a member variable
    26. mpFadeOut->start();
    27. }
    28.  
    29. void FaderDialog::onFadeOutFinished()
    30. {
    31. QDialog::done( mResult ); // now call the real done() slot
    32. }
    To copy to clipboard, switch view to plain text mode 

    There seems to be one glitch - the whole application "blinks" just as the fade-out animation starts. It seems that changing the opacity of the dialog causes a paint event in the parent. I will look into ways to counteract that. I think it is possible - if I do the same thing with a QML dialog, I don't think I see a blink.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Implementing fade-in / fade-out for a modal QDialog

    Not related to you blink problem, but one other thing you could try for the start of hide animation is overwriting setVisible()

    Cheers,
    _

  3. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Implementing fade-in / fade-out for a modal QDialog

    Quote Originally Posted by anda_skoa View Post
    Not related to you blink problem, but one other thing you could try for the start of hide animation is overwriting setVisible()
    Yes, I thought about that also. In the case of a non-modal dialog, that's probably where it would have to be done, but I can't be sure (without looking at the source code) that calling hide() might be an end-run around setVisible( false ), so it might have to be implemented in both places.

    Since a modal dialog vectors everything through the done() slot this seemed like a good place to intervene.

    Still don't know about the blink problem - I'm guessing that the window system manages things in the case of simply changing the visibility, but changing some other window property triggers the repaint. I won't worry about it until it becomes more annoying.

  4. #4
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Implementing fade-in / fade-out for a modal QDialog

    I can't be sure (without looking at the source code) that calling hide() might be an end-run around setVisible( false )
    From the docs:
    Hides the widget. This function is equivalent to setVisible(false).
    sources:
    Qt Code:
    1. void QWidget::hide()
    2. {
    3. setVisible(false);
    4. }
    5.  
    6. void QWidget::show()
    7. {
    8. bool isPopup = data->window_flags & Qt::Popup & ~Qt::Window;
    9. if (isWindow() && !isPopup && qApp->styleHints()->showIsFullScreen())
    10. showFullScreen();
    11. else
    12. setVisible(true);
    13. }
    14.  
    15. void QWidget::setHidden(bool hidden)
    16. {
    17. setVisible(!hidden);
    18. }
    19.  
    20. // sorry i forgot this one:
    21. void QWidget::showFullScreen()
    22. {
    23. ...
    24. ...
    25. setVisible(true);
    26. ...
    27. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by stampede; 30th October 2013 at 07:15. Reason: updated contents

Similar Threads

  1. QLabel Text fade in/out
    By lamp in forum Qt Programming
    Replies: 6
    Last Post: 6th October 2016, 09:12
  2. QGraphicsView scroll fade out
    By Cruz in forum Qt Programming
    Replies: 1
    Last Post: 30th September 2013, 10:03
  3. video fade in/out in phonon
    By panpanpandas in forum Qt Programming
    Replies: 2
    Last Post: 31st July 2011, 08:40
  4. Desktop wallpaper Fade In
    By Dan` in forum Qt Programming
    Replies: 4
    Last Post: 30th November 2010, 10:14
  5. fade from one colour to another
    By panduro in forum Newbie
    Replies: 2
    Last Post: 18th June 2008, 13:30

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.