PDA

View Full Version : SizeHints and Policies



QT8seven
7th December 2011, 02:46
#include "calcbutton.h"

QSize calcButton::sizeHint()
{
return QSize(35,25);
}

calcButton::calcButton(QString str) : QPushButton(str)
{
setSizePolicy(QSizePolicy::Minimum,QSizePolicy::Mi nimum);
setMinimumSize(35,25);
setMaximumSize(1000,1000);
}

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.

Santosh Reddy
7th December 2011, 03:01
You have to set it Fixed size policy

#include "calcbutton.h"


QSize calcButton::sizeHint()
{
return QSize(35,25);
}


calcButton::calcButton(QString str) : QPushButton(str)
{
setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixe d);
}

QT8seven
7th December 2011, 03:25
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.

Santosh Reddy
7th December 2011, 04:15
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:confused:

Can you explain in detail.

QT8seven
7th December 2011, 04:48
Well, I've figured out how to fix the issue, perhaps this will help understand my question, which still exists:


resize(minimumSize);

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.

Santosh Reddy
7th December 2011, 06:52
Set QSizePolicy::MinimumExpanding policy on both calcButton & QMainWindow