PDA

View Full Version : How to use QGraphicsView and Scene right ...



Mike
23rd October 2006, 22:01
Hi all,

I would like to get your feedback on how I should use QGraphicsView for a technical application.
I plan to render graphs that have X and Y axis that both render a range from 0..100. The Y-Axis does this for % and the X-Axis for msec.
I started to render the axis but 'am not 100% sure how to do it right. If you specify e.g. the x-axis to be 400 pixel long, and the y axis 200 pixel high, than that's what you get. Meaning what I really want is that the scene alway's takes up the most available space in the view. This is important if the user resizes the application window. So if I start like this:

#######################
# ^
# |
# |
# +---------------------------------->#
#######################

Now the user changes the size of the view:

############################
# ^
# |
# |
# |
# |
# +------------------------------------------->#
############################

Then I still want the scene to render correctly in all the available space of the view. Zooming doesn't really do the job, because the ratio between available pixel in y direction may differ from the x axis...

Also the scene does require you to specify fixed lengh parameters for e.g. a addLine method. I really wonder if i have to clear all graphics items in the scene when the user changes the size of the view... get the available space from the view and then render the scene again.

How would you use the QGraphicsView / Scene for this? Any suggestion?

Thanks.

jacek
23rd October 2006, 22:13
I would like to get your feedback on how I should use QGraphicsView for a technical application.
I plan to render graphs that have X and Y axis that both render a range from 0..100. The Y-Axis does this for % and the X-Axis for msec.
Maybe you should try Qwt (http://qwt.sourceforge.net/)? It should have all the functionality you need and it's LGPL.


Zooming doesn't really do the job, because the ratio between available pixel in y direction may differ from the x axis...
It doesn't? You can pass two different scaling factors to QGraphicsView::scale().

And how about this?

_ui.view->fitInView( _ui.view->scene()->sceneRect() );

Mike
24th October 2006, 15:11
Hi thanks. I missed this code:


this->fitInView(this->scene()->sceneRect());

It's maybe not perfect with text.. but for the graph I want to render it looks pretty good. Maybe this single line of code is really all I need.

Great!

ghorwin
12th November 2006, 22:26
I have a similar problem and for me scaling doesn't do the job.

My current approach is to reimplement the resizeevent of whatever widget the graphicsview is in and set the scene rect based on the dimensions of the graphicsview. I have to take 2 pixels off to avoid scroll bars or alternatively turn the scroll bars off.

Maybe that is an alternative to using scaling factors?

Andreas

PS: Is GWT Plot already using QGraphicsView and item detection, e.g. if you want to click on a line plot to probe for properties?

Mike
13th November 2006, 19:41
Well I have to admit, that I did try to use QGraphicsview, but with scaling the rendered text (labels for x and y axis) did look pretty ugly... so I'm also still looking for another approach. I haven't seen a way for scaling the graphics but not the text... so I'm currently back to rendering the stuff myself depending on the available pixels (width & height)

Maybe some sample - if someone solved this with QGraphicsview would be great...

Until then, I paint everything myself...

Bitto
20th November 2006, 22:33
If you're referring to fitInView()'s default behavior of scaling without keeping the aspect ratio, you can pass Qt::KeepAspectRatio as a second argument. Otherwise, this function shouldn't distort text rendering at all.

Mike
22nd January 2007, 08:51
Well today I had to smile... I saw this post on qtdeveloper.net:

http://blogs.qtdeveloper.net/archives/2007/01/21/untransformable-qgraphicsitem-tags-labels/

Apparently more people had the same problem than me, and now one would be able to keep lables and X / Y axis unchanged when resizing the GraphicsView. That is great news and I'm looking forward to the next Qt release to try it out...

Thanks Trolltech for listening to your users !