私のカスタム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も登録されています。
[QGraphicsSceneMouseEvent :: button](http://doc.qt.io/qt-5/qgraphicsscenemouseevent.html#button)を使用しているのはなぜですか? –
あなたはmousepresseventの中に実装するのですか? – VikingPingvin
いいえ、私は[QGraphicsSceneMouseEvent](http://doc.qt.io/qt-5/qgraphicsscenemouseevent.html)*ボタンが押されたことを見つけることに関して必要な機能を持っていることを意味します。リンク内のドキュメントをチェックしてください。あるいは私はあなたの質問を誤解していますか? –