Results 1 to 9 of 9

Thread: Changing co-ordinate system in a QGraphicsView

  1. #1
    Join Date
    Aug 2011
    Posts
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Changing co-ordinate system in a QGraphicsView

    Hi,

    I've made a QGraphicsView and I've added to it a custom graphicsItem. In its paint event it draws a rectangle with top left at (0,0) - but in the QGraphicsView it's always centered, and I want it in the top left. I've tried setting the graphic view's alignment to AlignLeft/AlignTop but that changes nothing. How do I set it so that (0,0) is always the top left corner? I ask now because eventually it'll be much larger; if I add an item at something like (10000, 10000) afterwards it works fine, but centers the view when the scene is smaller!

    Thanks in advance!

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows
    Thanks
    21
    Thanked 418 Times in 411 Posts

    Default Re: Changing co-ordinate system in a QGraphicsView

    Be sure to carefully read the QGraphicsViewer, QGraphicsScene and QGraphicsItem documentation.

    In its paint event it draws a rectangle with top left at (0,0)
    In which coordinate system? I take it you mean on scene coordinates.
    From the docs:
    Items can be placed at any position on the scene, and the size of the scene is by default unlimited.
    As you can see, as the scene is unlimited in size, position 0,0 is just as good as any other, as it is infinite in any direction.
    0,0 can't be at the "left upper corner" since there is no limit in both x or y.

    What you want is to show a certain part of the scene (such that the item will be seen at the upper left corner of the view.
    Again, read in the docs how to do that (show certain part of the scene), ask if you get stuck with specifics.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Changing co-ordinate system in a QGraphicsView

    Quote Originally Posted by danbelsey View Post
    How do I set it so that (0,0) is always the top left corner?
    You explicitly set the scene to begin at (0,0) by setting its geometry to a rect starting with (0,0). If you wish to retain that configuration when the scene size is smaller than the view size, align the scene to top and left of the view.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #4
    Join Date
    Aug 2011
    Posts
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Changing co-ordinate system in a QGraphicsView

    Quote Originally Posted by high_flyer View Post
    Again, read in the docs how to do that (show certain part of the scene), ask if you get stuck with specifics.
    This is indeed the part I get stuck with! I've been trying to display a rectangle, starting at the co-ordinates (0,0), and of size (graphicView->size()->width(), graphicView->size()->height() - so that I create a scene the same size as the viewer, with (0,0) at the top left. I've tried:

    _ui->graphicsView->ensureVisible(0,0,_ui->graphicsView->size().width(), _ui->graphicsView->size().height(), 0, 0);
    _ui->graphicsView->fitInView(0,0,_ui->graphicsView->size().width(), _ui->graphicsView->size().height());

    But this appears to change nothing! I've also tried using translate() with various params but the view doesn't move. Where am I going wrong?

    Cheers!

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Changing co-ordinate system in a QGraphicsView

    If your scene is smaller than the view then fitInView is a no-op.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #6
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows
    Thanks
    21
    Thanked 418 Times in 411 Posts

    Default Re: Changing co-ordinate system in a QGraphicsView

    If your scene is smaller than the view then fitInView is a no-op.
    Actually quite the contrary, you can't have it NOT in the view

    Qt Code:
    1. _ui->graphicsView->ensureVisible(0,0,_ui->graphicsView->size().width(), _ui->graphicsView->size().height(), 0, 0);
    2. _ui->graphicsView->fitInView(0,0,_ui->graphicsView->size().width(), _ui->graphicsView->size().height());
    To copy to clipboard, switch view to plain text mode 
    Try using QGraphicsView::centerOn() and give the center position of your desired rect.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  7. #7
    Join Date
    Aug 2011
    Posts
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Changing co-ordinate system in a QGraphicsView

    Ah, I found out my problem; working on the wrong object! Setting the alignment on the correct object did, indeed, fix it. I did wonder why that wasn't doing anything in the creator...

    Apologies for wasting your time, but thanks for your help This is why you should document your design for the person who works after you, I suppose!

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Changing co-ordinate system in a QGraphicsView

    Quote Originally Posted by high_flyer View Post
    Actually quite the contrary, you can't have it NOT in the view
    So calling it is a no-op, it has no effect.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows
    Thanks
    21
    Thanked 418 Times in 411 Posts

    Default Re: Changing co-ordinate system in a QGraphicsView

    So calling it is a no-op, it has no effect.
    I know what you meant, that is why I added the wink smilie.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

Similar Threads

  1. QGraphicsView coordinate system
    By roband915 in forum Qt Programming
    Replies: 15
    Last Post: 5th December 2013, 06:58
  2. QGraphicsView coordinate system
    By edxxgardo in forum Qt Programming
    Replies: 3
    Last Post: 12th July 2011, 14:38
  3. globally re-orient the co-ordinate system
    By tomg_66 in forum Qt Programming
    Replies: 2
    Last Post: 21st October 2010, 23:49
  4. co-ordinate confusion: scenePos()
    By Urthas in forum Newbie
    Replies: 3
    Last Post: 15th March 2010, 03:12
  5. Replies: 1
    Last Post: 21st August 2008, 08:29

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.