最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Qt實現(xiàn)轉(zhuǎn)動輪播圖

 更新時間:2020年06月08日 08:47:24   作者:h391998495979  
這篇文章主要為大家詳細(xì)介紹了Qt實現(xiàn)轉(zhuǎn)動輪播圖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

Qt輪播圖的實現(xiàn)代碼,供大家參考,具體內(nèi)容如下

qt輪播圖簡單的實現(xiàn),功能會在后面完善

效果圖:

這里我是用了QGraphicsScene+QGraphicsView+QGraphicsObject,其中對QGraphicsView和QGraphicsObject進(jìn)行繼承派生類功能進(jìn)行了添加。時間有限,直接貼上關(guān)鍵代碼部分供大家參考。

//pictrueitem.h
#ifndef PICTRUEITEM_H
#define PICTRUEITEM_H
#include <QGraphicsPixmapItem>
#include <QGraphicsItem>
#include <QGraphicsObject>
#include <QPixmap>
class PictrueItem : public QGraphicsObject
{
  Q_OBJECT

public:
  explicit PictrueItem(QGraphicsItem *parent = Q_NULLPTR);
  explicit PictrueItem(const QPixmap &pixmap, QGraphicsItem *parent = Q_NULLPTR);
  virtual ~PictrueItem();
  void setPixmap(const QPixmap &pixmap);
  QPixmap pixmap() const;
  virtual QRectF boundingRect() const;
  void setTransformationMode(Qt::TransformationMode mode);
  QPointF offset() const;
  void setOffset(const QPointF &offset);
  virtual int type()const;
  void setType(int type);
  int itemId()const;
  void setItemId(int id);

protected:
  void mousePressEvent(QGraphicsSceneMouseEvent *event);

  void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);

  virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);

Q_SIGNALS:

  void clicked();
  void clickedId(int);

private:

  QPointF pressedScenePoint;
  QPointF m_offset;
  QPointF m_pos;
  Qt::TransformationMode mode;
  QPixmap m_pixmap;
  bool isPressed;
  int m_type;
  int m_id;
  qreal m_pointPercent;
};

#endif // PICTRUEITEM_H
//pictrueitem.cpp
#include "pictrueitem.h"
#include <QGraphicsSceneMouseEvent>
#include <QPainter>
#include <QDebug>
PictrueItem::PictrueItem(QGraphicsItem *parent)
  :QGraphicsObject(parent),
   isPressed(false),
   mode(Qt::SmoothTransformation),
   m_type(0),
   m_id(0),
   m_pointPercent((qreal)0.0)
{

}

PictrueItem::PictrueItem(const QPixmap &pixmap, QGraphicsItem *parent)
  :QGraphicsObject(parent),
   isPressed(false),
   mode(Qt::SmoothTransformation),
   m_type(0)
{
  m_pixmap = pixmap;
}

PictrueItem::~PictrueItem()
{

}

void PictrueItem::setPixmap(const QPixmap &pixmap)
{
  prepareGeometryChange();
  m_pixmap = pixmap;
  update();
}

QPixmap PictrueItem::pixmap() const
{
  return m_pixmap;
}

QRectF PictrueItem::boundingRect() const
{
  if(m_pixmap.isNull())
    return QRectF();
  return QRectF(m_offset, m_pixmap.size() / m_pixmap.devicePixelRatio());
}

void PictrueItem::setTransformationMode(Qt::TransformationMode mode)
{
  if (mode != this->mode)
  {
    this->mode = mode;
    update();
  }
}

QPointF PictrueItem::offset() const
{
  return m_offset;
}

void PictrueItem::setOffset(const QPointF &offset)
{
  m_offset = offset;
  if (m_offset == offset)
    return;
  prepareGeometryChange();
  m_offset = offset;
  update();
}

int PictrueItem::type() const
{
  return m_type;
}

void PictrueItem::setType(int type)
{
  m_type = type;
}

int PictrueItem::itemId() const
{
  return m_id;
}

void PictrueItem::setItemId(int id)
{
  m_id = id;
}


void PictrueItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
  //只響應(yīng)鼠標(biāo)左鍵
  if(event->button() == Qt::LeftButton)
  {
    pressedScenePoint = event->pos();
    isPressed = true;

  }
}

void PictrueItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
  if(event->button() == Qt::LeftButton){
    if(isPressed &&
        boundingRect().contains(event->pos()) &&
        boundingRect().contains(pressedScenePoint))
    {
      isPressed = false;
      emit clicked();
      emit clickedId(type());
    }
  }
}

void PictrueItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
  Q_UNUSED(widget);
  Q_UNUSED(option);
  painter->setRenderHint(QPainter::SmoothPixmapTransform,
              (this->mode == Qt::SmoothTransformation));

  painter->drawPixmap(m_offset, m_pixmap);

}
//pictrueview.h
#ifndef PICTRUEVIEW_H
#define PICTRUEVIEW_H
#include <QGraphicsView>

class PictrueView : public QGraphicsView
{
  Q_OBJECT

public:
  PictrueView(QWidget *parent = Q_NULLPTR);
  virtual ~PictrueView();
protected:
  void resizeEvent(QResizeEvent *event);
public:
Q_SIGNALS:
  void sizeChanged(const QSize &);
};

#endif // PICTRUEVIEW_H
//pictrueview.cpp
#include "pictrueview.h"
#include <QResizeEvent>
PictrueView::PictrueView(QWidget *parent)
  :QGraphicsView(parent)
{

}

PictrueView::~PictrueView()
{
  //none
}

void PictrueView::resizeEvent(QResizeEvent *event)
{
  emit sizeChanged(event->size());
  return QGraphicsView::resizeEvent(event);
}

下面那行按鈕實現(xiàn)

//pictruebutton.h
#ifndef PICTRUERADIOBUTTON_H
#define PICTRUERADIOBUTTON_H
#include <QAbstractButton>

class PictrueButton : public QAbstractButton
{
  Q_OBJECT

public:
   explicit PictrueButton(QWidget *parent = Q_NULLPTR);
  ~PictrueButton();
  int id()const;
  void setId(int id);
Q_SIGNALS:
  void entered();
  void entered(int);
protected:
  virtual void paintEvent(QPaintEvent *);
  virtual void enterEvent(QEvent *event);
  virtual void leaveEvent(QEvent *event);
private:
  bool m_isSelected;
  int m_id;
};

#endif // PICTRUERADIOBUTTON_H
//pictruebutton.cpp
#include "pictruebutton.h"
#include <QPen>
#include <QPainter>
#include <QDebug>
#include <QPainterPath>
PictrueButton::PictrueButton(QWidget *parent)
  :QAbstractButton(parent),
   m_isSelected(false),
   m_id(0)
{
  setCheckable(true);
  setFixedSize(50,10);
}

PictrueButton::~PictrueButton()
{

}

int PictrueButton::id() const
{
  return m_id;
}

void PictrueButton::setId(int id)
{
  m_id = id;
}

void PictrueButton::paintEvent(QPaintEvent *)
{
  QPainter painter(this);
  QRectF drawRect = this->rect();
  QPainterPath drawPath;
  qDebug()<<drawRect;
  QPen drawPen;
  drawPen.setWidth(3);
  //選中為藍(lán),未選中為灰
  drawPen.setColor(Qt::gray);
  painter.setPen(drawPen);
  //抗鋸齒
  painter.setRenderHint(QPainter::Antialiasing);
  drawPath.addRoundedRect(drawRect,10,10);
  painter.setClipPath(drawPath);
  if(isChecked())
    painter.fillRect(drawRect,QColor(0,0,255,128));
  else
    painter.fillRect(drawRect,QColor(128,128,128,128));
}

void PictrueButton::enterEvent(QEvent *event)
{
  if(!isChecked())
    setChecked(true);
  emit entered(m_id);
  return QAbstractButton::enterEvent(event);
}

void PictrueButton::leaveEvent(QEvent *event)
{
  return QAbstractButton::leaveEvent(event);
}
//qmainWindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#define RAW_VIEW_SIZE QSize(476,414)
#define SCALE_VIEW_PIXMAP (qreal)2/1 //View與圖片比例
#define SCALE_BIG_SMALL (qreal)2/1 //圖片大小比例
//P1-P8,8個位置,根據(jù)需要改動
#define P1 (qreal)0.00
#define P2 (qreal)0.25
#define P3 (qreal)0.50
#define P4 (qreal)0.75
#define P5 (qreal)1.00
#define P6 P4
#define P7 P3
#define P8 P2
#define MID_TYPE 2
#define FPS 60//幀數(shù),每秒
#define DURATION_MS 500//移動一次圖元經(jīng)過時間,毫秒,不得低于幀數(shù)

