PDA

View Full Version : Crazy over the QGraphicsScene/Item



Morea
17th November 2006, 23:53
I have added an customized QGraphicsPixmapItem to the Scene, but when I click on the item.. what can I say, it records it as a ordinary click on the scene, but if I click more than once very quickly, it also notice that the item is clicked.

(as a side note: if I click exactly in the left margin of the Item, it seems to respond to a slow single click.)

It's driving me crazy. :crying: :eek: :confused:

Here is code for the item

void Town::mousePressEvent(QGraphicsSceneMouseEvent* ev)
{
Qt::MouseButtons btn = ev->buttons();
if (btn & Qt::LeftButton)
{
qDebug()<<"You visited Lamerville";
}
}
When I set the position for the item, I set it with integer coordinats. but I think that can hardly be the problem.

Here is code for the scene

void Game::mousePressEvent(QGraphicsSceneMouseEvent* ev)
{
QPointF p = ev->buttonDownScenePos( Qt::LeftButton);
int x,y;
x=p.x()/SKAL;
y=p.y()/SKAL;
qDebug()<<"you pressed mouse on map"<<x<<y;


SKAL is a constant for scaling purposes.

I'd be very happy for any suggestion. I guess there is a fundamental flaw... but what is it? :confused:

aamer4yu
18th November 2006, 05:05
Did u subclass the pix item ??
if yes, while handling mousepress event.. try passing it to QGraphicsItem::mousePressEvent ()....

also how are town and game related ??

Morea
18th November 2006, 07:50
Yes I did this:


class Town :public QGraphicsPixmapItem {
public:
Town(QPoint p=QPoint(0,0));
void mousePressEvent(QGraphicsSceneMouseEvent* ev);
protected:
QString name;
};

In the town.cpp file I have


void Town::mousePressEvent(QGraphicsSceneMouseEvent* ev)
{
Qt::MouseButtons btn = ev->buttons();
if (btn & Qt::LeftButton)
{
qDebug()<<"You visited Lamerville";
}
}

Should I add a line

QGraphicsItem::mousePressEvent ();
to the code? Where? In the beginning of the function?
Exactly, what will it accomplish?

town and game is related by populating the game ( a Scene) with towns (Items)


QVector<QPoint> cities = map->randomTownPositions(8);
QPoint pos;
Town* t;
foreach (pos, cities)
{
t = new Town(pos);
addItem(t);
towns.push_back(t);
}

I think the "additem" will be enough, won't it?

aamer4yu
20th November 2006, 04:02
did u try adding QGraphicsItem::mousePressEvent () to ur mousepressevent ??

Morea
20th November 2006, 08:46
Sorry, I did something to solve it. I can't remember right now.
But I think it was somethign with mousPressEvent.. or.. no. I don't remember what it was.

aamer4yu
20th November 2006, 09:09
well.. if u remmeber what u did.. u may post it.. it may benefit me too if ur way is not known :)

Morea
20th November 2006, 09:18
I think it was something with adding the item to the scene or setting the parent.