PDA

View Full Version : QScene doesn't retrive topmost item ?



AtlasS
5th October 2012, 15:05
Hello.

I'm writing a chess app., after implementing mouse events for movement, everything worked fine (I was able to move the pieces on the board like charm).

then my QScene stopped retrieving topmost items, and went to the lowest item (i.e chess squares), the pieces cannot be moved at all .
Did anybody encounter an issue like this ? how can I fix it ?

# note :the scene code is very big, but all I did is adding items, perfectly placing the pieces of chess above the squares (unique 64 squares , unique 16 pieces for each side), here's how the scene looks after running :

8289

I'm using (itemAt ( x,y)->setPos(z,q) ) to move items.

norobro
7th October 2012, 01:04
Why did you make the squares on your board movable?

AtlasS
7th October 2012, 15:24
I tried to lock them, but it was no good. Each and every square is an QPixmapItem defined inside a class called square. Being Pixmap items, I guess they're movable at all times, especially when using the "itemAt(*mousePress)->setPos(point *x)..

norobro
7th October 2012, 15:44
Can you not set your board up like this?
#include <QtGui>

int main(int argc, char** argv)
{
QApplication app(argc, argv);
QGraphicsScene scene;
for(int row=0; row<=7; ++row) {
int x=0;
int y= row*50;
for(int col=0; col<=7;++ col) {
QGraphicsRectItem *square = new QGraphicsRectItem(x, y, 50, 50); // not moveable by default
if(!(row%2 ^ col%2)) square->setBrush(QBrush(Qt::white));
else square->setBrush(QBrush(Qt::black));
scene.addItem(square);
x += 50;
}
}
QGraphicsItem *piece = scene.addEllipse(7, 7, 35, 35, QPen(),QBrush(Qt::red)); // checker :) in lieu of chessman
piece->setFlag(QGraphicsItem::ItemIsMovable); // make checker movable
QGraphicsView view(&scene);
view.show();
return app.exec();
}

AtlasS
7th October 2012, 16:39
Pretty nice & effective piece of code : ) but in that way,I don't have total control over "the data" of the squares. For example, I won't be able to tell if the square is white,black, occupied or not.

Thank you very much for your effort, but I fixed the error finally by straight luck :p .
Problems were :

1-Dynamic cast : I didn't typecast the fetched element from the scene.
2-The painting : here's the code ,
looks like the items moved, but were invisible xD, because of the QRectF return value (which is nothing), so everything was getting re-drawn in small pixels (very small).


QRectF BoardSquare::boundingRect() const
{

return QRectF(0,0,30,30); //Here was the error, QRectF ( ) was the return value, then I fixed it to QRectF(0,0,30,30)

}

void BoardSquare:: paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
QWidget *widget)
{
QPoint s;
painter->drawImage(s,*g);



}

Conclusion of the error & debugging :

The fetched item was a copy, not the real one, fetched and redrawn in a very small size. After using the "Dynamic cast", I was capable of returning the original element.

Afrer fixing the returned value from BoundingRect () function, the item was redrawn perfectly .

Thanks norobro for you effort : ) I will sure put your help into use & consideration in the future.