【Qt】QStackWidget动画(1)

使用直接提升为组件为当前组件即可。

.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#ifndef QANIMATIONSTACKEDWIDGET_H
#define QANIMATIONSTACKEDWIDGET_H

#include <QStackedWidget>
#include <QVariant>

class QPropertyAnimation;
class QGraphicsOpacityEffect;
class QParallelAnimationGroup;
//class QVariant;

class QAnimationStackedWidget : public QStackedWidget
{
Q_OBJECT
public:
explicit QAnimationStackedWidget(QWidget *parent = Q_NULLPTR);

~QAnimationStackedWidget();

void Next();

void SwitchToIndex(int NextIndex, bool bFront = true);

void setCurrentWidget(QWidget* widget);
protected:
void paintEvent(QPaintEvent *e);

private:
QParallelAnimationGroup* m_ParlGroup;

QPropertyAnimation *m_pAnimPrty;

QGraphicsOpacityEffect* m_pAnimGraphicsEffect;
QPropertyAnimation* m_pAnimGraphics;

bool m_bAnimation;

int m_nDuration;

int m_nWidgetCount;

int m_nNextIndex;

QVariant m_nCurValue;

bool m_bMoveFront;

private:
void Init();

void paintPrevious(QPainter &paint, int currentIndex);

void paintNext(QPainter &paint, int nextIndex);

void moveAnimationStart(bool bEnter);

private slots:
void slotAnimationValueChanged(QVariant value);

void slotAnimationFinished();
};

#endif // QANIMATIONSTACKEDWIDGET_H

.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#include "QAnimationStackedWidget.h"

#include <QPropertyAnimation>
#include <QPainter>
#include <QParallelAnimationGroup>
#include <QGraphicsOpacityEffect>

QAnimationStackedWidget::QAnimationStackedWidget(QWidget *parent) :
QStackedWidget(parent)
{
Init();
}

QAnimationStackedWidget::~QAnimationStackedWidget()
{

}

void QAnimationStackedWidget::Init()
{
m_bAnimation = false;

m_nDuration = 350;

m_ParlGroup = new QParallelAnimationGroup();

m_pAnimGraphicsEffect = new QGraphicsOpacityEffect(this);
m_pAnimGraphicsEffect->setOpacity(1);
this->setGraphicsEffect(m_pAnimGraphicsEffect);

m_pAnimGraphics = new QPropertyAnimation(m_pAnimGraphicsEffect, "opacity");
m_pAnimPrty = new QPropertyAnimation(this, QByteArray());
connect(m_pAnimPrty, SIGNAL(valueChanged(QVariant)), this, SLOT(slotAnimationValueChanged(QVariant)));
connect(m_pAnimPrty, SIGNAL(finished()), this, SLOT(slotAnimationFinished()));

m_pAnimPrty->setEasingCurve(QEasingCurve::InOutQuad);
m_pAnimGraphics->setEasingCurve(QEasingCurve::InOutQuad);

m_ParlGroup->addAnimation(m_pAnimPrty);
m_ParlGroup->addAnimation(m_pAnimGraphics);

}

void QAnimationStackedWidget::slotAnimationValueChanged(QVariant value)
{
m_nCurValue = value;

update();
}

void QAnimationStackedWidget::slotAnimationFinished()
{
m_bAnimation = false;

widget(currentIndex())->show();

setCurrentIndex(m_nNextIndex);
}

void QAnimationStackedWidget::paintEvent(QPaintEvent *e)
{
Q_UNUSED(e)

if(m_bAnimation)
{
QPainter paint(this);

if (m_bMoveFront)
{
//绘制当前Widget
paintPrevious(paint, currentIndex());

//绘制下一个widget
paintNext(paint, m_nNextIndex);
}
else
{
//绘制当前Widget
paintPrevious(paint, m_nNextIndex);

//绘制下一个widget
paintNext(paint, currentIndex());
}
}
}

