我正在尝试制作一个QGraphicObject,它表示一个圆角矩形,可以使用鼠标移动。
这个项目似乎画得正确,在文档中搜索后,我发现我必须设置标志
QGraphicsItem::ItemIsMovable
它使项目朝着正确的方向移动,但它总是比鼠标移动得更快,那么我做错了什么?
这是.h文件:
class GraphicRoundedRectObject : public GraphicObject
{
Q_OBJECT
public:
explicit GraphicRoundedRectObject(
qreal x ,
qreal y ,
qreal width ,
qreal height ,
qreal radius=0,
QGraphicsItem *parent = nullptr);
virtual ~GraphicRoundedRectObject();
qreal radius() const;
void setRadius(qreal radius);
qreal height() const ;
void setHeight(qreal height) ;
qreal width() const ;
void setWidth(qreal width) ;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *) override;
QRectF boundingRect() const override;
private:
qreal m_radius;
qreal m_width;
qreal m_height;
};
以及.cpp:
#include "graphicroundedrectobject.h"
#include <QPainter>
GraphicRoundedRectObject::GraphicRoundedRectObject(
qreal x ,
qreal y ,
qreal width ,
qreal height ,
qreal radius,
QGraphicsItem *parent
)
: GraphicObject(parent)
, m_radius(radius)
, m_width(width)
, m_height(height)
{
setX(x);
setY(y);
setFlag(QGraphicsItem::ItemIsMovable);
}
GraphicRoundedRectObject::~GraphicRoundedRectObject() {
}
void GraphicRoundedRectObject::paint
(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget*) {
painter->drawRoundedRect(x(), y(),m_width, m_height, m_radius, m_radius );
}
QRectF GraphicRoundedRectObject::boundingRect() const {
return QRectF(x(), y(), m_width, m_height);
}