Results 1 to 7 of 7

Thread: Custom Widgets and layout managers...

  1. #1
    Join Date
    Feb 2009
    Posts
    29
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question Custom Widgets and layout managers...

    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

  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: Custom Widgets and layout managers...

    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.
    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. #3
    Join Date
    Feb 2009
    Posts
    29
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Custom Widgets and layout managers...

    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?

  4. #4
    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: Custom Widgets and layout managers...

    From what you say there is no "right size". In that case you have to think of sizeHint() as of "default size".
    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.


  5. #5
    Join Date
    Feb 2009
    Posts
    29
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Custom Widgets and layout managers...

    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...

  6. #6
    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: Custom Widgets and layout managers...

    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.
    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. #7
    Join Date
    Feb 2009
    Posts
    29
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Custom Widgets and layout managers...

    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.

Similar Threads

  1. Custom widgets in layout
    By Palmik in forum Newbie
    Replies: 11
    Last Post: 26th January 2009, 12:08
  2. Replies: 3
    Last Post: 27th November 2008, 23:57
  3. Promoted widgets and layout boxes
    By notsonerdysunny in forum Qt Tools
    Replies: 3
    Last Post: 2nd May 2007, 14:15
  4. Custom plugin for a layout
    By cocheci in forum Qt Tools
    Replies: 2
    Last Post: 12th June 2006, 18:36
  5. Layout Managers???
    By jayw710 in forum Qt Programming
    Replies: 7
    Last Post: 18th February 2006, 03:42

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.