PDA

View Full Version : No mouseButtonRelease detected



maitrezeta
2nd September 2010, 16:34
Hello !

I'm working on a Warcraft 2-like game, and I'm currently making the selection of several units.

I have a MapView :


#include <QGraphicsView>



class QGraphicsScene;



namespace Interface

{

class MainWindow;



class WarMapView : public QGraphicsView

{

protected:

MainWindow* window;

bool eventFilter(QObject *obj, QEvent *event);



public:

WarMapView(QGraphicsScene* scene, MainWindow* parent = 0);

void update();

};

}

The Map View is where there I can see my units and select them. I have already implement something to select just one unit, using MouseButtonPress event like that :



bool WarMapView::eventFilter(QObject *obj, QEvent *event)

{

if(event->type() == QEvent::MouseButtonPress)

{

QMouseEvent* m = dynamic_cast<QMouseEvent*>(event);

QPointF p = mapToScene(mapFromParent(m->pos()));



// if in the map

int size = GAME.getMap().getSize();

if(p.x() >= 0 && p.y() >= 0 && p.x() < size * BLOCK_SIZE && p.y() < size * BLOCK_SIZE)

GAME.mouseClick(FLOOR(p.x()), FLOOR(p.y()), m->button() == Qt::RightButton);
//it select the unit
}
...
}


It works well.

Now I want to select several units, so I decided to use the MouseButtonRelease. I save the coord of the point where the mouse is pressed, and when it is released I make a rectangle using the coords of the released point, and I select all the units in this rectangle.


The problem is that the MouseButtonRelease Event is not detected :-/ whereas the Press Event works well...
I don't understand where the problem come from.

I've tested it with that code in my event filter :



if(event->type() == QEvent::MouseButtonPress)

{

qDebug("pressed");

}

else if(event->type() == QEvent::MouseButtonRelease)

{

qDebug("released");

}


When I click, for example, 3 times in the map I have in my console :


>pressed
>pressed
>pressed


And I want to have


>pressed
>released
>pressed
>released
>pressed
>released


Another problem : I wanted to use the setDragMode(RubberBandDrag) in my MapView to have a nice rectangle, but when I set it, MousePress aren't detected any more :-/

Can someone explain to me what I made wrong ?

hobbyist
2nd September 2010, 17:11
if(event->type() == QEvent::MouseButtonPress)

{

qDebug("pressed");

}

else if(event->type() == QEvent::MouseButtonPress)

{

qDebug("released");

}


Unless there's a typo here, you're not testing for the release event.

maitrezeta
2nd September 2010, 17:44
Yes there is a typo, my code is :



if(event->type() == QEvent::MouseButtonPress)

{

qDebug("pressed");

}

else if(event->type() == QEvent::MouseButtonRelease)

{

qDebug("released");

}


Sorry for that :p

squidge
2nd September 2010, 18:26
Maybe it would be advisable to just dump all the event types you receive and see what you get.

Or maybe the release is being filtered out further up the chain before it gets to the widget you are filtering.

maitrezeta
3rd September 2010, 15:23
Thank you for you help.

But i finally find another thread where I find how to do
http://www.qtcentre.org/threads/16747-Problem-with-QGraphicsView-amp-mouseRelaseEvent?highlight=rubberbanddrag

I don't use the event filter, i have reimplement the mouseReleaseEvent() method :)