简体   繁体   中英

QT: Detecting left and right mouse press events on QGraphicsItem

I am having trouble registering right click on my custom QGraphics item.

My custom class's header:

#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

And here is the implementation of said class:

    #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);


}

I am drawing this on a Dialog window with a graphicsview and a graphicsscene.

I wanto increase the internal int of the class on left click, and decrease it on right click. Problem is, mousepressevent registers the event and not which button was pressed. In my code you can see i tried to cast it to regular Mouse Event, but failed, obviously.

Honestly i want to write

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

but the QGraphicsSceneMouseEvent *event does not have such one. What is the issue?

I also tried using the contextmenuevent, which works perfectly and registers the right click, but the regular mousepressevent gets registered too.

First, you cannot cast from QGraphicsSceneMouseEvent to QMouseEvent . QGraphicsSceneMouseEvent doesn't derive from QMouseEvent , so that's just not a safe cast to make. The buttons method is probably not actually calling the right method because that cast is bad. Second, QGraphicsSceneMouseEvent::buttons does exist, and it does what you want, but it's a mask. You should be doing this:

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

Even without treating this as a mask, I would expect that your direct comparison is likely to work as long as you don't press a combination of buttons at once. However, I haven't tested that to be sure.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM