我遇到了mousePressEvent(QGraphicsSceneMouseEvent *event)
問題,的確,可點擊區域看起來很小,並且偏離了它所鏈接的QGraphicsPixmapItem
。Qt「mousePressEvent」修改了可點擊區域
紅線是其中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;
};
在game.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();
}
非常感謝您的幫助
非常感謝你:) – 2015-03-31 20:43:06