Results 1 to 5 of 5

Thread: QGraphicsOpacityEffect and QLabel

  1. #1
    Join Date
    Oct 2009
    Posts
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QGraphicsOpacityEffect and QLabel

    I want to animate error messages inside the status bar of my application.
    I use a QLabel for the statusbar.

    On status bar change i do the following:

    Qt Code:
    1. void sectiona::statusBarChanged(const QString& text, bool message)
    2. {
    3. QLabel* b=m_gui->m_textOut;
    4. QString newtext;
    5. oldStatusBarText=b->text();
    6. if(message==false)
    7. {
    8. b->setText(text);
    9. return;
    10. }else //animated message
    11. {
    12. newtext=text;
    13. newtext=newtext.prepend("<font color=\"red\"><b>").append("</b></font>");
    14. b->setText(newtext);
    15. QGraphicsOpacityEffect* effect=new QGraphicsOpacityEffect(b);
    16. b->setGraphicsEffect(effect);
    17. QPropertyAnimation* animation=new QPropertyAnimation(effect,"opacity");
    18. effect->setOpacity(0.0);
    19. animation->setDuration(3000);
    20. animation->setKeyValueAt(0.0,1.0);
    21. animation->setKeyValueAt(0.9,1.0);
    22. animation->setKeyValueAt(1.0,0.0);
    23. animation->setEasingCurve(QEasingCurve::InOutCubic);
    24. connect(animation,SIGNAL(finished()),this,SLOT(statusBarAnimationFinished()));
    25. animation->start(QAbstractAnimation::DeleteWhenStopped);
    26. };
    27. }
    To copy to clipboard, switch view to plain text mode 

    if the new status bar is a temporary message I animate the fadeout for 3seconds and then on the finish of the animation I restore the permanent status bar text:

    Qt Code:
    1. void sectiona::statusBarAnimationFinished()
    2. {
    3. QLabel* b=m_gui->m_textOut;
    4. QGraphicsOpacityEffect* effect=new QGraphicsOpacityEffect(b);
    5. effect->setOpacity(0.0);
    6. qDebug()<<effect->opacity();
    7. b->setText(oldStatusBarText);
    8. qDebug()<<effect->opacity();
    9. b->setGraphicsEffect(effect);
    10. qDebug()<<effect->opacity();
    11. b->setText(oldStatusBarText);
    12. qDebug()<<effect->opacity();
    13. effect->setOpacity(1.0);
    14. qDebug()<<effect->opacity();
    15. }
    To copy to clipboard, switch view to plain text mode 

    It works fine except a minor detail. The qDebug statements return:
    Qt Code:
    1. 0
    2. 0
    3. 0
    4. 0
    5. 1
    To copy to clipboard, switch view to plain text mode 
    , so the opacity changes only on the final statement, <b>BUT</b> the previous text blinks for a frame before setting the oldStatusBarText
    I want to smoothly disappear the message, and the just update the QLabel with the previous text.
    Any ideas?

  2. #2
    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: QGraphicsOpacityEffect and QLabel

    Why do you think the opacity should change? You create a new effect in the statusBarAnimationFinished() slot, set the opacity to 0, then don't do anything with it except print its value until you finally set the value to 1 and print it again. The code is doing exactly what you've told it to do.

    Are you confusing this effect instance with the effect instance that you create in the statusBarChanged() slot? The one you create in the statusBarAnimationFinished() slot is not the one you installed on the label in the statusBarChanged() slot, it's a new one. And in any case, by the time the statusBarAnimationFinished() slot is called, the original effect has an opacity of 1, because that's what you've told the animation to do and it doesn't emit its finished() signal until it's done doing it.

  3. #3
    Join Date
    Oct 2009
    Posts
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsOpacityEffect and QLabel

    It's difficult to explain the problem without seeing it.
    Let me be more detailed:

    1. statusBar text is "mode 1". in black
    2. I emit an error message "Error 1" that is catched be statusBarChanged("Error 1", true)
    3. I made a red bold message of "Error 1" and set it to statusBar.
    4. Status bar is now "error 1" in red bold
    5. I create a QGraphicsOpacityEffect with initial opacity 1.0 (setKeyValueAt(0.0,1.0)) and final opacity 0.0 (setKeyValueAt(1.0,0.0)).
    6. I start the animation .
    7. "error 1" in statusBar fades out to invisible (opacity 0.0 at 1.0time).
    8. signal "finished()" is triggered and slot "statusBarAnimationFinished" is executed.
    9. I change the text back to oldstatusBar "mode 1" checking all the time if opacity is still 0.0 invisible.
    10. After settings the text back to "mode 1" I change the opacity back to 1.0 and check if the value has changed.

    All the above logic and the opacity values I get are correct, but in the steps 8-10 I get the following behaviour:
    "error 1" is invisible and is made visible again for one ugly frame, before "mode 1" is shown in statusBar. I want to know if there is a way to avoid this "opacity jump" in the logic of the above.

    Is it more clear now ?

    I think the problem is related to some standard effect animation inside QLabel setText change, and not in my code. But I cannot figure it out.

  4. #4
    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: QGraphicsOpacityEffect and QLabel

    The instances of QGraphicsOpacityEffect in your two slots are unrelated, even though you are supplying the QLabel instance when you create them. In the statusBarAnimationFinished() slot, you never call b->setGraphicsEffect(effect); so the label is still using the old effect from the slot that starts the animation. This might account for the "ugly" behavior you are seeing. Changing the opacity of the new effect instance in the finished() slot does nothing because you haven't set it on the label instance. You're just changing the opacity of an effect that isn't hooked up to anything.

  5. #5
    Join Date
    Oct 2009
    Posts
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsOpacityEffect and QLabel

    Thanks for the correction. I added b->setGraphicsEffect(effect); on the slot you mentioned. But even If I set the oldtext on the status bar before it is shown, I see the "frame" with the "error 1" with full opacity before updating to oldtext. So I concluded that my kde, uses its own animations for changing text in qlabel and somehow it conflicts with my animation.
    So I decided to drop the animation anyway, because it is to jumpy like that, and use a simple setText with a timer.

Similar Threads

  1. Replies: 1
    Last Post: 22nd July 2013, 15:09
  2. Replies: 0
    Last Post: 3rd September 2012, 12:11
  3. Replies: 1
    Last Post: 4th August 2010, 14:06
  4. Replies: 1
    Last Post: 29th September 2009, 19:44

Tags for this Thread

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.