Qt轮播图的实现代码,供大家参考,具体内容如下
qt轮播图简单的实现,功能会在后面完善
效果图:
这里我是用了QGraphicsScene+QGraphicsView+QGraphicsObject,其中对QGraphicsView和QGraphicsObject进行继承派生类功能进行了添加。时间有限,直接贴上关键代码部分供大家参考。
//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) { //只响应鼠标左键 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); }
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/c-cdevelopment/485037.html