Results 1 to 6 of 6

Thread: Resize QGraphicsItem when window resizes

  1. #1
    Join Date
    Feb 2014
    Posts
    94
    Thanks
    5
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Resize QGraphicsItem when window resizes

    Hello all!

    I am trying to resize a QGraphicsItem when I resize the app window but its not working.
    How could I do that?
    Qt Code:
    1. //Tool Box 3
    2. QGridLayout *gridLayoutInvestimento5 = new QGridLayout(boxInvestimento3);
    3. QGraphicsView *graphicsViewFluxoCaixa = new QGraphicsView(boxInvestimento3);
    4. QGraphicsScene *graphicsSceneFluxoCaixa = new QGraphicsScene(graphicsViewFluxoCaixa);
    5. graphicsViewFluxoCaixa->setScene(graphicsSceneFluxoCaixa);
    6.  
    7. boxInvestimento3->setLayout(gridLayoutInvestimento5);
    8. gridLayoutInvestimento5->addWidget(graphicsViewFluxoCaixa);
    9.  
    10. //graphicsViewFluxoCaixa->adjustSize();
    11.  
    12. //graphicsViewFluxoCaixa->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    13.  
    14. //graphicsViewFluxoCaixa->fitInView(graphicsSceneFluxoCaixa->itemsBoundingRect(), Qt::IgnoreAspectRatio);
    15.  
    16. QPen caneta(Qt::black);
    17. caneta.setWidth(4);
    18. QGraphicsLineItem *linhaTempo = new QGraphicsLineItem(0,0,graphicsViewFluxoCaixa->width(),0);
    19.  
    20. linhaTempo->setPen(caneta);
    21. graphicsSceneFluxoCaixa->addItem(linhaTempo);
    22. graphicsViewFluxoCaixa->adjustSize();
    23. graphicsViewFluxoCaixa->setBackgroundBrush(QBrush(Qt::red));
    To copy to clipboard, switch view to plain text mode 

    Thanks a lot.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,243
    Thanks
    303
    Thanked 866 Times in 853 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Resize QGraphicsItem when window resizes

    So you've posted 23 lines of code, with no information about where this code lives. Is it in some constructor for a widget? If so, there's no resizing going on there at all, because the window hasn't been displayed yet. That doesn't happen until the showEvent().

    If you want things to be resized when the window resizes, that needs to be done in the resizeEvent(). Line 14 will do what you want if it is moved to the resizeEvent(). You'll probably need to make the QGraphicsView a member variable if you want to get access to it outside of the constructor.

  3. #3
    Join Date
    Feb 2014
    Posts
    94
    Thanks
    5
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Resize QGraphicsItem when window resizes

    Thank you very much.

    I subclassed QGRaphicsView, and its is working.

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,243
    Thanks
    303
    Thanked 866 Times in 853 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Resize QGraphicsItem when window resizes

    I subclassed QGRaphicsView
    There was probably no need to do that at all. It's hard to tell what your code is supposed to do, but judging from this and other questions you've posted on the forum, it is probably more likely that you aren't using the layouts correctly in your GUI widgets. If you put the graphics view into a layout, and you correctly set the layout on the parent widget, then the layout should automatically take care of resizing the graphics view when the widget containing the layout is resized.

  5. #5
    Join Date
    Feb 2014
    Posts
    94
    Thanks
    5
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Resize QGraphicsItem when window resizes

    First of all, thank you for the help.

    I am using the QGraphicsView inside a QGridLayout and it resizes fine. My problem is that I need to resize the itens inside a QGraphicsScene that is inside the view. I need the itens resizing when the view is resized. Could you help me in this?

  6. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,243
    Thanks
    303
    Thanked 866 Times in 853 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Resize QGraphicsItem when window resizes

    Implement resizeEvent() for the widget that holds the QGraphicsView. Maybe this is the MainWindow of your class. In that event handler, retrieve the QRect of the QGraphicsView (not the QRect passed in the event - that's the rect for the entire main window), then call QGraphicsView::fitInView() with that QRect. If you want the objects in the view to stretch the same in both directions (squares remain square, circles remain circular), then use the Qt::AspectRatioMode value Qt::KeepAspectRatio, otherwise it will stretch differently in horizontal vs. vertical dimensions - squares will become rectangles, circles will become ellipses, etc.

Similar Threads

  1. Replies: 1
    Last Post: 29th December 2013, 19:06
  2. Replies: 2
    Last Post: 5th February 2012, 18:44
  3. Replies: 7
    Last Post: 1st December 2011, 17:24
  4. Replies: 4
    Last Post: 20th April 2011, 03:46
  5. resize a QGraphicsItem
    By dreamer in forum Qt Programming
    Replies: 1
    Last Post: 29th April 2008, 10:58

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.