Results 1 to 6 of 6

Thread: SizeHints and Policies

  1. #1

    Default SizeHints and Policies

    Qt Code:
    1. #include "calcbutton.h"
    2.  
    3. QSize calcButton::sizeHint()
    4. {
    5. return QSize(35,25);
    6. }
    7.  
    8. calcButton::calcButton(QString str) : QPushButton(str)
    9. {
    10. setSizePolicy(QSizePolicy::Minimum,QSizePolicy::Minimum);
    11. setMinimumSize(35,25);
    12. setMaximumSize(1000,1000);
    13. }
    To copy to clipboard, switch view to plain text mode 

    Despite the obvious lacking header file, this code is complete. The header is as can be expected, it simply inherits QPushButton in order to modify the sizing. This is done in an attempt to test out sizing, however, I have come to a problem:

    Using the above code, and many other alternatives I've tried, I have been unable to understand why when placing a lot of "calcButtons" within a QGridLayout the buttons all want to be larger than they need to be. They are created with minimal height, but the width is almost double the size-hint. I have a feeling it has to do with the widget which is holding everything. Basically I do the following:

    Create new widget.
    Create grid layout.
    Add a bunch of calcButtons.
    Set as central widget.
    setLayout

    The only way I have been successful with getting the buttons to be minimal size is by using setMaximumSize on the parent widget/central widget, which obviously isn't correct.

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: SizeHints and Policies

    You have to set it Fixed size policy
    Qt Code:
    1. #include "calcbutton.h"
    2.  
    3.  
    4. QSize calcButton::sizeHint()
    5. {
    6. return QSize(35,25);
    7. }
    8.  
    9.  
    10. calcButton::calcButton(QString str) : QPushButton(str)
    11. {
    12. setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);
    13. }
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  3. #3

    Default Re: SizeHints and Policies

    Fixed will definitely fix the issue of buttons sticking to the sizeHint, however, it causes new issues. The problem is, however, it wont allow stretch. I need stretching for two reasons: First, I want the application to scale to the size of the main window, which I DO want sizable. Second, inside the grid of buttons, some buttons take more than one spot. If the size is fixed, they cannot scale to fit the area. Basically, I'm looking for the buttons to take up as little space as possible, but to scale to fill as much space as needed.

    This does NOT have to be done via subclassing QPushButton. I am doing it this way for two reasons: First, since this is all testing and not an actual application, it makes creating several buttons with similar behavior much less extensive. Second, I was thinking it may open up some additional options by allowing access to overriding methods or simply being able to modify protected members.

  4. #4
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: SizeHints and Policies

    I'm looking for the buttons to take up as little space as possible, but to scale to fill as much space as needed.
    Conflicting statements / requirements

    Can you explain in detail.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  5. #5

    Default Re: SizeHints and Policies

    Well, I've figured out how to fix the issue, perhaps this will help understand my question, which still exists:

    Qt Code:
    1. resize(minimumSize);
    To copy to clipboard, switch view to plain text mode 

    When run on the mainWindow, the above code solves the problem I first brought up. It forces the application to take up the least amount of space possible without restricting resize. My question then becomes, is this the only way to get this result, or is there something I'm missing, such as some sort of size-policy? It seems odd to me that by default QT is wanting to make the application larger than required.

  6. #6
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: SizeHints and Policies

    Set QSizePolicy::MinimumExpanding policy on both calcButton & QMainWindow
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

Similar Threads

  1. question about size hints and size policies
    By Spooky in forum Qt Programming
    Replies: 5
    Last Post: 7th April 2011, 12:00

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.