Results 1 to 7 of 7

Thread: Set opacity in Linux

  1. #1
    Join Date
    Jun 2011
    Location
    Porto Alegre, Brazil
    Posts
    482
    Thanks
    165
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Set opacity in Linux

    Hello!

    I want to add some "fading" effect (I guess that's the name) in a pop-up widget in one of my applications for Linux when the widget is both shown and hidden. I'ld guess that the normal way of doing this is to add a QPropertyAnimation that would change the window's opacity from 0 to 255 in a period of time (lets say 500 ms).

    Now the problem is that in the Linux I'm using window opacity is not available; so I both can't change the window opacity with QProperyAnimation, nor with setWindowOpacity(qreal).


    The question: how do I do this, then? I tried with stylesheet (connecting a slot to the QProperyAnimation and changing the window's background-color alpha channel from 0 to 255) but it doesn't seem to be interesting setting a new stylesheet so quickly (calling a set QString method frequently), nor is it elegant.

    I wonder if there is another way of doing this for this circumstance that is not by stylesheet?


    I'm glad for any help,

    Momergil


    Note: using Qt 4.8 for ARM
    Last edited by Momergil; 17th June 2014 at 15:22.
    May the Lord be with you. Always.

  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: Set opacity in Linux

    Quote Originally Posted by Momergil View Post
    Now the problem is that in the Linux I'm using window opacity is not available
    Yes, it is.

    Just tried with
    Qt Code:
    1. #include <QApplication>
    2. #include <QLabel>
    3. #include <QPropertyAnimation>
    4.  
    5. int main(int argc, char ** argv)
    6. {
    7. QApplication app(argc, argv);
    8.  
    9. QLabel label("Hello World");
    10.  
    11. label.show();
    12.  
    13. QPropertyAnimation fade(&label, "windowOpacity");
    14. fade.setStartValue(1.0);
    15. fade.setEndValue(1.0);
    16. fade.setKeyValueAt(0.5, 0.0);
    17. fade.setDuration(5000);
    18.  
    19. fade.start();
    20.  
    21. return app.exec();
    22. }
    To copy to clipboard, switch view to plain text mode 

    A window with the label in it appears, fades out and fades in again.

    Cheers,
    _

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

    Momergil (17th June 2014)

  4. #3
    Join Date
    Jun 2011
    Location
    Porto Alegre, Brazil
    Posts
    482
    Thanks
    165
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Set opacity in Linux

    Hello anda_skoa!

    First, thanks for the reply.

    Second, I was quite interested when you provided a direct use of QPropertyAnimation as a solution to my problem, since (as I mentioned above) I've tried that solution before without success. I decided to give a try, nonetheless, and for my surprise your code actually worked.

    I began to make comparisons between your QPropertyAnimation implementation and mine (having done the proper adaptations) and despite various checkings and debugs, I simply couldn't make my code work; the fade effect simply don't happen.

    So the code I'm using is below. As you may see, it's supposed to be a replacement of QMessageBox that is activated once the user clicks in a place, when the show1() method is called. It should than start the fade effect on poBaseWidget, and once the user clicks on it, a new closing fade should occur. Could you please tell me why it's not working?

    Thanks,

    Momergil

    Qt Code:
    1. class MessageFrame : public QFrame
    2. {
    3. Q_OBJECT
    4. Q_PROPERTY(QString currentText READ currentText WRITE setCurrentText)
    5. Q_ENUMS(Icon)
    6. Q_ENUMS(TextType)
    7.  
    8. public:
    9. enum Icon { Question, Information, Warning, Critical };
    10. enum TextType { ScreenDescription, GeneralText };
    11.  
    12. MessageFrame(QWidget* parent = 0) :
    13. QFrame(parent),
    14. poBaseWidget(new QFrame(this))
    15. {
    16. setObjectName("MessageFrame");
    17. setGeometry(0,20,800,460);
    18.  
    19. //
    20. poBaseWidget->setGeometry(120,0,560,100);
    21. poBaseWidget->setStyleSheet("* { background-color: rgb(192,192,192);"
    22. "background-origin: content;"
    23. "border: 2px rgb(128,128,127);"
    24. "border-radius: 10px; }");
    25.  
    26. //
    27. iconLabel = new QLabel(poBaseWidget);
    28. iconLabel->resize(32,32);
    29. setIcon(Information);
    30.  
    31. //
    32. textLabel = new QLabel(poBaseWidget);
    33. textLabel->setWordWrap(true);
    34. textLabel->setMaximumSize(textLabel->parentWidget()->width() - iconLabel->width() - 20, textLabel->parentWidget()->height() - 10);
    35. textLabel->resize(textLabel->maximumSize());
    36.  
    37. //
    38. startFadeAnimation = new QPropertyAnimation(poBaseWidget, "windowOpacity",this);
    39. startFadeAnimation->setStartValue(0.0);
    40. startFadeAnimation->setEndValue(1.0);
    41. startFadeAnimation->setDuration(5000);
    42.  
    43. stopFadeAnimation = new QPropertyAnimation(poBaseWidget, "windowOpacity",this);
    44. stopFadeAnimation->setStartValue(1.0);
    45. stopFadeAnimation->setEndValue(0.0);
    46. stopFadeAnimation->setDuration(5000);
    47.  
    48. connect(stopFadeAnimation,SIGNAL(finished()),this,SLOT(close()));
    49.  
    50. //
    51. poBaseWidget->installEventFilter(this);
    52.  
    53. hide(); //hide();
    54. }
    55.  
    56. virtual ~MessageFrame()
    57. {
    58. poBaseWidget->removeEventFilter(this);
    59. }
    60.  
    61. inline QString currentText() const;
    62.  
    63. void setCurrentText(const QString& text);
    64.  
    65. void setIcon(const Icon icon);
    66.  
    67. static void information(const QString& text);
    68.  
    69. static void warning(const QString& text);
    70.  
    71. static void critical(const QString& text);
    72.  
    73. private:
    74. QLabel* textLabel,
    75. * iconLabel;
    76. QPropertyAnimation* startFadeAnimation,
    77. * stopFadeAnimation;
    78. QString screenText;
    79. QFrame* poBaseWidget;
    80.  
    81. protected:
    82. bool eventFilter(QObject *obj, QEvent *ev)
    83. {
    84. if (obj == poBaseWidget && ev->type() == QEvent::MouseButtonRelease)
    85. {
    86. stopFadeAnimation->start();
    87. return true;
    88. }
    89. else
    90. return QFrame::eventFilter(obj,ev);
    91. }
    92.  
    93. public slots:
    94. void show1(const TextType type = GeneralText)
    95. {
    96. switch (type)
    97. {
    98. case GeneralText: //last set text
    99. break;
    100.  
    101. case ScreenDescription:
    102. // setCurrentText(screenText);
    103. break;
    104. }
    105.  
    106. show();
    107.  
    108. startFadeAnimation->start();
    109. }
    110.  
    111. inline void setScreenText(const QString& text) { screenText = text; }
    112.  
    113. /////////////////////////////////////////////////////
    114. /// SINGLETON STUFF - DON'T TOUCH
    115. public:
    116. static void start(QWidget* parent = 0);
    117.  
    118. static void MessageFrameDestroy();
    119.  
    120. inline static MessageFrame& instance();
    121.  
    122. private:
    123. static MessageFrame* poInstance;
    124. };
    To copy to clipboard, switch view to plain text mode 
    Last edited by Momergil; 17th June 2014 at 16:21.
    May the Lord be with you. Always.

  5. #4
    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: Set opacity in Linux

    You are animating windowOpacity of poBaseWidget instead of your MessageFrame instance. Only top-level windows can have their windowOpacity manipulated.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. The following user says thank you to wysota for this useful post:

    Momergil (18th June 2014)

  7. #5
    Join Date
    Jun 2011
    Location
    Porto Alegre, Brazil
    Posts
    482
    Thanks
    165
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Set opacity in Linux

    Quote Originally Posted by wysota View Post
    You are animating windowOpacity of poBaseWidget instead of your MessageFrame instance. Only top-level windows can have their windowOpacity manipulated.
    Oh, I remember reading that in the manual; I didn't noticed I was doing that in my code. Anyway, why is that so? And, well, if the method show by anda_skoa is valid only for top-level windows, I'll restate my first question to: is there a way to create such a "fade" effect that is not by stylesheet and that is valid for any widget I want to apply the effect regardless of being a top level widget or not?
    May the Lord be with you. Always.

  8. #6
    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: Set opacity in Linux

    So you want the content of the window to fade, not the window itself?

    Maybe usingQGraphicsOpacityEffect on the widget which you want to fade.

    Cheers,
    _

  9. The following user says thank you to anda_skoa for this useful post:

    Momergil (18th June 2014)

  10. #7
    Join Date
    Jun 2011
    Location
    Porto Alegre, Brazil
    Posts
    482
    Thanks
    165
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Set opacity in Linux

    Quote Originally Posted by anda_skoa View Post
    So you want the content of the window to fade, not the window itself?
    Exactly, and your suggestion worked perfectly. Thanks!

    Momergil
    May the Lord be with you. Always.

Similar Threads

  1. QMainWindow Opacity
    By Henry Blue Heeler in forum Newbie
    Replies: 3
    Last Post: 11th January 2014, 21:25
  2. change the opacity
    By askatuak in forum Qt Quick
    Replies: 1
    Last Post: 18th September 2013, 16:20
  3. Plasmoid opacity
    By AcerExtensa in forum KDE Forum
    Replies: 3
    Last Post: 25th July 2009, 14:22
  4. Window Opacity
    By chethana in forum Qt Programming
    Replies: 1
    Last Post: 10th October 2007, 10:45
  5. Opacity in Linux OS
    By shyam prasad in forum Qt Programming
    Replies: 2
    Last Post: 22nd May 2007, 05:59

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.