当前位置:   article > 正文

qt 轮播图 实现,美化版本

qt 轮播图 实现,美化版本

        今天文章讲述的是如何用Qt实现图片轮播的效果,其实我们经常在网页中看到各种广告就是使用了图片轮播,实现小区域内嵌入多个广告的效果。

 

        其实实现起来也不难,只要使用Qt的动画类即可实现类似的效果。之前做了一个,效果不佳,今天重新写了一遍,实现了类似上面第一种的效果,通过Qt的动画类修改透明度来实现上下两张图片的切换效果,下面上效果图。

二、代码之路

CarouselImageWindow.h

  1. #include <QWidget>
  2. #include <QScrollArea>
  3. #include <QTimer>
  4. #include <QPropertyAnimation>
  5. #include <QPushButton>
  6. class CarouselImageWindow : public QWidget
  7. {
  8. Q_OBJECT
  9. public:
  10. CarouselImageWindow(QWidget *parent = NULL);
  11. ~CarouselImageWindow();
  12. // 设置图片列表;
  13. void setImageList(QStringList imageFileNameList);
  14. // 添加图片;
  15. void addImage(QString imageFileName);
  16. // 开始播放;
  17. void startPlay();
  18. private:
  19. // 初始化图片切换按钮;
  20. void initChangeImageButton();
  21. // 绘图事件;
  22. void paintEvent(QPaintEvent *event);
  23. // 鼠标点击事件;
  24. void mousePressEvent(QMouseEvent* event);
  25. public slots:
  26. // 图片切换时钟;
  27. void onImageChangeTimeout();
  28. // 图片切换按钮点击;
  29. void onImageSwitchButtonClicked(int buttonId);
  30. private:
  31. // 用来做图片切换滑动效果,目前以透明度作为切换效果;
  32. QScrollArea* m_imagePlayWidget;
  33. // 图片列表;
  34. QList<QString> m_imageFileNameList;
  35. // 图片切换时钟;
  36. QTimer m_imageChangeTimer;
  37. // 当前显示图片index;
  38. int m_currentDrawImageIndx;
  39. // 切换图片;
  40. QPixmap m_currentPixmap;
  41. QPixmap m_nextPixmap;
  42. // 图片切换动画类;
  43. QPropertyAnimation* m_opacityAnimation;
  44. // 按钮列表;
  45. QList<QPushButton*> m_pButtonChangeImageList;
  46. };

