QGridLayout with Two Widgets with equal width and height
-
wrote on 13 May 2022, 15:28 last edited by sitesv
Hi!
I have QTabWidget with QGridLayout in one part of gui.
QGridLayout has a two child widgets. How to make a equal width and height of that widgets (squares)? Position of the widgets should be centered.
Thanks for advice. -
Hi,
If you want square widgets, you can reimplement heightForWidth.
As for centering, I am not sure I am following you. Just put your widget on top of each other and they should be centered.
-
Hi,
If you want square widgets, you can reimplement heightForWidth.
As for centering, I am not sure I am following you. Just put your widget on top of each other and they should be centered.
wrote on 17 Jun 2022, 14:53 last edited by sitesv@SGaist said in QGridLayout with Two Widgets with equal width and height:
If you want square widgets, you can reimplement heightForWidth.
@SGaist
Hi.
I reimplemented widget class and heightForWIdth function. But widgets have fixed dimensions... How to make squares which dimension depends of size of tab widget?class mySquare : public QWidget { public: explicit mySquare(QWidget *parent = nullptr) : QWidget(parent){ QSizePolicy p(sizePolicy()); p.setWidthForHeight(true); setSizePolicy(p); setStyleSheet("background-color: rgb(255, 0, 0);"); } ~mySquare(){ ; } int heightForWidth(int w) const{ return w; } QSize sizeHint() const{ return QSize(height(), heightForWidth(height())); // ? how make this correct? } };
-
Did you check which dimension you are returning ?
-
wrote on 20 Jun 2022, 09:16 last edited by
@SGaist said in QGridLayout with Two Widgets with equal width and height:
Did you check which dimension you are returning ?
Hi
That exactly is a question. How to make heightForWidth execution each time as form resizing? -
This Stack Overflow thread covers the subject pretty extensively.
-
This Stack Overflow thread covers the subject pretty extensively.
wrote on 11 Jul 2022, 10:30 last edited by sitesv 7 Nov 2022, 14:06@SGaist said in QGridLayout with Two Widgets with equal width and height:
This Stack Overflow thread covers the subject pretty extensively.
Hi! It's not clear for me.
Some Q's:
- Should widget expands with QSizePolicy::Expanding into all available space?
- If 'Y', that heightForWidth reimplementation is not enough for square creation and for avoid rectangle creation. Because it provide calculation for only one dimension. Layout could be resized by both dimensions.
class mySquare : public QWidget { public: explicit mySquare(QWidget *parent = nullptr) : QWidget(parent){ QSizePolicy p(sizePolicy()); p.setVerticalPolicy(QSizePolicy::Expanding); p.setHorizontalPolicy(QSizePolicy::Expanding); p.setWidthForHeight(true); setSizePolicy(p); setStyleSheet("background-color: rgb(255, 0, 0);"); } ~mySquare(){ ; } int heightForWidth(int w) const{ return w; } QSize sizeHint() const{ return QSize(width(), heightForWidth(width())); } }; class Form : public QWidget { Q_OBJECT public: explicit Form(QWidget *parent = nullptr); ~Form(); private: Ui::Form *ui; };
-
wrote on 11 Jul 2022, 15:27 last edited by sitesv 7 Dec 2022, 10:17
And:
Alignment becomes incorrect (not centered, square is placed on the left) if make a reimplementation of qwidget resizeEvent like this:virtual void resizeEvent(QResizeEvent *event){ int h = event->size().height(); int w = event->size().width(); if(h < w) w = h; resize(w,w); }
But qwidget geometry has correct behaviour in this case.
-
wrote on 12 Jul 2022, 10:33 last edited by sitesv 7 Dec 2022, 10:38
Very strange behaviour while expanding:
Correct behaviour while shrinking:
class mySquare : public QWidget { public: explicit mySquare(QWidget *parent = nullptr) : QWidget(parent){ QSizePolicy p(sizePolicy()); p.setVerticalPolicy(QSizePolicy::Expanding); p.setHorizontalPolicy(QSizePolicy::Preferred); p.setHeightForWidth(true); setSizePolicy(p); setContentsMargins(0,0,0,0); setStyleSheet("background-color: rgb(255, 0, 0);"); resize(1000, 1000); } ~mySquare(){ ; } int heightForWidth(int w) const override{ return w; } QSize sizeHint() const override{ return QSize(width(), heightForWidth(width())); } protected: virtual void resizeEvent(QResizeEvent *event) override{ int h = event->size().height(); int w = event->size().width(); if(h < w) w = h; resize(w, w); } };
ui->gridLayout->addWidget(new mySquare(this),0,0,Qt::AlignCenter); ui->gridLayout->addWidget(new mySquare(this),0,1,Qt::AlignCenter);
-
wrote on 12 Jul 2022, 15:15 last edited by
It will be better to check this issue inside designer and you can copy the code generated from ui output.
-
It will be better to check this issue inside designer and you can copy the code generated from ui output.
-
@SGaist said in QGridLayout with Two Widgets with equal width and height:
If you want square widgets, you can reimplement heightForWidth.
@SGaist
Hi.
I reimplemented widget class and heightForWIdth function. But widgets have fixed dimensions... How to make squares which dimension depends of size of tab widget?class mySquare : public QWidget { public: explicit mySquare(QWidget *parent = nullptr) : QWidget(parent){ QSizePolicy p(sizePolicy()); p.setWidthForHeight(true); setSizePolicy(p); setStyleSheet("background-color: rgb(255, 0, 0);"); } ~mySquare(){ ; } int heightForWidth(int w) const{ return w; } QSize sizeHint() const{ return QSize(height(), heightForWidth(height())); // ? how make this correct? } };
-
wrote on 13 Jul 2022, 19:13 last edited by JoeCFD
@sitesv You are laying out widets in your classes, not from ui file generated from designer. I often does this on designer to see how widgets are laid out and resize. Then build it in a small case and copy the code in ui_something to my project code.
-
@sitesv in gridlayout the width and height will be equal if you set the sizepolicy of the two widgets as QSizePolicy.Expanding.