2015-03-31 195 views
2

我遇到了mousePressEvent(QGraphicsSceneMouseEvent *event)問題,的確,可點擊區域看起來很小,並且偏離了它所鏈接的QGraphicsPixmapItemQt「mousePressEvent」修改了可點擊區域

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; 
}; 

在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(); 
} 

非常感謝您的幫助

回答

1

一個的QGraphicsItem的可點擊區域是由它的boundingRectshape功能定義。

我會先不使用QGraphicsPixmapItem。你需要一個自定義的圖形項目,它具有信號和插槽的功能,所以從QGraphicsObject派生。

class Player : public QGraphicsObject 
{ 
}; 

正如我們現在從這個類派生的,我們需要重寫一些純粹的虛函數;即boundingRectpaint

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

的boundingRect函數定義在本地座標的對象。例如,讓我們假設角色的寬度和高度都是100。如果我們將boundingRect設置爲返回(0,0,100,100),則這個角色將圍繞左上角。取而代之的是,我們要集中在我們的播放器的邊界RECT:

QRectF Player::boundingRect() const 
{ 
    return QRectF(-50, -50, 100, 100); // local coordinates, centered on the 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函數。由於玩家的形狀是統一的(一個矩形),我們只關心boundingRect。在形狀不規則的情況下,您也可以重寫形狀函數。

我該如何居中並最終使其變大並改變它的形狀?

希望你現在知道,爲了讓播放器更大,這只是增加其在boundingRect函數中返回的本地座標的問題。所以,如果我們希望它的寬度和高度的兩倍,我們可以這樣做:

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

要改變它的形狀,實現Shape()函數和調試,油漆painterPath從該函數返回的,而不是繪製boundingRect。

例如,讓我們有一個圓形的可點擊區域。

假設你已經添加形狀申報給玩家頭:注意

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。

+1

非常感謝你:) – 2015-03-31 20:43:06