void QAnimationStackedWidget::paintPrevious(QPainter &paint, int currentIndex)
{
//获得当前页面的Widget
QWidget *w = widget(currentIndex);

// 切换过程窗口的背景色
QString strStyle = QString("#%1{background-color: #FFFFFF}").arg(w->objectName());

w->setStyleSheet(strStyle);

QPixmap pixmap(w->size());

//将Widget的内容渲染到QPixmap对象中,即将Widget变成一张图片
w->render(&pixmap);

QRect r = w->geometry();

//绘制当前的Widget
double value = m_nCurValue.toDouble();
QRectF r1(0.0, 0.0, value, r.height());
QRectF r2(r.width() - value, 0, value, r.height());
paint.drawPixmap(r1, pixmap, r2);
}

void QAnimationStackedWidget::paintNext(QPainter &paint, int nextIndex)
{
QWidget *nextWidget = widget(nextIndex);

QString strStyle = QString("#%1{background-color: #FFFFFF}").arg(nextWidget->objectName());

nextWidget->setStyleSheet(strStyle);

QRect r = geometry();

//这行代码不加会有bug,第一次切换的时候,QStackedWidget并没有为child分配大小
nextWidget->resize(r.width(), r.height());
QPixmap nextPixmap(nextWidget->size());

nextWidget->render(&nextPixmap);
double value = m_nCurValue.toDouble();
QRectF r1(value, 0.0, r.width() - value, r.height());
QRectF r2(0.0, 0.0, r.width() - value, r.height());

paint.drawPixmap(r1, nextPixmap, r2);
}

void QAnimationStackedWidget::moveAnimationStart(bool bEnter)
{
if (bEnter)
{
m_pAnimPrty->setStartValue(this->width());
m_pAnimPrty->setEndValue(0);

m_pAnimGraphics->setDuration(m_nDuration);
m_pAnimGraphics->setStartValue(0);
m_pAnimGraphics->setEndValue(1);
}
else
{
m_pAnimPrty->setStartValue(0);
m_pAnimPrty->setEndValue(this->width());

m_pAnimGraphics->setDuration(m_nDuration);
m_pAnimGraphics->setStartValue(0);
m_pAnimGraphics->setEndValue(1);
}

m_pAnimPrty->setDuration(m_nDuration);

// m_pAnimPrty->start();

m_ParlGroup->start();
}

void QAnimationStackedWidget::SwitchToIndex(int index, bool bFront)
{
//如果正在动画,那么return
int curIndex = currentIndex();

m_bMoveFront = bFront;
m_bAnimation = true;
m_nWidgetCount = count();

m_nNextIndex = index;

//隐藏当前的widget
widget(curIndex)->hide();

//开始动画并设置间隔和开始、结束值
QRect g = geometry();

int width = g.width();

bFront = index >= curIndex;

moveAnimationStart(bFront);
}

void QAnimationStackedWidget::setCurrentWidget(QWidget* widget)
{
int index = indexOf(widget);
SwitchToIndex(index);
}

void QAnimationStackedWidget::Next()
{
//如果正在动画,那么return
if(m_bAnimation)
{
return;
}

m_bAnimation = true;
m_nWidgetCount = count();
int nIndex = currentIndex();

//计算下一页的索引
m_nNextIndex = (nIndex + 1) % m_nWidgetCount;

//隐藏当前的widget
widget(nIndex)->hide();

//开始动画并设置间隔和开始、结束值
QRect g = geometry();
int x = g.x();
int width = g.width();
m_pAnimPrty->setStartValue(width);
m_pAnimPrty->setEndValue(0);
m_pAnimPrty->setDuration(m_nDuration);
m_pAnimPrty->start();

Q_UNUSED(x)
}


【Qt】QStackWidget动画(1)
https://hodlyounger.github.io/B_Code/Qt/动画/【Qt】QStackWidget动画/
作者
mingming
发布于
2023年10月27日
许可协议