QGridLayout with Two Widgets with equal width and height
-
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.
@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 ?
-
This Stack Overflow thread covers the subject pretty extensively.
-
This Stack Overflow thread covers the subject pretty extensively.
@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; };
-
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.
-
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);
-
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? } };
-
@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.