こんにちは、私の現在のプロジェクトでは、 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*)