1
我試圖做一個QGraphicObject,它代表了一個帶有圓角的矩形,可以使用鼠標移動。實現一個可移動的QGraphicsObject子類
該項目似乎繪製正確,並在文檔搜索後,我發現我不得不設置標誌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);
}
非常感謝你。 – 2013-02-25 10:23:39