我在pixmap板上有一个pixmap项目,我想在mouseReleaseEvent中删除这个项目。 我为我的项目设置zvalue为1,为我的板设置zvalue为0,我使用了itemat函数来执行此操作。我的项目的边界矩形大小为140 * 140,但图像的大小为90 * 90。问题是itemat函数检测到了板,而不是item.I尝试了一个140 * 140的pixmap,它填充了所有的boundingrect并且工作正常。所以我试图用qpainter中的fillrect函数填充我的项目的背景:
paint.fillRect(this->boundingRect(),Qt::NoBrush);
但它并没有影响边界。 文件说:
This pure virtual function defines the outer bounds of the item as a rectangle
所以我想也许我可以改变那个矩形。我可能不会感到不安和束缚。如果我错了,请纠正我。
这是代码:
void knight::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
//.....
if (this->scene()->itemAt(targetlocation,QTransform())->zValue()==1)
this->scene()->removeItem(this->scene()->itemAt(targetlocation,QTransform()));
}
我的knight.cpp文件:
knight::knight()
{
boundingRect();
setFlag(ItemIsSelectable);
_target=new QGraphicsPixmapItem[8];
setZValue(1);
}
QRectF knight::boundingRect()
{
return QRectF (0,0,140,140);
}
void knight::target(QPointF location)
{
for (int i=0;i<8;i++)
{
pointarray[i].setX(5);//hamintori 5 gozashtam ;
}
qreal x ,y,xarray[4],yarray[4];
{
x=location.x();
y=location.y();
xarray[0]=x+280;
xarray[1]=x-280;
xarray[2]=x+140;
xarray[3]=x-140;
yarray[0]=y+280;
yarray[1]=y-280;
yarray[2]=y+140;
yarray[3]=y-140;
}
for (int i=0;i<4;i++)
{
if (xarray[i]>=980&&xarray[i]<0)
xarray[i]=5;
if (yarray[i]>=980&&xarray[i]<0)
yarray[i]=5;
}
if (xarray[0]!=5)
{
pointarray[0].setX(xarray[0]);
pointarray[1].setX(xarray[0]);
if (yarray[2]!=5)
pointarray[0].setY(yarray[2]);
if (yarray[3]!=5)
pointarray[1].setX(yarray[3]);
}
if (xarray[1]!=5)
{
pointarray[2].setX(xarray[1]);
pointarray[3].setX(xarray[1]);
if (yarray[2]!=5)
pointarray[2].setY(yarray[2]);
if (yarray[3]!=5)
pointarray[3].setX(yarray[3]);
}
if (xarray[2]!=5)
{
pointarray[4].setX(xarray[2]);
pointarray[5].setX(xarray[2]);
if (yarray[0]!=5)
pointarray[4].setY(yarray[0]);
if (yarray[1]!=5)
pointarray[5].setX(yarray[1]);
}
if (xarray[3]!=5)
{
pointarray[6].setX(xarray[3]);
pointarray[7].setX(xarray[3]);
if (yarray[0]!=5)
pointarray[6].setY(yarray[0]);
if (yarray[1]!=5)
pointarray[7].setX(yarray[1]);
}
board _board;
QPixmap imagetarget(":/images/target.png");
for (int i=0;i<8;i++)
{
if (pointarray[i].y()!=5)
{
(_target+i)->setPixmap(imagetarget);
(_target+i)->setPos(pointarray[i]);
(_target+i)->boundingRect();
// qDebug() <<pointarray[i];
(_target+i)->setParentItem(_board.boarditem);
this->scene()->addItem(_target+i);
}
}
}
void knight::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
location=event->scenePos();
location=locationset(location);
target(location);
QGraphicsPixmapItem::mousePressEvent(event);
}
void knight::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
int contain;
for (int i=0;i<8;i++)
{
this->scene()->removeItem(_target+i);
}
targetlocation=event->scenePos();
targetlocation=locationset(targetlocation);
for (int i=0;i<8;i++)
{
if (targetlocation==pointarray[i])
contain=1;
}
if (contain==1)
this->setPos(targetlocation);
else
this->setPos(location);
if (this->scene()->itemAt(targetlocation,QTransform())->zValue()==1)
this->scene()->removeItem(this->scene()->itemAt(targetlocation,QTransform()));
}
void knight::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
this->setPos(event->scenePos());
}
QPointF knight::locationset(QPointF location)
{
int x,y;
x=location.x();
y=location.y();
x=x/140;
x=x*140;
y=y/140;
y=y*140;
location.setX(x);
location.setY(y);
return location;
}
我是新手所以,抱歉代码和英语:)