PDA

View Full Version : Custom Widgets and layout managers...



TemporalBeing
25th February 2009, 23:09
I've developed a custom widget that draws some data giving a set of data points. I am placing the custom widget in a dialog that uses a layout manager. I'd like to put spacers on either side to keep the widget in the center of the dialog; however, that results in my custom widget being shrunk to nothing - namely because I do not have sizeHint() setup.

My problem comes in that I would like the sizeHint() to be relative to the object being drawn and the dialog area. The data for my coordinates comes in inches or millimeters. I am successfully using a QPainter with a matrix transform to scale it to the area available, so it draws nicely and I can scale it, and even discovered how to use QSize in the resizeEvent() to keep the aspect ratio correct. (I really wish they'd put that trick for aspect ratios in the documentation. It would be so much more helpful and easier to find that way.)

However, I am perplexed at how to determine an appropriate size for the sizeHints (sizeHint, minimumSize, etc) without resorting to generating an aspect ratio corrected statically sized QSize object - e.g. setting up a QSize with a specific, hard-coded box size (i.e. 30x40) and then using the QSize::scale(QSize(w,h),Qt::KeepAspectRatio) functionality to maintain the correct aspect ratio.

I don't want to make the software use up too much screen real estate, or make the widgets too small.

What is the correct way to do this?

TIA,

Ben

wysota
26th February 2009, 08:23
At some point you calculate the size of the object you draw as you have to know where to draw. Simply store that size in a member variable and return it from within sizeHint. The matrix you use will help you to do the calculations.

TemporalBeing
26th February 2009, 13:55
The data is easily available, and sizes are in inches and millimeters (not pixels)...it's the relationship to the pixels and such that I'm unable to figure out...

When I draw it I just scale it to the size of the window - whatever that may be - I don't calculate the size beyond that. In this case, for sizeHint I want to be able to go one step further and actually be able to make a size calculation. Yes, the scaling matrix will help keep aspect ratio, but that's about it.

How do I actually go about making a guess to the right size?

For example, the layout of the first dialog I am using this widget in uses a QVBoxLayout manager, with a slider on top, and then my widget below it. I'd like to put the widget into a QHBoxLayout with horizontal spacers on either side.

I could probably access the parent and get one of its rects, but that would give a larger area than I would technically have. Or I could loop through all the children of the parent widget that aren't me and get their minimum sizes, etc. and figure out a size, but then - isn't that the job of the layout manager?

wysota
26th February 2009, 21:24
From what you say there is no "right size". In that case you have to think of sizeHint() as of "default size".

TemporalBeing
3rd March 2009, 17:17
Ok, so I think I solved this is a somewhat creative way...I simply setup the parent to be able to tell the child what the available size is; of course, this only works when the parent handles its resize event and tells the child the corrected values. The child generates a resize of its own to ensure that the values get set with current information.

Not entirely the way I would have liked, but satisfactory for this case at least...

wysota
3rd March 2009, 22:43
Have you tried simply using Expanding size policy for the widget and putting it in a layout manager? I'm not really sure what the problem is :) What is that you are trying to obtain and what you have now.

TemporalBeing
4th March 2009, 13:48
I can easily draw objects in my widget's area. The problem I have is that I need to be able to have the widget work properly within the layout. So I do need to be able to determine size, and also be able to take up space. My initial use of the widget requires that it be centered in the dialog - so I wanted to put a horizontal spacer on either side; I might even try adding some vertical spacers around it as well to help center it entirely.

I have now implemented minimizeSizeHint(), sizeHint(), and set the sizePolicy to QSizePolicy(Expanding,Expanding). It seems to be doing what I want now.