Qt实现文字滚动、翻动动画

Qt实现文字滚动、翻动动画方式不唯一,这里尝试了2个手段。

  1. 基于动画类:QPropertyAnimation

  2. 使用QLabel的QPainter动态绘制

 

具体来看:

    A.使用动画类QPropertyAnimation:

具体可以参考blog:https://www.cnblogs.com/lvdongjie/p/4366092.html

简单来说,就几句话:

QPropertyAnimation在内的一簇类是Qt里用来实现动画的

QPropertyAnimation的动画可以针对所有的QWidget控件进行动态属性变化

位置类的变化(geometry):简单的可以设置起点、终点、大小、运动时间,然后就自动完成动作

// init

    m_TopPropertyAnimation = new QPropertyAnimation(this);

    // bind

    m_TopPropertyAnimation->setTargetObject(m_TopLabel);

    m_TopPropertyAnimation->setPropertyName("geometry");

    // set 动画的起点、终点、持续时间

    m_TopPropertyAnimation->setDuration(1000);

    m_TopPropertyAnimation->setStartValue(QRect(0, 0, width, height));

    m_TopPr
qt 滑屏翻页效果C++程序 #ifndef PICTUREFLOW_H #define PICTUREFLOW_H #include class PictureFlowPrivate; /*! Class PictureFlow implements an image show widget with animation effect like Apple's CoverFlow (in iTunes and iPod). Images are arranged in form of slides, one main slide is shown at the center with few slides on the left and right sides of the center slide. When the next or previous slide is brought to the front, the whole slides flow to the right or the right with smooth animation effect; until the new slide is finally placed at the center. */ class PictureFlow : public QWidget { Q_OBJECT Q_PROPERTY(QColor backgroundColor READ backgroundColor WRITE setBackgroundColor) Q_PROPERTY(QSize slideSize READ slideSize WRITE setSlideSize) Q_PROPERTY(int slideCount READ slideCount) Q_PROPERTY(int centerIndex READ centerIndex WRITE setCenterIndex) public: enum ReflectionEffect { NoReflection, PlainReflection, BlurredReflection }; /*! Creates a new PictureFlow widget. */ PictureFlow(QWidget* parent = 0); /*! Destroys the widget. */ ~PictureFlow(); /*! Returns the background color. */ QColor backgroundColor() const; /*! Sets the background color. By default it is black. */ void setBackgroundColor(const QColor& c); /*! Returns the dimension of each slide (in pixels). */ QSize slideSize() const; /*! Sets the dimension of each slide (in pixels). */ void setSlideSize(QSize size); /*! Returns the total number of slides. */ int slideCount() const; /*! Returns QImage of specified slide. */ QImage slide(int index) const; /*! Returns the index of slide currently shown in the middle of the viewport. */ int centerIndex() const; /*! Returns the effect applied to the reflection. */ ReflectionEffect reflectionEffect() const; /*! Sets the effect applied to the reflection. The default is PlainReflection. */ void setReflectionEffect(ReflectionEffect effect); public slots: /*! Adds a new slide. */ void addSlide(const QImage& image); /*! Adds a new slide. */ void addSlide(const QPixmap& pixmap); /*! Sets an image for specified slide. If the slide already exists, it will be replaced. */ void setSlide(int index, const QImage& image); /*! Sets a pixmap for specified slide. If the slide already exists, it will be replaced. */ void setSlide(int index, const QPixmap& pixmap); /*! Sets slide to be shown in the middle of the viewport. No animation effect will be produced, unlike using showSlide. */ void setCenterIndex(int index); /*! Clears all slides. */ void clear(); /*! Shows previous slide using animation effect. */ void showPrevious(); /*! Shows next slide using animation effect. */ void showNext(); /*! Go to specified slide using animation effect. */ void showSlide(int index); /*! Rerender the widget. Normally this function will be automatically invoked whenever necessary, e.g. during the transition animation. */ void render(); /*! Schedules a rendering update. Unlike render(), this function does not cause immediate rendering. */ void triggerRender(); signals: void centerIndexChanged(int index); protected: void paintEvent(QPaintEvent *event); void keyPressEvent(QKeyEvent* event); void mousePressEvent(QMouseEvent* event); void resizeEvent(QResizeEvent* event); private slots: void updateAnimation(); private: PictureFlowPrivate* d; }; #endif // PICTUREFLOW_H
Qt中使用QLabel实现文字的上下滚动可以通过以下步骤来实现: 1. 创建一个QLabel对象并设置其文本内容。 2. 设置QLabel的固定大小,以确保文本溢出时可以进行滚动。 3. 创建一个QTimer对象用于触发滚动事件。 4. 在滚动事件中,将QLabel的y坐标减少一个固定值,以实现向上滚动效果。 5. 当QLabel滚动到顶部时,将其y坐标重置为初始值,以实现无限滚动的效果。 下面是示例代码实现: ```cpp #include <QtWidgets> class ScrollLabel : public QLabel { public: ScrollLabel(const QString& text, QWidget* parent = nullptr) : QLabel(parent), m_yPos(0) { setText(text); setAlignment(Qt::AlignTop); setFixedSize(200, 100); // 设置固定大小 QTimer* scrollTimer = new QTimer(this); // 创建定时器 connect(scrollTimer, &QTimer::timeout, this, &ScrollLabel::scrollText); // 连接滚动事件 scrollTimer->start(100); // 设置滚动间隔 } private: void scrollText() { m_yPos -= 1; // 向上滚动一个固定值 move(pos().x(), m_yPos); // 更新QLabel的位置 if (m_yPos <= -height()) // 判断是否滚动到顶部 { m_yPos = 0; move(pos().x(), m_yPos); // 重置位置 } } private: int m_yPos; }; int main(int argc, char *argv[]) { QApplication a(argc, argv); ScrollLabel* scrollLabel = new ScrollLabel("这是一个滚动文字", nullptr); scrollLabel->show(); return a.exec(); } ``` 在这个例子中,我们创建了一个名为ScrollLabel的自定义QLabel类,并重写了其滚动事件scrollText。在scrollText函数中,我们不断减少QLabel的y坐标来实现文字的上下滚动。当滚动到顶部时,我们将y坐标重置为初始值,以实现无限滚动的效果。最后,在主函数中创建ScrollLabel对象,并显示出来。 这样,我们就成功地利用QLabel实现文字的上下滚动效果。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值