Square QWidget - Height = Width
-
Hi
You can use
http://doc.qt.io/qt-5/qwidget.html#hasHeightForWidth
and
int QWidget::heightForWidth(int w) const
Those are virtuals so you need to subclass to use. -
@mrjj Thanks for your comment.
I've just implemented both the
hasHeightForWidth
(returning true) and theheightForWidth
function (returning the width) - which I'm guessing is the correct way to do it.The outcome is ultimately the same - the widget is not square.
-
@mrjj Thanks for your comment.
I've just implemented both the
hasHeightForWidth
(returning true) and theheightForWidth
function (returning the width) - which I'm guessing is the correct way to do it.The outcome is ultimately the same - the widget is not square.
-
HI you might also need sizeHint
see this old sample
https://doc.qt.io/archives/qq/qq04-height-for-width.html
(the QFrame)
Sorry could not find newer. -
I fought this fight in the past.
heightForWidth
determines the preferred height for width not the actual one. any layout is free to go beyond it.if you can live with
setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);
then use it, otherwise you'll need to create a custom layout -
I fought this fight in the past.
heightForWidth
determines the preferred height for width not the actual one. any layout is free to go beyond it.if you can live with
setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);
then use it, otherwise you'll need to create a custom layout -
I must admit last time I looked at it I was still a n00b of Qt (even though little changed in the meantime). I vaguely remember having a layout with a
Qt::Alignment
private member. in the setGeometry I calculated the minimum of width and height, resized the widget according to that minimum and then placed it according to the alignment set. It supported only 1 item but it wasn't a big issue as you can combine layouts.If you are interested the code can be found here: header/source but, as I mentioned, it was a project I used to learn Qt so the code quality is surely below any reasonable standard