Results 1 to 5 of 5

Thread: QGraphicsScene Coordinate System

  1. #1
    Join Date
    May 2011
    Posts
    46
    Thanks
    5

    Default QGraphicsScene Coordinate System

    I have the following code that render a 10 x 10 grid, I was expecting the grid start at {0, 0}, which should be the top left of the window, but the result is that {0, 0} is the center of the window.

    Qt Code:
    1. void GameScene::drawBackground(QPainter *painter, const QRectF &rect)
    2. {
    3. int colCount = 10;
    4. int rowCount = 10;
    5. int tileWidth = 32;
    6. int tileHeight = 32;
    7.  
    8. // clear background
    9. painter->fillRect(rect, QBrush(Qt::black));
    10.  
    11. //painter->translate(rect.width() / -2.0, rect.height() / -2.0);
    12.  
    13. painter->setRenderHint(QPainter::Antialiasing, true);
    14.  
    15. QPen pen;
    16. pen.setStyle(Qt::DotLine);
    17. pen.setColor(QColor(Qt::gray));
    18. painter->setPen(pen);
    19.  
    20. int x, y, x2, y2;
    21.  
    22. y = 0; //painter->window().top();
    23. y2 = y + (rowCount * tileHeight);
    24. for(int c = 0; c <= colCount; c++)
    25. {
    26. x = c * tileWidth;
    27. x2 = x;
    28.  
    29. painter->drawLine(x, y, x2, y2);
    30. }
    31.  
    32. x = 0; //painter->window().left();
    33. x2 = x + (colCount * tileWidth);// painter->window().right();
    34. for(int r = 0; r <= rowCount; r++){
    35. y = r * tileHeight;
    36. y2 = y;
    37. painter->drawLine(x, y, x2, y2);
    38. }
    39. }
    To copy to clipboard, switch view to plain text mode 
    So, is this normal? Or I must translate the painter to have {0,0} be the top left corner?

    shot.jpg

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QGraphicsScene Coordinate System

    That is normal. You just have to align your scene at the top left. By default it is centered. See QGraphicsView documentation.

  3. #3
    Join Date
    Nov 2015
    Posts
    1
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Windows

    Smile Re: QGraphicsScene Coordinate System

    From this code you can set coordinate to top left corner

    ui->graphicsView->setAlignment(Qt::AlignTop|Qt::AlignLeft);


    replace graphicsView by your graphics view name

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,797
    Thanks
    263
    Thanked 777 Times in 767 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: QGraphicsScene Coordinate System

    I am sure that the original poster will be overjoyed to read this. He has been waiting nearly 4 years for the answer.

  5. #5
    Join Date
    Feb 2016
    Posts
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QGraphicsScene Coordinate System

    Hello Just joined this forum. Really new to QT. So the original question and answer is helpful. I have been trying to figure out how to get the coordinates from the graphics view/graphics scene but very confusing. Myself I want the cords in center but nice to know you can set them different. So I now am doing searches to see if I can figure out how to get cords from the view HELP!

Similar Threads

  1. QGraphicsView coordinate system
    By roband915 in forum Qt Programming
    Replies: 15
    Last Post: 5th December 2013, 07:58
  2. QGraphicsView coordinate system
    By edxxgardo in forum Qt Programming
    Replies: 3
    Last Post: 12th July 2011, 15:38
  3. coordinate system
    By Wojtek.wk in forum Newbie
    Replies: 7
    Last Post: 12th April 2010, 14:47
  4. 2D Graphics on coordinate system
    By soumya in forum Qt Programming
    Replies: 0
    Last Post: 4th November 2009, 07:27
  5. The coordinate system
    By avis_phoenix in forum Qt Programming
    Replies: 1
    Last Post: 28th July 2008, 13:16

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.