2017-04-10 3 views
0

私のカスタムQGraphicsアイテムの右クリック登録に問題があります。QT:QGraphicsItemでの左右のマウスプレスイベントの検出

マイカスタムクラスのヘッダ:

#include "tile_square.h" 

Tile_Square::Tile_Square() 
{ 
    Pressed = false; 
    MovementCostValue = 1; 

} 

QRectF Tile_Square::boundingRect() const 
{ 
    return QRectF(0,0,10,10); 
} 

void Tile_Square::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) 
{ 
    QRectF rec = boundingRect(); 
    QBrush brush(Qt::white); 

    painter->fillRect(rec,brush); 
    painter->drawRect(rec); 
} 

//Left click 
void Tile_Square::mousePressEvent(QGraphicsSceneMouseEvent *event) 
{ 
    QMouseEvent *mouseevent = static_cast<QMouseEvent *>(*event); 
    if(mouseevent->buttons() == Qt::LeftButton){ 
     MovementCostValue++; 
     qDebug() << "LEFT: Movement value is: " << MovementCostValue; 
    } 
    else if(mouseevent->buttons() == Qt::RightButton){ 
     MovementCostValue--; 
     qDebug() << "RIGHT: Movement value is: " << MovementCostValue; 
    } 
    update(); 
    QGraphicsItem::mousePressEvent(event); 


} 

私はgraphicsviewとgraphicssceneとのダイアログウィンドウ上でこれを描いています:ここ

#ifndef TILE_SQUARE_H 
#define TILE_SQUARE_H 
#include <QPainter> 
#include <QGraphicsItem> 
#include <QtDebug> 
#include <QMouseEvent> 

class Tile_Square : public QGraphicsItem 
{ 
public: 
Tile_Square(); 

bool Pressed; 
int MovementCostValue; 

QRectF boundingRect() const; 
void paint(QPainter *painter,const QStyleOptionGraphicsItem *option, QWidget *widget); 


protected: 
    void mousePressEvent(QGraphicsSceneMouseEvent *event); 
    void contextMenuEvent(QGraphicsSceneContextMenuEvent *cevent); 


}; 
#endif // TILE_SQUARE_H 

とは言っクラスの実装です。

左クリックでクラスの内部intを増やし、右クリックで減らしたいと思います。問題は、mousepresseventはどのボタンが押されたのではなく、イベントを登録することです。私のコードでは、私はそれを通常のMouse Eventにキャストしようとしていましたが、明らかに失敗していることがわかります。

正直なところ、私は

event->buttons() == Qt::LeftButton 

を書きたいけどQGraphicsSceneMouseEvent *イベントには、そのようなものを持っていません。どうした?

また、完全に動作し、右クリックを登録するcontextmenueventを使用してみましたが、通常のmousepresseventも登録されています。

+0

[QGraphicsSceneMouseEvent :: button](http://doc.qt.io/qt-5/qgraphicsscenemouseevent.html#button)を使用しているのはなぜですか? –

+0

あなたはmousepresseventの中に実装するのですか? – VikingPingvin

+0

いいえ、私は[QGraphicsSceneMouseEvent](http://doc.qt.io/qt-5/qgraphicsscenemouseevent.html)*ボタンが押されたことを見つけることに関して必要な機能を持っていることを意味します。リンク内のドキュメントをチェックしてください。あるいは私はあなたの質問を誤解していますか? –

答えて

0

まず、QGraphicsSceneMouseEventからQMouseEventにキャストすることはできません。 QGraphicsSceneMouseEventQMouseEventから派生していないので、これは安全なキャストではありません。ボタンメソッドは実際には正しいメソッドを呼び出さないでしょう、そのキャストは悪いです。第二に、QGraphicsSceneMouseEvent::buttonsが存在し、それはあなたが望むことをしますが、それはマスクです。あなたはこれを行うべきである:

#include <QGraphicsSceneMouseEvent> 

void Tile_Square::mousePressEvent (QGraphicsSceneMouseEvent *event) 
{ 
    if (event->buttons() & Qt::LeftButton) 
    { 
     // ... handle left click here 
    } 
    else if (event->buttons() & Qt::RightButton) 
    { 
     // ... handle right click here 
    } 
} 

でもマスクとしてこれを治療せずに、私はあなたの直接の比較は限り、あなたは一度にボタンの組み合わせを押さないように動作する可能性があることを期待します。しかし、私は確かにそれをテストしていません。

+0

それは私が最初に試したことでした。しかし、イベント - >何も指していません。私はボタン()もボタン()も、そこにあるべきものを呼び出すことはできません。 – VikingPingvin

+0

'event'がヌルポインタの場合(あなたが示唆しているように)、あなたのコードのどこかに*非常に*間違ったものがあります。 –

+0

しかし、私のコードは文字通りこれです。ダイアログ内のシーンとビューの作成者だけが他のコードです。 – VikingPingvin

関連する問題