打地鼠游戏(7)

需求:

1、当移动鼠标到view容器中时,让鼠标样式变为一个锤子抬起的样子

2、点击时,鼠标样式变为按下的锤子样式

3、鼠标单击之后抬起时,显式为锤子抬起的样式

在QWidget中有一个属性:cursor可以定义用户的鼠标样式,可以通过设置这个属性值达到修改鼠标样式:

查看帮助文档:QCursor

1 QCursor(const QBitmap &bitmap, const QBitmap &mask, int hotX = -1, int hotY = -1)
2 
3 QCursor(const QPixmap &pixmap, int hotX = -1, int hotY = -1)

第一个参数,就是设置一个样式图片,其他参数有默认值。

对谁的cursor属性进行设置?

当移动鼠标到图元时(红色框)才需要显示为锤子样式;当移动鼠标到view容器时,如果没有移动到图元上,就不用显示为锤子样式,所以设置图元类:

 1 void myitem::mousePressEvent(QGraphicsSceneMouseEvent *event){
 2 
 3     //修改鼠标样式
 4     this->setCursor(QPixmap(":/bg/pic/picturedown.png"));
 5 
 6     //判断当前是否是老鼠
 7     if(this->ismouse()){
 8         qDebug()<<"打到老鼠";
 9         handle* pt = handle::getinstence();
10         pt->addscore();
11 
12         //避免连续两次打击,从而记分重复
13         this->setmouse(false);
14 
15         //打到老鼠之后更改显示为击中的样式
16         this->setPixmap(QPixmap(":/mouse/pic/beatmouse.png"));
17 
18     }else{
19         qDebug()<<"未打到老鼠";
20     }
21 }
1 void myitem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event){
2     //修改鼠标样式
3     this->setCursor(QPixmap(":/bg/pic/pictureUp.png"));
4 }
 1 void myitem::mousePressEvent(QGraphicsSceneMouseEvent *event){
 2 
 3     //修改鼠标样式
 4     this->setCursor(QPixmap(":/bg/pic/picturedown.png"));
 5 
 6     //判断当前是否是老鼠
 7     if(this->ismouse()){
 8         qDebug()<<"打到老鼠";
 9         handle* pt = handle::getinstence();
10         pt->addscore();
11 
12         //避免连续两次打击,从而记分重复
13         this->setmouse(false);
14 
15         //打到老鼠之后更改显示为击中的样式
16         this->setPixmap(QPixmap(":/mouse/pic/beatmouse.png"));
17 
18     }else{
19         qDebug()<<"未打到老鼠";
20     }
21 }

猜你喜欢

转载自www.cnblogs.com/data1213/p/10852454.html