namespace Ui {
class MainWindow;
}
class QGraphicsScene;
class QButtonGroup;
class MainWindow : public QMainWindow
{
  Q_OBJECT

public:
  enum Rules:int{
    RuleA = 1,
    RuleB = -1,
    RuleC = 2,
    RuleD = -2
  };

  explicit MainWindow(QWidget *parent = 0);
  ~MainWindow();
  int getIndexByRules(int oldIndex,int rule);
  template<typename T>
  void rollList(QList<T> &oldList, int dir, int count);
  void rollItem(int rollDir,unsigned rollCount);
public Q_SLOTS:
  void timerOutFunc(); 
  void nextItem();
  void previousItem();
  void clickedItemRoll(int type);
protected:

private:
  Ui::MainWindow *ui;
  QTimer *m_timer;
  QGraphicsScene *m_scene;
  QLineF midLine;
  QList<qreal> pointList;
  QList<QPixmap> pixmapList;
  QList<qreal> zValueList;
  QList<qreal> pixmapScaleList;
  int m_index;
  Rules currentRule;
  unsigned m_rollCount;
  QButtonGroup *btnGroup;
  bool btnMoveEnable;
};

#endif // MAINWINDOW_H

下面是滾動的關(guān)鍵代碼

void MainWindow::timerOutFunc()
{
  for(int i = 0; i<8; i++)
  {
    if(qAbs(midLine.pointAt(pointList[getIndexByRules(i,dir)]).x()-itemList[i]->pos().x())<qAbs(unitList[i]))
    {
      if(finishList.contains(i))
        continue;
      itemList[i]->setPos(midLine.pointAt(pointList[getIndexByRules(i,dir)]));
      //設(shè)置新的顯示優(yōu)先級
      itemList[i]->setScale(pixmapScaleList[getIndexByRules(i,dir)]);
      //設(shè)置新的類型
      itemList[i]->setType(getIndexByRules(i,dir));
      //i==7-->最后一個圖元移動完成
      finishList.append(i);
      if(finishList.size() == 8)
      {
        //循環(huán)旋轉(zhuǎn)圖元表和圖片表
        rollList(itemList,dir,1);
        rollList(pixmapList,dir,1);
        for(int i = 0; i<8; i++)
          itemList[i]->setZValue(zValueList[i]);
        m_timer->stop();
        finishList.clear();
        if(btnGroup->checkedId()!=itemList[MID_TYPE]->itemId())
          btnGroup->button(getIndexByRules(btnGroup->checkedId(),dir))->setChecked(true);
        if(--m_rollCount)
        {
          if(dir == 1)
            nextItem();
          else
            previousItem();
        }
        break;
      }
    }
    else
    {
      //按計算好的移動單位移動一次
      itemList[i]->setPos(QPointF(itemList[i]->pos().x()+unitList[i],itemList[i]->pos().y()));
      //轉(zhuǎn)換因子不是1.0時進(jìn)行轉(zhuǎn)換設(shè)置
      if(transScaleList[i] != (qreal)1.0)
      {
        itemList[i]->setScale(itemList[i]->scale()*transScaleList[i]);
      }
    }
    m_scene->invalidate();
  }
}
void MainWindow::rollItem(int rollDir, unsigned rollCount)
{
  if(m_timer->isActive())
    return;
  //清除之前的空間差列表和移動單位列表
  m_rollCount = rollCount;
  spaceList.clear();
  unitList.clear();
  transScaleList.clear();
  dir = rollDir;
  qDebug()<<"rollCount"<<rollCount;
  for(int i = 0; i<8; i++)
  {
    spaceList.append(midLine.pointAt(pointList[getIndexByRules(i,dir)]).x()-itemList[i]->pos().x());//計算移動總距離
    unitList.append(spaceList[i]/(FPS*DURATION_MS/1000));//計算移動單個距離
    transScaleList.append(pow(pixmapScaleList[getIndexByRules(i,dir)]/pixmapScaleList[i],\
               (qreal)1/(FPS*DURATION_MS/1000)));//計算增長倍數(shù)
  }

  //啟動定時器
  m_timer->start();

}

