
Originally Posted by
wysota
The bounding rect specifies the local coordinate system
of the item (not of its parent). You should probably use QRectF(-20,-20,40,40) and then use
QGraphicsItem::setPos() or
QGraphicsItem::move() to position the item on the scene.
I still can't get the position and now the item keeps getting smaller after every click and finally disappears.
{
int bRectLeft = -20;
int bRectTop = -20;
int bRectW = 40;
int bRectH = 40;
}
void ArmoredVehicle
::moveUnit(QPointF newPoint
) {
this->setPos(newPoint);
update();
}
QRectF ArmoredVehicle
::boundingRect() const {
return QRectF(bRectLeft, bRectTop, bRectW, bRectH
);
}
{
Q_UNUSED(option);
Q_UNUSED(widget);
painter->setPen(Qt::NoPen);
painter->setBrush(Qt::blue);
painter
->drawPixmap
(this
->pos
().
x(),this
->pos
().
y(),
QPixmap(3,
30));
painter->setPen(Qt::NoPen);
painter->setBrush(Qt::green);
painter
->drawPixmap
(this
->pos
().
x(),this
->pos
().
y(),
QPixmap(40,
20));
qDebug("This pos: %d", this->pos().x());
}
QRectF ArmoredVehicle
::getBoundingRect() {
return QRectF(bRectLeft, bRectTop, bRectW, bRectH
);
}
ArmoredVehicle::ArmoredVehicle(QPixmap turret, QPixmap hull)
{
unitTurret = new QGraphicsPixmapItem(turret);
unitHull = new QGraphicsPixmapItem(hull);
int bRectLeft = -20;
int bRectTop = -20;
int bRectW = 40;
int bRectH = 40;
}
void ArmoredVehicle::moveUnit(QPointF newPoint)
{
this->setPos(newPoint);
update();
}
QRectF ArmoredVehicle::boundingRect() const
{
return QRectF(bRectLeft, bRectTop, bRectW, bRectH);
}
void ArmoredVehicle::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
Q_UNUSED(option);
Q_UNUSED(widget);
painter->setPen(Qt::NoPen);
painter->setBrush(Qt::blue);
painter->drawPixmap(this->pos().x(),this->pos().y(),QPixmap(3,30));
painter->setPen(Qt::NoPen);
painter->setBrush(Qt::green);
painter->drawPixmap(this->pos().x(),this->pos().y(),QPixmap(40,20));
qDebug("This pos: %d", this->pos().x());
}
QRectF ArmoredVehicle::getBoundingRect()
{
return QRectF(bRectLeft, bRectTop, bRectW, bRectH);
}
To copy to clipboard, switch view to plain text mode
BattleField::BattleField()
{
scene->setSceneRect(-100, -100, 1600, 1600);
scene->addItem(vehicle);
setScene(scene);
}
{
qDebug("VehX before %d", vehicle->pos().x());
vehicle
->moveUnit
(QPointF(event
->x
(),event
->y
()));
qDebug("VehX after %d", vehicle->pos().x());
qDebug("Widget posX %d", vehicle->pos().x());
qDebug("X relative to widget(scene?): %d", event->x());
qDebug("Y relative to widget(scene?): %d", event->y());
qDebug("VehX %d", vehicle->unitHull->x());
}
BattleField::BattleField()
{
QGraphicsScene *scene = new QGraphicsScene(this);
vehicle = new ArmoredVehicle(QPixmap(40,20),QPixmap(3,30));
scene->setItemIndexMethod(QGraphicsScene::NoIndex);
scene->setSceneRect(-100, -100, 1600, 1600);
scene->addItem(vehicle);
setScene(scene);
}
void BattleField::mousePressEvent(QMouseEvent *event)
{
qDebug("VehX before %d", vehicle->pos().x());
vehicle->moveUnit(QPointF(event->x(),event->y()));
qDebug("VehX after %d", vehicle->pos().x());
qDebug("Widget posX %d", vehicle->pos().x());
qDebug("X relative to widget(scene?): %d", event->x());
qDebug("Y relative to widget(scene?): %d", event->y());
qDebug("VehX %d", vehicle->unitHull->x());
}
To copy to clipboard, switch view to plain text mode
Bookmarks