Results 1 to 12 of 12

Thread: Position of Items in QGraphicsScene/QGraphicsView

  1. #1
    Join Date
    Jul 2009
    Posts
    6
    Thanks
    2

    Question Position of Items in QGraphicsScene/QGraphicsView

    Hello,

    I have a problem with the position of Items in QGraphicsScenes.

    I wrote this simple program for testing, I made the ui with the designer:
    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QApplication app(argc, argv);
    4.  
    5. Form form;
    6.  
    7.  
    8. scene.setSceneRect(0, 0, form.graphicsView->width(), form.graphicsView->height());
    9.  
    10. scene.addRect(0, 0, 10, 10, QPen(), QBrush(QColor(Qt::black)));
    11.  
    12. form.graphicsView->setScene(&scene);
    13. form.graphicsView->fitInView(scene.sceneRect());
    14.  
    15. form.show();
    16.  
    17. return app.exec();
    18. }
    To copy to clipboard, switch view to plain text mode 

    When I run this program the RectItem somewhere in the view, not at the position (0,0).

    Where is the error in this code, I thought fitInView() manipulates the view in a way that the sceneRect is scaled to the right size.
    Attached Images Attached Images

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

    Default Re: Position of Items in QGraphicsScene/QGraphicsView

    You didn't apply a layout on the view. Your view is much smaller than your scene so everything gets scaled down when you try to fit the scene into 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.


  3. The following user says thank you to wysota for this useful post:

    StefanK2 (6th July 2009)

  4. #3
    Join Date
    Jul 2009
    Posts
    6
    Thanks
    2

    Default Re: Position of Items in QGraphicsScene/QGraphicsView

    Thanks for your fast reply.

    You didn't apply a layout on the view. Your view is much smaller than your scene so everything gets scaled down when you try to fit the scene into the view.
    I thought I did this with fitInView and by setting the sceneRect to the size of the view?

    What do you mean by apply a layout on the view, can't find anything about this.

  5. #4
    Join Date
    Dec 2008
    Location
    Istanbul, TURKEY
    Posts
    537
    Thanks
    14
    Thanked 13 Times in 13 Posts
    Qt products
    Qt4
    Platforms
    Windows Android

    Default Re: Position of Items in QGraphicsScene/QGraphicsView

    Hi,

    I guess you need to add a geometry for the view using setGeometry() method.

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

    Default Re: Position of Items in QGraphicsScene/QGraphicsView

    Quote Originally Posted by StefanK2 View Post
    I thought I did this with fitInView
    No, I mean the layout of the parent widget of the view.

    and by setting the sceneRect to the size of the view?
    You have set the size of the scene to the size of the parent widget, not the size of the view.

    What do you mean by apply a layout on the view, can't find anything about this.
    This is basic Qt stuff. Read about Using layouts in Designer and about Layout classes in general.
    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.


  7. #6
    Join Date
    Jul 2009
    Posts
    6
    Thanks
    2

    Default Re: Position of Items in QGraphicsScene/QGraphicsView

    Ok, I thought you mean some kind of layout on the graphicsView.

    I applied a vertical Layout to the QWidget, I also changed the sceneRect to the size of the Parent-Widget but it still doesn't work.

    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QApplication app(argc, argv);
    4.  
    5. Form form;
    6.  
    7.  
    8. scene.setSceneRect(0, 0, form.width(), form.height());
    9.  
    10. scene.addRect(0, 0, 10, 10, QPen(), QBrush(QColor(Qt::black)));
    11.  
    12. form.graphicsView->setScene(&scene);
    13. form.graphicsView->fitInView(scene.sceneRect());
    14.  
    15. form.show();
    16.  
    17. return app.exec();
    18. }
    To copy to clipboard, switch view to plain text mode 

    I attached the ui-file, maybe there is still some kind of error.
    Attached Files Attached Files
    • File Type: ui gui.ui (767 Bytes, 17 views)

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

    Default Re: Position of Items in QGraphicsScene/QGraphicsView

    What do you get and what do you expect? By the way, the scene is still of incorrect size. You probably want it the size of the maximumViewportSize().
    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. #8
    Join Date
    Jul 2009
    Posts
    6
    Thanks
    2

    Default Re: Position of Items in QGraphicsScene/QGraphicsView

    I try to get the rectangle to position 0,0 on the graphicsview, this is just a test to understand how QGraphicsScene and QGraphicsView work.

    I have programmed a version of this without QtDesigner and this works in the way I expected it.

    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QApplication app(argc, argv);
    4.  
    5. QWidget widget;
    6.  
    7.  
    8. scene.setSceneRect(0,0,500,500);
    9.  
    10. scene.addRect(0,0,10,10,QPen(), QBrush(QColor(Qt::black)));
    11.  
    12. QGraphicsView view(&widget);
    13.  
    14. view.setScene(&scene);
    15.  
    16. widget.show();
    17.  
    18. return app.exec();
    19. }
    To copy to clipboard, switch view to plain text mode 

    But when I try to build the gui with QtDesigner than I have this Problem with the size of the QGraphicsScene. I just want that the scene has the same size as the view, so that I can move the Items on the scene and they have the same coordinates on the view.

    At the moment I get this window (example2.jpg)
    Attached Images Attached Images

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

    Default Re: Position of Items in QGraphicsScene/QGraphicsView

    Don't set the scene size at all (and forget about fitInView). It will automatically adjust to the items it contains.
    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.


  11. #10
    Join Date
    Jul 2009
    Posts
    6
    Thanks
    2

    Default Re: Position of Items in QGraphicsScene/QGraphicsView

    Ok thanks,

    I will try this.

  12. #11
    Join Date
    Jul 2009
    Posts
    6
    Thanks
    2

    Default Re: Position of Items in QGraphicsScene/QGraphicsView

    I used maximumViewportSize(), this worked.

    When I don't set a sceneRect, the position 0,0 is the center of the view.

    Thanks for your help.

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

    Default Re: Position of Items in QGraphicsScene/QGraphicsView

    Quote Originally Posted by StefanK2 View Post
    When I don't set a sceneRect, the position 0,0 is the center of the view.
    The whole scene is centered in the view. Unless you place an item in negative coordinates of the scene, the scene's left top corner will have (0,0) coordinates.
    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.


  14. The following user says thank you to wysota for this useful post:

    StefanK2 (8th July 2009)

Similar Threads

  1. Replies: 2
    Last Post: 12th June 2009, 10:55
  2. Some menubar items can not be clicked
    By richardander in forum Qt Programming
    Replies: 4
    Last Post: 11th March 2009, 00:26
  3. Light items for the graphicsView
    By maverick_pol in forum Qt Programming
    Replies: 12
    Last Post: 1st November 2007, 18:51
  4. QGraphicsView: Dialog Position Doubt
    By arjunasd in forum Qt Programming
    Replies: 1
    Last Post: 6th August 2007, 17:48
  5. Selective highlighting of Items
    By Kapil in forum Qt Programming
    Replies: 3
    Last Post: 26th May 2006, 12:20

Tags for this Thread

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.