Helo,在我当前的项目中,我遇到了在 QGraphicsScene 中选择 QAbstractGraphicsShapeItem 的问题,我执行以下操作:
QAbstractGraphicsShapeItem* i = canvas.addEllipse(QRectF(-radius,-radius,radius,radius));
i->setFlag(QGraphicsItem::ItemIsMovable);
i->setPen(Qt::NoPen);
i->setBrush( QColor(red, green , blue) );
i->setPos(x,y);
i->setZValue(qrand()%256);
画布在哪里 QGraphicsScene,因为我添加了标志 ItemIsMovable ,这允许拖动项目,但是当用户双击它们时我需要更改项目的颜色,有什么建议吗?
现在以下
class MyRectItem : public QObject, public QAbstractGraphicsShapeItem
{
Q_OBJECT
public:
MyRectItem(qreal x, qreal y, qreal w, qreal h) : QGraphicsRectItem(x,y,w,h)
{}
signals:
void selectionChanged(bool newState);
protected:
QVariant itemChange(GraphicsItemChange change, const QVariant &value)
{
if (change == QGraphicsItem::ItemSelectedChange)
{
bool newState = value.toBool();
emit selectionChanged(newState);
}
return QGraphicsItem::itemChange(change, value);
}
};
并尝试从这里调用它
void Main::addCircle(int x, int y,int radius,int red, int green, int blue)
{
MyRectItem *i = new MyRectItem(-50, -50, 50, 50);
canvas.addItem(i);
i->setFlag(QGraphicsItem::ItemIsSelectable);
i->setPen(QPen(Qt::darkBlue));
}
并得到以下错误
../portedcanvas/canvas.cpp: In member function 'void Main::addCircle(int, int, int, int, int, int)':
../portedcanvas/canvas.cpp:233: error: cannot allocate an object of abstract type 'MyRectItem'
../portedcanvas/canvas.cpp:68: note: because the following virtual functions are pure within 'MyRectItem':
../../../../Desktop/Qt/4.8.0/gcc/lib/QtGui.framework/Versions/4/Headers/qgraphicsitem.h:331: note: virtual QRectF QGraphicsItem::boundingRect() const
../../../../Desktop/Qt/4.8.0/gcc/lib/QtGui.framework/Versions/4/Headers/qgraphicsitem.h:352: note: virtual void QGraphicsItem::paint(QPainter*, const QStyleOptionGraphicsItem*, QWidget*)