Results 1 to 5 of 5

Thread: QScene doesn't retrive topmost item ?

  1. #1
    Join Date
    Oct 2012
    Posts
    13
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X

    Default QScene doesn't retrive topmost item ?

    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 :

    Untitled.jpg

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

  2. #2
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: QScene doesn't retrive topmost item ?

    Why did you make the squares on your board movable?

  3. #3
    Join Date
    Oct 2012
    Posts
    13
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: QScene doesn't retrive topmost item ?

    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)..

  4. #4
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: QScene doesn't retrive topmost item ?

    Can you not set your board up like this?
    Qt Code:
    1. #include <QtGui>
    2.  
    3. int main(int argc, char** argv)
    4. {
    5. QApplication app(argc, argv);
    6. for(int row=0; row<=7; ++row) {
    7. int x=0;
    8. int y= row*50;
    9. for(int col=0; col<=7;++ col) {
    10. QGraphicsRectItem *square = new QGraphicsRectItem(x, y, 50, 50); // not moveable by default
    11. if(!(row%2 ^ col%2)) square->setBrush(QBrush(Qt::white));
    12. else square->setBrush(QBrush(Qt::black));
    13. scene.addItem(square);
    14. x += 50;
    15. }
    16. }
    17. QGraphicsItem *piece = scene.addEllipse(7, 7, 35, 35, QPen(),QBrush(Qt::red)); // checker :) in lieu of chessman
    18. piece->setFlag(QGraphicsItem::ItemIsMovable); // make checker movable
    19. QGraphicsView view(&scene);
    20. view.show();
    21. return app.exec();
    22. }
    To copy to clipboard, switch view to plain text mode 

  5. The following user says thank you to norobro for this useful post:

    AtlasS (8th October 2012)

  6. #5
    Join Date
    Oct 2012
    Posts
    13
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: QScene doesn't retrive topmost item ?

    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 .
    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).

    Qt Code:
    1. QRectF BoardSquare::boundingRect() const
    2. {
    3.  
    4. 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)
    5.  
    6. }
    7.  
    8. void BoardSquare:: paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
    9. QWidget *widget)
    10. {
    11. QPoint s;
    12. painter->drawImage(s,*g);
    13.  
    14.  
    15.  
    16. }
    To copy to clipboard, switch view to plain text mode 

    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.

Similar Threads

  1. Retrive values using qt
    By jeyanthinatarajan in forum Newbie
    Replies: 1
    Last Post: 5th July 2012, 08:31
  2. How does QErrorMessage dialog make itself TopMost?
    By timewolf in forum Qt Programming
    Replies: 2
    Last Post: 17th August 2011, 02:57
  3. QGraphicsView, QScene and keyboard
    By harmodrew in forum Newbie
    Replies: 4
    Last Post: 21st August 2010, 21:22
  4. remove item for QGirdLayout doesn't work.
    By klnusbaum in forum Qt Programming
    Replies: 4
    Last Post: 23rd May 2008, 23:04
  5. QGraphicScene and different topmost QGraphicItem
    By Xaar in forum Qt Programming
    Replies: 3
    Last Post: 5th December 2007, 16:58

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.