【问题标题】:Transition effect when showing a custom widget in Qt在 Qt 中显示自定义小部件时的过渡效果
【发布时间】:2013-01-21 15:35:59
【问题描述】:

我想逐步显示一个自定义小部件,例如从 0 不透明度开始并在给定时间内达到 100。是否有可能以为此目的开发的简单 Qt 方式来做到这一点?或者我可以自己做吗?

干杯,

【问题讨论】:

    标签: qt qt4 widget effects transitions


    【解决方案1】:

    QGraphicsOpacityEffectQPropertyAnimation 一起使用会更简单,这正是为这样的东西而设计的。您需要做的就是将您的QGraphicsOpacityEffect 附加到您的QWidget 后代,然后将其设置为新创建的QPropertyAnimation 的目标,并使用您想要的选项运行QPropertyAnimation

    【讨论】:

      【解决方案2】:

      您可以尝试使用计时器,它会逐渐调整小部件在节目事件中的不透明度。举这个例子(写的很匆忙):

      class MyWidget: public QWidget{
      public:
          MyWidget(QWidget* parent) : QWidget(parent){
             //setup the timer first
             timer.setInterval(xxx);
             connect(&timer, SIGNAL(timeOut()),this, SLOT(increaseOpacity()));
          };
      
      protected:
          virtual void MyWidget::showEvent ( QShowEvent * event ){
              opacity = 0;
              setOpacity(opacity);
              QWidget::showEvent(event);
              timer.start();
          }
      
          virtual void MyWidget::hideEvent ( QHideEvent * event ){
              QWidget::hideEvent(event);
              timer.stop();
          }
      
      private slot:
          void increaseOpacity(){
              if(opacity>=1.0){
                 timer.stop();
              }
              opacity += 0.1;
              setOpacity(opacity);
          }
      private:
         Qtimer timer;
         double opacity; 
      

      }

      【讨论】:

      • 不是 setOpacity 方法。该方法只为窗口设置不透明度,并调用另一个。
      • 我不明白你的意思。你能解释一下吗?
      • 在您的示例中,您使用 setOpacity 方法。该方法不存在。您提供的 url 包含有关 setWindowOpacity 的信息,而不是有关 setOpacity 的信息。这意味着,您的示例将出现编译错误。
      【解决方案3】:

      如果你的 QWidget 是窗口,你可以使用:

      #include <QApplication>
      #include <QWidget>
      #include <QPropertyAnimation>
      
      int main(int argc, char *argv[])
      {
          QApplication a(argc, argv);
          QWidget w;
          w.show();
      
          QPropertyAnimation animation(&w, "windowOpacity");
          animation.setDuration(10000);
          animation.setStartValue(1.0);
          animation.setEndValue(0.0);
      
          animation.start();
      
          return a.exec();
      }
      

      其他:

      #include <QApplication>
      #include <QWidget>
      #include <QPropertyAnimation>
      #include <QGraphicsOpacityEffect>
      #include <QPushButton>
      
      int main(int argc, char *argv[])
      {
          QApplication a(argc, argv);
          QWidget w;
          QPushButton b(&w);
          w.show();
      
          QGraphicsOpacityEffect effect;
          b.setGraphicsEffect(&effect);
      
          QPropertyAnimation animation(&effect, "opacity");
          animation.setDuration(1000);
          animation.setStartValue(1.0);
          animation.setEndValue(0.0);
      
          animation.start();
      
          return a.exec();
      }
      

      最好的简单方法是为这个目标使用 QML:

      import QtQuick 2.0
      
      Rectangle {
          width: 360
          height: 360
      
          Rectangle {
              width: 100
              height: 100
              color: "red"
      
              NumberAnimation on opacity {
                  from: 1.0; to: 0.0
                  duration: 5000
              }
          }
      }
      

      【讨论】:

      • 第二种情况,我正在尝试为按钮设置动画。这是行不通的。无论如何感谢您的回答。
      • @DídacPérez 在我回答之前,我已经编译了所有案例。第二种情况你有什么问题?
      • 嗨米洛维多夫。只是它不起作用,一旦我运行上面的行,什么都不会发生:-(我在 Windows 7 6 位上使用 Qt 4.8.4。
      • @DídacPérez 你有任何编译错误吗?说实话,我是在 Qt5.0.0 上编译的,但我认为在 Qt 4.8.4 上也必须工作。
      猜你喜欢
      • 1970-01-01
      • 2020-03-02
      • 1970-01-01
      • 2012-08-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多