日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

Qt 图片轮播

發布時間:2025/6/17 35 如意码农
生活随笔 收集整理的這篇文章主要介紹了 Qt 图片轮播 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

最近研究了一下圖片輪播,主要是用到了QPropertyAnimation這個類,具體代碼示例如下:

main.cpp

#include <QApplication>
#include "widget.h" int main(int argc, char *argv[])
{
QApplication a(argc, argv); Widget view;
view.show(); return a.exec();
}

widget.h

#ifndef WIDGET_H
#define WIDGET_H #include <QWidget>
#include <QPropertyAnimation> QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE class Widget : public QWidget
{
Q_OBJECT public:
Widget(QWidget *parent = nullptr); ~Widget(); protected:
const QString popImage(); void takeAnimation(); void timerEvent( QTimerEvent *event ); private:
void onAnimation0Finished(); void onAnimation1Finished(); signals:
void TakeAnimation(); private:
QVector<QString> aryImage; QWidget *widget0; QWidget *widget1; QPropertyAnimation *animation0; QPropertyAnimation *animation1; Ui::Widget *ui;
};
#endif // WIDGET_H

widget.cpp

#include <QPropertyAnimation>
#include "widget.h"
#include "ui_widget.h" Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
setWindowFlags(windowFlags() | Qt::FramelessWindowHint | Qt::WindowSystemMenuHint); // image
aryImage << "c:/1.jpg"
<< "c:/2.jpg"
<< "c:/3.jpg"; // widget
widget0 = new QWidget( this );
widget0->setFixedSize( 375, 200 ); widget1 = new QWidget( this );
widget1->setFixedSize( 375, 200 ); // animation
animation0 = new QPropertyAnimation();
animation1 = new QPropertyAnimation(); takeAnimation(); startTimer( 5000 );
} Widget::~Widget()
{
delete animation0;
delete animation1;
delete ui;
} const QString Widget::popImage()
{
auto img = aryImage.first();
aryImage.pop_front();
aryImage.push_back( img );
return img;
} void Widget::takeAnimation()
{
if ( animation0->targetObject() == nullptr ) {
widget0->setStyleSheet( QString( "border-image: url(%1);" ).arg( popImage() ) );
animation0->setTargetObject( widget0 );
} else if ( animation0->targetObject() == widget1 ) {
animation0->setTargetObject( widget0 );
} else if ( animation0->targetObject() == widget0 ) {
animation0->setTargetObject( widget1 );
} animation0->setPropertyName( "geometry" );
animation0->setDuration( 1500 );
animation0->setStartValue( QRect( 0, 0, 375, 200 ) );
animation0->setEndValue( QRect( -375, 0, 0, 200 ) );
animation0->start(); if ( animation1->targetObject() == nullptr ) {
widget1->setStyleSheet( QString( "border-image: url(%1);" ).arg( popImage() ) );
animation1->setTargetObject( widget1 );
} else if ( animation1->targetObject() == widget0 ) {
widget1->setStyleSheet( QString( "border-image: url(%1);" ).arg( popImage() ) );
animation1->setTargetObject( widget1 );
} else if ( animation1->targetObject() == widget1 ) {
widget0->setStyleSheet( QString( "border-image: url(%1);" ).arg( popImage() ) );
animation1->setTargetObject( widget0 );
} animation1->setPropertyName( "geometry" );
animation1->setDuration( 1500 );
animation1->setStartValue( QRect( 375, 0, 750, 200 ) );
animation1->setEndValue( QRect( 0, 0, 375, 200 ) );
animation1->start();
} void Widget::timerEvent( QTimerEvent *event )
{
takeAnimation();
}

總結

以上是生活随笔為你收集整理的Qt 图片轮播的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。