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>
namespace Interface
{
class MainWindow;
{
protected:
MainWindow* window;
public:
void update();
};
}
#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();
};
}
To copy to clipboard, switch view to plain text mode
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 :
{
if(event
->type
() == QEvent::MouseButtonPress)
{
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
}
...
}
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
}
...
}
To copy to clipboard, switch view to plain text mode
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");
}
if(event->type() == QEvent::MouseButtonPress)
{
qDebug("pressed");
}
else if(event->type() == QEvent::MouseButtonRelease)
{
qDebug("released");
}
To copy to clipboard, switch view to plain text mode
When I click, for example, 3 times in the map I have in my console :
>pressed
>pressed
>pressed
>pressed
>pressed
>pressed
To copy to clipboard, switch view to plain text mode
And I want to have
>pressed
>released
>pressed
>released
>pressed
>released
>pressed
>released
>pressed
>released
>pressed
>released
To copy to clipboard, switch view to plain text mode
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 ?
Bookmarks