CarouselImageWindow.cpp

  1. #include "CarouselImageWindow.h"
  2. #include <QHBoxLayout>
  3. #include <QPainter>
  4. #include <QDebug>
  5. #include <QButtonGroup>
  6. CarouselImageWindow::CarouselImageWindow(QWidget *parent)
  7. : QWidget(parent)
  8. , m_currentDrawImageIndx(0)
  9. {
  10. // 添加ImageOpacity属性;
  11. this->setProperty("ImageOpacity", 1.0);
  12. // 动画切换类;
  13. m_opacityAnimation = new QPropertyAnimation(this, "ImageOpacity");
  14. // 这里要设置的动画时间小于图片切换时间;
  15. m_opacityAnimation->setDuration(1500);
  16. // 设置ImageOpacity属性值的变化范围;
  17. m_opacityAnimation->setStartValue(1.0);
  18. m_opacityAnimation->setEndValue(0.0);
  19. // 透明度变化及时更新绘图;
  20. connect(m_opacityAnimation, SIGNAL(valueChanged(const QVariant&)), this, SLOT(update()));
  21. // 设置图片切换时钟槽函数;
  22. connect(&m_imageChangeTimer, SIGNAL(timeout()), this, SLOT(onImageChangeTimeout()));
  23. this->setFixedSize(QSize(400, 250));
  24. this->setWindowFlags(Qt::FramelessWindowHint);
  25. }
  26. CarouselImageWindow::~CarouselImageWindow()
  27. {
  28. }
  29. void CarouselImageWindow::initChangeImageButton()
  30. {
  31. // 注意图片过多按钮可能放置不下;
  32. QButtonGroup* changeButtonGroup = new QButtonGroup;
  33. QHBoxLayout* hLayout = new QHBoxLayout();
  34. hLayout->addStretch();
  35. for (int i = 0; i < m_imageFileNameList.count(); i++)
  36. {
  37. QPushButton* pButton = new QPushButton;
  38. pButton->setFixedSize(QSize(16, 16));
  39. pButton->setCheckable(true);
  40. pButton->setStyleSheet("QPushButton{border-image:url(:/Resources/select1.png);}\
  41. QPushButton:checked{border-image:url(:/Resources/select2.png);}");
  42. changeButtonGroup->addButton(pButton, i);
  43. m_pButtonChangeImageList.append(pButton);
  44. hLayout->addWidget(pButton);
  45. }
  46. hLayout->addStretch();
  47. hLayout->setSpacing(10);
  48. hLayout->setMargin(0);
  49. connect(changeButtonGroup, SIGNAL(buttonClicked(int)), this, SLOT(onImageSwitchButtonClicked(int)));
  50. QVBoxLayout* mainLayout = new QVBoxLayout(this);
  51. mainLayout->addStretch();
  52. mainLayout->addLayout(hLayout);
  53. mainLayout->setContentsMargins(0, 0, 0, 20);
  54. }
  55. void CarouselImageWindow::setImageList(QStringList imageFileNameList)
  56. {
  57. m_imageFileNameList = imageFileNameList;
  58. }
  59. void CarouselImageWindow::addImage(QString imageFileName)
  60. {
  61. m_imageFileNameList.append(imageFileName);
  62. }
  63. void CarouselImageWindow::startPlay()
  64. {
  65. // 添加完图片之后,根据图片多少设置图片切换按钮;
  66. initChangeImageButton();
  67. if (m_imageFileNameList.count() == 1)
  68. {
  69. m_pButtonChangeImageList[m_currentDrawImageIndx]->setChecked(true);
  70. }
  71. else if (m_imageFileNameList.count() > 1)
  72. {
  73. m_pButtonChangeImageList[m_currentDrawImageIndx]->setChecked(true);
  74. m_currentPixmap = QPixmap(m_imageFileNameList.at(m_currentDrawImageIndx));
  75. m_imageChangeTimer.start(2000);
  76. update();
  77. }
  78. }
  79. void CarouselImageWindow::onImageChangeTimeout()
  80. {
  81. // 设置前后的图片;
  82. m_currentPixmap = QPixmap(m_imageFileNameList.at(m_currentDrawImageIndx));
  83. m_currentDrawImageIndx++;
  84. if (m_currentDrawImageIndx >= m_imageFileNameList.count())
  85. {
  86. m_currentDrawImageIndx = 0;
  87. }
  88. m_nextPixmap = QPixmap(m_imageFileNameList.at(m_currentDrawImageIndx));
  89. m_pButtonChangeImageList[m_currentDrawImageIndx]->setChecked(true);
  90. // 动画类重新开始;
  91. m_opacityAnimation->start();
  92. }
  93. void CarouselImageWindow::paintEvent(QPaintEvent *event)
  94. {
  95. QPainter painter(this);
  96. QRect imageRect = this->rect();
  97. // 如果图片列表为空,显示默认图片;
  98. if (m_imageFileNameList.isEmpty())
  99. {
  100. QPixmap backPixmap = QPixmap(":/Resources/CarouselImageBack.png");
  101. painter.drawPixmap(imageRect, backPixmap.scaled(imageRect.size()));
  102. }
  103. // 如果只有一张图片;
  104. else if (m_imageFileNameList.count() == 1)
  105. {
  106. QPixmap backPixmap = QPixmap(m_imageFileNameList.first());
  107. painter.drawPixmap(imageRect, backPixmap.scaled(imageRect.size()));
  108. }
  109. // 多张图片;
  110. else if (m_imageFileNameList.count() > 1)
  111. {
  112. float imageOpacity = this->property("ImageOpacity").toFloat();
  113. painter.setOpacity(1);
  114. painter.drawPixmap(imageRect, m_nextPixmap.scaled(imageRect.size()));
  115. painter.setOpacity(imageOpacity);
  116. painter.drawPixmap(imageRect, m_currentPixmap.scaled(imageRect.size()));
  117. }
  118. }
  119. void CarouselImageWindow::onImageSwitchButtonClicked(int buttonId)
  120. {
  121. m_currentDrawImageIndx = buttonId - 1;
  122. if (m_currentDrawImageIndx == -1)
  123. {
  124. m_currentDrawImageIndx = m_imageFileNameList.count() - 1;
  125. }
  126. onImageChangeTimeout();
  127. m_imageChangeTimer.start(2000);
  128. update();
  129. }
  130. void CarouselImageWindow::mousePressEvent(QMouseEvent* event)
  131. {
  132. // 这里可以对当前图片进行点击然后触发每个图片对应的效果;
  133. // 比如web上好多类似的弹出对应的广告页面等功能;
  134. qDebug() << m_currentDrawImageIndx;
  135. return __super::mousePressEvent(event);
  136. }

测试代码

  1. int main(int argc, char *argv[])
  2. {
  3. QApplication a(argc, argv);
  4. CarouselImageWindow w;
  5. w.addImage(":/Resources/image1.jpg");
  6. w.addImage(":/Resources/image2.jpg");
  7. w.addImage(":/Resources/image3.jpg");
  8. w.addImage(":/Resources/image4.jpg");
  9. w.startPlay();
  10. w.show();
  11. return a.exec();
  12. }
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/305243
推荐阅读
相关标签
  

闽ICP备14008679号