examples/effects/fademessage/fademessage.cpp
changeset 3 41300fa6a67c
parent 0 1918ee327afb
child 4 3b1da2848fc7
--- a/examples/effects/fademessage/fademessage.cpp	Tue Jan 26 12:42:25 2010 +0200
+++ b/examples/effects/fademessage/fademessage.cpp	Tue Feb 02 00:43:10 2010 +0200
@@ -43,7 +43,7 @@
 
 #include <QtGui>
 
-FadeMessage::FadeMessage(QWidget *parent): QGraphicsView(parent), m_index(0.0)
+FadeMessage::FadeMessage(QWidget *parent): QGraphicsView(parent)
 {
     setScene(&m_scene);
     setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
@@ -51,9 +51,12 @@
 
     setupScene();
 
-    m_timeLine = new QTimeLine(500, this);
-    m_timeLine->setCurveShape(QTimeLine::EaseInOutCurve);
-    connect(m_timeLine, SIGNAL(valueChanged(qreal)), m_effect, SLOT(setStrength(qreal)));
+    m_animation = new QPropertyAnimation(m_effect, "strength", this);
+    m_animation->setDuration(500);
+    m_animation->setEasingCurve(QEasingCurve::InOutSine);
+    m_animation->setStartValue(0);
+    m_animation->setEndValue(1);
+
 
     setRenderHint(QPainter::Antialiasing, true);
     setFrameStyle(QFrame::NoFrame);
@@ -63,13 +66,12 @@
 {
     if (m_message->isVisible()) {
         m_message->setVisible(false);
-        m_timeLine->setDirection(QTimeLine::Backward);
-        m_timeLine->start();
+        m_animation->setDirection(QAbstractAnimation::Backward);
     } else {
         m_message->setVisible(true);
-        m_timeLine->setDirection(QTimeLine::Forward);
-        m_timeLine->start();
+        m_animation->setDirection(QAbstractAnimation::Forward);
     }
+    m_animation->start();
 }
 
 void FadeMessage::setupScene()
@@ -94,13 +96,17 @@
     QFont font;
     font.setPointSize(font.pointSize() * 2);
     font.setBold(true);
-    int fh = QFontMetrics(font).height();
+    QFontMetrics fontMetrics(font);
+    int fh = fontMetrics.height();
 
-    QGraphicsRectItem *block = m_scene.addRect(50, 300, 300, fh + 3);
+    QString sceneText = "Qt Everywhere!";
+    int sceneTextWidth = fontMetrics.width(sceneText);
+
+    QGraphicsRectItem *block = m_scene.addRect(50, 300, sceneTextWidth, fh + 3);
     block->setPen(Qt::NoPen);
     block->setBrush(QColor(102, 153, 51));
 
-    QGraphicsTextItem *text = m_scene.addText("Qt Everywhere!", font);
+    QGraphicsTextItem *text = m_scene.addText(sceneText, font);
     text->setDefaultTextColor(Qt::white);
     text->setPos(50, 300);
     block->setZValue(2);