gpt4 book ai didi

c++ - Qt "mousePressEvent"修改可点击区域

转载 作者:搜寻专家 更新时间:2023-10-31 02:23:23 31 4
gpt4 key购买 nike

我在使用 mousePressEvent(QGraphicsSceneMouseEvent *event) 时遇到问题,实际上可点击区域看起来很小并且偏离了它链接到的 QGraphicsPixmapItem 的中心。

http://i.stack.imgur.com/znpgW.png

红线是 QGraphicsPixmapItem 可点击的地方。

我如何将它居中并最终使其变大并改变它的形状?

以下是我的代码中可能有用的部分:

在player.h中

class Player:public QObject, public QGraphicsPixmapItem{
Q_OBJECT
public:
Player();
void place_player(int x, int y);
void mousePressEvent(QGraphicsSceneMouseEvent *event);
};

在 player.cpp 中

Player::Player(): QGraphicsPixmapItem(){
}

void Player::place_player(int x,int y)
{
this->setPixmap(QPixmap("test.png"));
this->setPos(x,y);
game->scene->addItem(this);
}

void Player::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
qDebug()<< event;
};

在游戏.cpp中

Game::Game(){

setFixedSize(1600,900);

scene = new QGraphicsScene(this);
scene->setSceneRect(0,0,1600,900);

setScene(scene);
}

void Game::start(){
player1 = new Player();
player1->place_player(300,300);
}

void Game::mousePressEvent(QMouseEvent *event)
{
QGraphicsView::mousePressEvent(event);
}

最后是 main.cpp

int main(int argc, char *argv[]){

QApplication a(argc, argv);

game = new Game();

game->show();
game->start();

return a.exec();
}

非常感谢您的帮助

最佳答案

QGraphicsItem 的可点击区域由其 boundingRect 定义和 shape功能。

我将从不使用 QGraphicsPixmapItem 开始。你想要一个自定义图形项,它具有信号和槽的功能,所以从 QGraphicsObject 派生.

class Player : public QGraphicsObject
{
};

因为我们现在已经从这个类派生出来了,所以我们需要覆盖几个纯虚函数;即boundingRect , paint

class Player : public QGraphicsObject
{
public:
QRectF boundingRect() const;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,QWidget *widget);
};

boundingRect 函数定义局部坐标中的对象。例如,假设字符的宽度和高度为 100。如果我们将 boundingRect 设置为 return (0, 0, 100, 100),这将围绕左上角定向。相反,我们希望将边界矩形集中在我们的播放器上:

QRectF Player::boundingRect() const
{
return QRectF(-50, -50, 100, 100); // local coordinates, centered on the Player
}

要绘制我们的 Player,请在类中存储一个 QPixmap

class Player : public QGraphicsObject
{
public:
QRectF boundingRect() const;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,QWidget *widget);

private:
QPixmap m_playerPixmap;
};

我假设您知道如何加载像素图并且可以在播放器的构造函数中执行此操作。

现在我们只需要渲染播放器,我们还将显示可点击区域,该区域由 boundingRect() 函数定义:-

void Player::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,QWidget *widget)
{
// draw the player
painter->drawPixmap(0, 0, m_playerPixmap);

// set the pen to draw debug rect
painter->setPen(QColor(255, 0, 0, 127));

// for debug purposes, show the bounding rect (clickable area)
painter->drawRect(boundingRect());
}

最初我提到可点击区域是由 boundingRect 和 shape 函数定义的。由于 Player 是统一形状(矩形),我们只关心 boundingRect。在形状不规则的情况下,您还可以覆盖形状函数。

How would I centre it and eventually make it bigger and change it's shape ?

希望您现在知道要使 Player 变大,只需增加 boundingRect 函数中返回的局部坐标即可。所以,如果我们想将它的宽度和高度加倍,我们会这样做:

QRectF Player::boundingRect() const
{
return QRectF(-100, -100, 200, 200); // local coordinates, centered on the Player
}

要更改其形状,请实现 shape() 函数并进行调试,绘制从该函数返回的 painterPath,而不是绘制 boundingRect。

例如,我们有一个圆形的可点击区域。

假设您已将形状声明添加到播放器 header :

QPainterPath Player::shape() const
{
QPainterPath path;
path.addEllipse(-100, -100, 200, 200);
return path;
}

void Player::paint(QPainter * painter, const QStyleOptionGraphicsItem, QWidget*)
{
// draw the player
painter->drawPixmap(0, 0, m_playerPixmap);

// set the pen to draw debug path
painter->setPen(QColor(255, 0, 0, 127));

// for debug purposes, show the path (clickable area)
painter->drawPath(shape());
}

最后要注意的是,如果您要重写形状函数,您仍然必须实现 boundingRect。

关于c++ - Qt "mousePressEvent"修改可点击区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29372383/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com