PDA

View Full Version : Resize QGraphicsItem when window resizes



guidupas
20th May 2014, 22:06
Hello all!

I am trying to resize a QGraphicsItem when I resize the app window but its not working.
How could I do that?


//Tool Box 3
QGridLayout *gridLayoutInvestimento5 = new QGridLayout(boxInvestimento3);
QGraphicsView *graphicsViewFluxoCaixa = new QGraphicsView(boxInvestimento3);
QGraphicsScene *graphicsSceneFluxoCaixa = new QGraphicsScene(graphicsViewFluxoCaixa);
graphicsViewFluxoCaixa->setScene(graphicsSceneFluxoCaixa);

boxInvestimento3->setLayout(gridLayoutInvestimento5);
gridLayoutInvestimento5->addWidget(graphicsViewFluxoCaixa);

//graphicsViewFluxoCaixa->adjustSize();

//graphicsViewFluxoCaixa->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

//graphicsViewFluxoCaixa->fitInView(graphicsSceneFluxoCaixa->itemsBoundingRect(), Qt::IgnoreAspectRatio);

QPen caneta(Qt::black);
caneta.setWidth(4);
QGraphicsLineItem *linhaTempo = new QGraphicsLineItem(0,0,graphicsViewFluxoCaixa->width(),0);

linhaTempo->setPen(caneta);
graphicsSceneFluxoCaixa->addItem(linhaTempo);
graphicsViewFluxoCaixa->adjustSize();
graphicsViewFluxoCaixa->setBackgroundBrush(QBrush(Qt::red));


Thanks a lot.

d_stranz
21st May 2014, 03:25
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.

guidupas
22nd May 2014, 13:33
Thank you very much.

I subclassed QGRaphicsView, and its is working.

d_stranz
22nd May 2014, 23:47
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.

guidupas
23rd May 2014, 13:25
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?

d_stranz
24th May 2014, 17:32
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.