QGraphicsItem、QGraphicsView、QGraphicsScene
前言
使用QGraphicsItem、QGraphicsScene、QGraphicsView。主要功能,实现地图的放大、缩小、复位和运动轨迹的显示和清除,根据运动轨迹旋转图片
一、自定义QGraphicsItem
代码如下
graphicsitemairplan.h
#ifndef GRAPHICSITEMAIRPLAN_H
#define GRAPHICSITEMAIRPLAN_H
#include <QObject>
#include <QGraphicsItem>
#include <QPainter>
class GraphicsItemAirPlan : public QObject,public QGraphicsItem
{
Q_OBJECT
public:
explicit GraphicsItemAirPlan(QGraphicsItem *parent = nullptr);
void timerEvent(QTimerEvent *)override;
QRectF boundingRect() const override;
private:
QImage m_Image;
public:
//加载图片
void loadImage(QString strPath);
//图标的大小
QRect getAriPlanRect();
protected:
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = nullptr) override;
signals:
};
#endif // GRAPHICSITEMAIRPLAN_H
graphicsitemairplan.cpp
#include "graphicsitemairplan.h"
GraphicsItemAirPlan::GraphicsItemAirPlan(QGraphicsItem *parent) : QGraphicsItem(parent)
{
}
//加载图片
void GraphicsItemAirPlan::loadImage(QString strPath)
{
m_Image.load(strPath);
}
void GraphicsItemAirPlan::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,QWidget *widget)
{
//旋转
//setRotation(45);
if (!m_Image.isNull())
painter->drawImage(m_Image.rect(), m_Image);
}
void GraphicsItemAirPlan::timerEvent(QTimerEvent *)
{
}
QRectF GraphicsItemAirPlan::boundingRect() const
{
return m_Image.rect();
}
QRect GraphicsItemAirPlan::getAriPlanRect()
{
return m_Image.rect();
}
二、自定义QGraphicsView
graphicsviewmap.h
#ifndef GRAPHICSVIEWMAP_H
#define GRAPHICSVIEWMAP_H
#include