내가 쓴 코드의 매우 단순화 된 버전 :
그것은 애니메이션 텍스트를 표시하고 원하는 거의 어떤 위젯입니다.
class Labels : public QFrame {
Q_OBJECT
Q_PROPERTY(int offset READ offset WRITE setOffset)
public:
/* The property used to animate the view */
int off;
QStringList texts;
Label() : QFrame() {
texts << "text 1" << "text 2" << "text 3" << "text 4";
setFixedSize(200, 200);
}
void paintEvent(QPaintEvent *) {
QPainter painter(this);
int x = 20;
int y = 20;
foreach(QString str, texts) {
int y1 = y + off;
/* Used to draw the texts as a loop */
/* If texts is underneath the bottom, draw at the top */
if (y1 > height()) {
y1 -= height();
}
painter.drawText(x, y1, str);
y+= 50;
}
}
int offset() {
return off;
}
void setOffset(int o) {
off = o;
update();
}
};
주요
:
int main(int argc, char **argv) {
QApplication app(argc, argv, true);
Labels l;
l.show();
/* Animated the view */
QPropertyAnimation *animation = new QPropertyAnimation(&l,"offset");
animation->setLoopCount(-1); /* infinite loop */
animation->setDuration(2000);
animation->setStartValue(0.0);
animation->setEndValue(200.0);
animation->start();
return app.exec();
}
가장 어려운 것은 최대 오프셋을 계산하는 것입니다 ...
내가 비슷한 이에했다. 내 경우에는 최대 오프셋을 계산하는 것이 어렵지 않았습니다. 동일한 크기의 이미지를 표시하고 동일한 간격으로 이미지를 표시하기 때문입니다. 고맙습니다. – Amy