void MainWindow::nextItem()
{
  rollItem(1,1);
}

void MainWindow::previousItem()
{
  rollItem(-1,1);
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

您可能感興趣的文章:

相關(guān)文章

  • 用C++編寫擴(kuò)展node.js(node-ffi版)

    用C++編寫擴(kuò)展node.js(node-ffi版)

    今天小編就為大家分享一篇關(guān)于用C++編寫擴(kuò)展node.js(node-ffi版),小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2018-12-12
  • C語言修煉之路悟徹數(shù)組真妙理?巧用下標(biāo)破萬敵上篇

    C語言修煉之路悟徹數(shù)組真妙理?巧用下標(biāo)破萬敵上篇

    在C語言和C++等語言中,數(shù)組元素全為指針變量的數(shù)組稱為指針數(shù)組,指針數(shù)組中的元素都必須具有相同的存儲類型、指向相同數(shù)據(jù)類型的指針變量。指針數(shù)組比較適合用來指向若干個字符串,使字符串處理更加方便、靈活
    2022-02-02
  • C# interface與delegate效能比較的深入解析

    C# interface與delegate效能比較的深入解析

    本篇文章是對C#中interface與delegate的效能比較進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-05-05
  • C語言中組成不重復(fù)的三位數(shù)問題

    C語言中組成不重復(fù)的三位數(shù)問題

    這篇文章主要介紹了C語言中組成不重復(fù)的三位數(shù)問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • c++基礎(chǔ)算法動態(tài)DP解決CoinChange問題

    c++基礎(chǔ)算法動態(tài)DP解決CoinChange問題

    這篇文章主要為大家介紹了c++基礎(chǔ)算法如何利用動態(tài)DP來解決Coin Change的問題示例過程,有需要的朋友可以借鑒參考下,希望能夠有所幫助
    2021-10-10
  • 淺析C++中的函數(shù)與指針

    淺析C++中的函數(shù)與指針

    這篇文章主要介紹了淺析C++中的函數(shù)與指針,是C++入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下
    2015-09-09
  • C++中Stack(棧)的使用方法與基本操作詳解

    C++中Stack(棧)的使用方法與基本操作詳解

    Stack是一種常見的數(shù)據(jù)結(jié)構(gòu),常常被用來解決遞歸問題、括號匹配問題、函數(shù)調(diào)用棧等等。本文將介紹C++中stack的使用方法及基本操作,需要的可以參考一下
    2023-05-05
  • C語言實現(xiàn)小貓釣魚游戲

    C語言實現(xiàn)小貓釣魚游戲

    這篇文章主要為大家詳細(xì)介紹了C語言實現(xiàn)小貓釣魚游戲,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-01-01
  • 詳解C++中存儲類的使用

    詳解C++中存儲類的使用

    存儲類定義?C++?程序中變量/函數(shù)的范圍(可見性)和生命周期。這些說明符放置在它們所修飾的類型之前。auto、register、static、extern和mutable是C++程序中常用的存儲類,本文主要介紹了它們的使用方法,需要的可以參考一下
    2022-12-12
  • 詳細(xì)理解函C語言的函數(shù)棧幀

    詳細(xì)理解函C語言的函數(shù)棧幀

    這篇文章主要為大家介紹了C語言的函數(shù)棧幀,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助,希望能夠給你帶來幫助
    2021-11-11

最新評論

云林县| 监利县| 河东区| 阳春市| 齐齐哈尔市| 宁德市| 海宁市| 临泽县| 措美县| 白银市| 杨浦区| 古交市| 阿瓦提县| 太湖县| 安达市| 遵化市| 嘉峪关市| 墨玉县| 五莲县| 运城市| 称多县| 陆良县| 漳平市| 新巴尔虎右旗| 麻城市| 抚顺市| 绿春县| 怀宁县| 兴安县| 徐水县| 铜梁县| 桦甸市| 富民县| 杭州市| 永德县| 罗源县| 遵化市| 从江县| 沈阳市| 鹰潭市| 茶陵县|