Hey folks, I've had to go about designing my own layout that worked as a clone of WPF's Grid with "star sizing" capabilities, so I've done just that.
QGridDefinition mainGridDef; mainGridDef.RowDefinition(0.03f); mainGridDef.RowDefinition(0.08f); mainGridDef.RowDefinition(0.94f); mainGridDef.RowDefinition(0.08f); mainGridDef.RowDefinition(0.03f); mainGridDef.ColumnDefinition(0.03f); mainGridDef.ColumnDefinition(0.2f); mainGridDef.ColumnDefinition(0.03f); mainGridDef.ColumnDefinition(); QGridStarLayout *mainGrid = new QGridStarLayout(mainGridDef); mainGrid->addWidget(new QPushButton("Test"), 1, 1); mainGrid->addWidget(new QPushButton("Test"), 3, 1); QGridDefinition subGridDef; subGridDef.RowDefinition(0.03f); subGridDef.RowDefinition(0.15f); subGridDef.RowDefinition(); subGridDef.RowDefinition(0.03f); subGridDef.ColumnDefinition(0.2f); subGridDef.ColumnDefinition(0.5f); subGridDef.ColumnDefinition(0.2f); QGridStarLayout *subGrid = new QGridStarLayout(subGridDef); subGrid->addWidget(new QScrollBar(Qt::Orientation::Horizontal), 1, 1); mainGrid->addLayout(subGrid, 0, 3, 5); centralWidget()->setLayout(mainGrid);
An imgur album detailing the above code and their definitions (blue is mainGrid, red is subGrid):
For more information on how star sizing works, please refer to this Stack Overflow post:
Furthermore, if you see where the layout can be optimized, feel free to fork the repo and apply any necessary changes.
"QGridStarLayout is a Qt layout with a design similar to that of WPF's Grid with "star sizing", where each row and column is defined as a weighted proportion of the available space."
can you explain that in layman's terms?
@mrjj Most definitely! I updated the OP with some information on star sizing and example usage. It's a simple concept, really.
Ahh. its in procent.
That is actually a something ppl sometimes request on forum.
So its a % version of Layout::layoutStretch
@mrjj Indeed! It makes layouts far easier to work with, I was very confused when this wasn't already a feature of Qt.
I'm still in need of assistance, however. I'm unsure as to the use of sizeHint and minimumSize; their current implementation seems to have no impact on the layout, but I'd much prefer if I actually knew their purpose. Could anyone elaborate on these methods?
Well as far as I know:
sizeHint is used as default size if you just create an object. Like new QButton()
minimumSize is hard limit on how small the widget can get. They have clear impact in normal layouts as
it wont ever get smaller than that.
@mrjj In my case I default all Widgets to SizePolicy::Expanding, so sizeHint is irrelevant to this application?
it means that it should use extra space if possible so MinSize could still apply if you scale
the container so all areas grow smaller.
Then MinSize is used to stop shrinking the widget at some point.
But if its usefull in this case is more doubtful. I guess it depends on design etc.