Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. how to make the widget fade out with the animation object?

how to make the widget fade out with the animation object?

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 3 Posters 514 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • nicker playerN Offline
    nicker playerN Offline
    nicker player
    wrote on last edited by
    #1
    int Dialog::onAnimationFade(QWidget*from,QWidget* to){
        //m_Animation = new QLabel();
        m_Animation->setParent(from);
        m_Animation->resize(from->size());
        m_Animation->setPixmap(from->grab());
        m_Animation->show();
        from->hide();
        QPropertyAnimation *animation = new QPropertyAnimation(m_Animation,"windowOpacity");
        animation->setDuration(600);
        animation->setStartValue(0.95);
        animation->setEndValue(0.30);
        animation->setEasingCurve(QEasingCurve::BezierSpline);
    
        QObject::connect(animation, &QPropertyAnimation::finished, [to,this]() {
            to->show();
            m_Animation->hide(); 
        });
        animation->start();
    }
    

    when i used the codes upside.the widget of the 'from' dosent fade out smoothly, it just disappeared suddenly.the animation seemed dont work.

    Pl45m4P 1 Reply Last reply
    0
    • nicker playerN nicker player
      int Dialog::onAnimationFade(QWidget*from,QWidget* to){
          //m_Animation = new QLabel();
          m_Animation->setParent(from);
          m_Animation->resize(from->size());
          m_Animation->setPixmap(from->grab());
          m_Animation->show();
          from->hide();
          QPropertyAnimation *animation = new QPropertyAnimation(m_Animation,"windowOpacity");
          animation->setDuration(600);
          animation->setStartValue(0.95);
          animation->setEndValue(0.30);
          animation->setEasingCurve(QEasingCurve::BezierSpline);
      
          QObject::connect(animation, &QPropertyAnimation::finished, [to,this]() {
              to->show();
              m_Animation->hide(); 
          });
          animation->start();
      }
      

      when i used the codes upside.the widget of the 'from' dosent fade out smoothly, it just disappeared suddenly.the animation seemed dont work.

      Pl45m4P Offline
      Pl45m4P Offline
      Pl45m4
      wrote on last edited by Pl45m4
      #2

      @nicker-player said in how to make the widget fade out with the animation object?:

      animation->setEasingCurve(QEasingCurve::BezierSpline);

      What's the point for this?
      If you want to fade a widget out, I suggest using

      QEasingCurve::OutQuad
      or
      QEasingCurve::Linear

      Also why is your end value for "opacity" 0.30 ( = 70% transparent / 30% opaque )?
      If you want to make it fade-out completely, it needs to end at 0.00.
      In addition I would set the start value to 1.00.

      Or why do you use 95% to 30%?

      Is it ChatGPT code above? :DD

      Edit:

      What I've noticed afterwards:

      Besides all what I wrote above regarding your animation config:
      Making from the parent of m_animation and then hiding from, will also hide all children... including m_animation.
      Which makes everything else (the screen grabbing, the animation...) completely useless, as you won't see any of it anyway.


      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      nicker playerN 1 Reply Last reply
      2
      • S Offline
        S Offline
        SimonSchroeder
        wrote on last edited by
        #3

        Here is an excerpt from the documentation of QWidget about translucent widgets:

        To enable this feature in a top-level widget, set its Qt::WA_TranslucentBackground attribute with setAttribute() and ensure that its background is painted with non-opaque colors in the regions you want to be partially transparent.

        Platform notes:

        • X11: This feature relies on the use of an X server that supports ARGB visuals and a compositing window manager.
        • Windows: The widget needs to have the Qt::FramelessWindowHint window flag set for the translucency to work.
        • macOS: The widget needs to have the Qt::FramelessWindowHint window flag set for the translucency to work.

        There is some additional information for the attribute autoFillBackground . Maybe the suggestions for this attribute already work.

        1 Reply Last reply
        0
        • Pl45m4P Pl45m4

          @nicker-player said in how to make the widget fade out with the animation object?:

          animation->setEasingCurve(QEasingCurve::BezierSpline);

          What's the point for this?
          If you want to fade a widget out, I suggest using

          QEasingCurve::OutQuad
          or
          QEasingCurve::Linear

          Also why is your end value for "opacity" 0.30 ( = 70% transparent / 30% opaque )?
          If you want to make it fade-out completely, it needs to end at 0.00.
          In addition I would set the start value to 1.00.

          Or why do you use 95% to 30%?

          Is it ChatGPT code above? :DD

          Edit:

          What I've noticed afterwards:

          Besides all what I wrote above regarding your animation config:
          Making from the parent of m_animation and then hiding from, will also hide all children... including m_animation.
          Which makes everything else (the screen grabbing, the animation...) completely useless, as you won't see any of it anyway.

          nicker playerN Offline
          nicker playerN Offline
          nicker player
          wrote on last edited by
          #4

          @Pl45m4
          I just follow your tips.rem the code of the from->hide();.but how to figure out the result what i wanted.
          widget a faded out and the other one faded in.
          so that the user could change the ui interface with the animations.

          Pl45m4P 1 Reply Last reply
          0
          • nicker playerN nicker player

            @Pl45m4
            I just follow your tips.rem the code of the from->hide();.but how to figure out the result what i wanted.
            widget a faded out and the other one faded in.
            so that the user could change the ui interface with the animations.

            Pl45m4P Offline
            Pl45m4P Offline
            Pl45m4
            wrote on last edited by Pl45m4
            #5

            @nicker-player said in how to make the widget fade out with the animation object?:

            how to figure out the result what i wanted.

            How should we know what you want?
            I used the code you have posted and corrected the obvious issues.

            Have you also corrected the animation?
            Does it work now?
            What do you expect then?

            There is nothing about "fading in" in your current code.
            So you need the same property animation again (with an "in"-curve) and use it on your to widget.


            If debugging is the process of removing software bugs, then programming must be the process of putting them in.

            ~E. W. Dijkstra

            nicker playerN 1 Reply Last reply
            0
            • Pl45m4P Pl45m4

              @nicker-player said in how to make the widget fade out with the animation object?:

              how to figure out the result what i wanted.

              How should we know what you want?
              I used the code you have posted and corrected the obvious issues.

              Have you also corrected the animation?
              Does it work now?
              What do you expect then?

              There is nothing about "fading in" in your current code.
              So you need the same property animation again (with an "in"-curve) and use it on your to widget.

              nicker playerN Offline
              nicker playerN Offline
              nicker player
              wrote on last edited by
              #6

              @Pl45m4
              what I wanted is that one widget fade out and hide(),then another widget came out and show.but if I added both of the widgets,the layout became twice size .Bu f if I hided one of them,then the animations dont work.I saw that someone said that the windowOpacity only worked for the whole window ui object,but not the child object of the qwidget.So it is normally that it wont work ?

              Pl45m4P 1 Reply Last reply
              0
              • nicker playerN nicker player

                @Pl45m4
                what I wanted is that one widget fade out and hide(),then another widget came out and show.but if I added both of the widgets,the layout became twice size .Bu f if I hided one of them,then the animations dont work.I saw that someone said that the windowOpacity only worked for the whole window ui object,but not the child object of the qwidget.So it is normally that it wont work ?

                Pl45m4P Offline
                Pl45m4P Offline
                Pl45m4
                wrote on last edited by
                #7

                @nicker-player said in how to make the widget fade out with the animation object?:

                what I wanted is that one widget fade out and hide(),then another widget came out and show.but if I added both of the widgets,the layout became twice size .Bu f if I hided one of them,then the animations dont work.

                Yeah, I got that, but your initial code as well as your topic title is only about fading out :)

                So it is normally that it wont work ?

                No, you can make it work.
                With QStackedLayout, for example.

                • https://doc.qt.io/qt-6/qstackedlayout.html#details

                Put all widgets you want to replace via fading in and fading out on top of each other in the layout.
                Then write a function similar to yours above, where you add your animations and change the currently active widget in the layout.
                You simply always fade out the current one (currentIndex of QStackedLayout), and bring the widget of your choice back up to the front via "fade-in" animation. Change the active index as soon as the animation finishes.


                If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                ~E. W. Dijkstra

                nicker playerN 1 Reply Last reply
                1
                • Pl45m4P Pl45m4

                  @nicker-player said in how to make the widget fade out with the animation object?:

                  what I wanted is that one widget fade out and hide(),then another widget came out and show.but if I added both of the widgets,the layout became twice size .Bu f if I hided one of them,then the animations dont work.

                  Yeah, I got that, but your initial code as well as your topic title is only about fading out :)

                  So it is normally that it wont work ?

                  No, you can make it work.
                  With QStackedLayout, for example.

                  • https://doc.qt.io/qt-6/qstackedlayout.html#details

                  Put all widgets you want to replace via fading in and fading out on top of each other in the layout.
                  Then write a function similar to yours above, where you add your animations and change the currently active widget in the layout.
                  You simply always fade out the current one (currentIndex of QStackedLayout), and bring the widget of your choice back up to the front via "fade-in" animation. Change the active index as soon as the animation finishes.

                  nicker playerN Offline
                  nicker playerN Offline
                  nicker player
                  wrote on last edited by
                  #8

                  @Pl45m4
                  Ive tried what u just mentioned.but didnt work.someone said that the if u want to get the effect of the fade out for the child widget,you need to use the other class.heres the code i founded on somewhere.

                  QGraphicsOpacityEffect *m_pGraphicsOpacityEffect =
                      new QGraphicsOpacityEffect(ui->groupBox_login);
                      m_pGraphicsOpacityEffect->setOpacity(1.0);
                      ui->groupBox_login->setGraphicsEffect(m_pGraphicsOpacityEffect);
                      
                     QPropertyAnimation *animation =
                     new QPropertyAnimation(m_pGraphicsOpacityEffect,"opacity",ui->groupBox_login);
                     animation->setDuration(500);
                     animation->setStartValue(1);
                     animation->setEndValue(0);
                     animation->setEasingCurve(QEasingCurve::Linear);
                     animation->start();
                   [link text](http://www.lotfond.com/articles/detail?2023123101)
                  
                  Pl45m4P 1 Reply Last reply
                  0
                  • nicker playerN nicker player

                    @Pl45m4
                    Ive tried what u just mentioned.but didnt work.someone said that the if u want to get the effect of the fade out for the child widget,you need to use the other class.heres the code i founded on somewhere.

                    QGraphicsOpacityEffect *m_pGraphicsOpacityEffect =
                        new QGraphicsOpacityEffect(ui->groupBox_login);
                        m_pGraphicsOpacityEffect->setOpacity(1.0);
                        ui->groupBox_login->setGraphicsEffect(m_pGraphicsOpacityEffect);
                        
                       QPropertyAnimation *animation =
                       new QPropertyAnimation(m_pGraphicsOpacityEffect,"opacity",ui->groupBox_login);
                       animation->setDuration(500);
                       animation->setStartValue(1);
                       animation->setEndValue(0);
                       animation->setEasingCurve(QEasingCurve::Linear);
                       animation->start();
                     [link text](http://www.lotfond.com/articles/detail?2023123101)
                    
                    Pl45m4P Offline
                    Pl45m4P Offline
                    Pl45m4
                    wrote on last edited by Pl45m4
                    #9

                    @nicker-player

                    That might also work, but from my personal experience and other topics like this one (see @Devopia53 's reply there), the first method should work as well, if done correctly.


                    If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                    ~E. W. Dijkstra

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved