how to align widget in Center of the window if it has fixed size
-
As said in question I have a widget named "center" which has some more widget. The maximum and minimum size of that widget is ''300 x 300.''
So How can we align it in center of that window. I tried using margins but it is specific to only specific size. It has vertical layout. It doesn't work for window resizing. Are there any method to fix it.
Yes there are many pages for positioning but I can't find the working solution.Thank You in Advance
-
@jsulm No it is not working
Can we use margins in QGridLayout. I have used margin :10 in bottom. But after removing it also It doesn't work. How can I fix it@Thank-You Do you actually have any other widgets at left/right or /top/bottom side of that central widget? If not you can simply use horizontal and vertical spacers.
-
As said in question I have a widget named "center" which has some more widget. The maximum and minimum size of that widget is ''300 x 300.''
So How can we align it in center of that window. I tried using margins but it is specific to only specific size. It has vertical layout. It doesn't work for window resizing. Are there any method to fix it.
Yes there are many pages for positioning but I can't find the working solution.Thank You in Advance
-
@Thank-You What about using QGridLayout?
-
@Thank-You What about using QGridLayout?
-
@jsulm No it is not working
Can we use margins in QGridLayout. I have used margin :10 in bottom. But after removing it also It doesn't work. How can I fix it@Thank-You Do you actually have any other widgets at left/right or /top/bottom side of that central widget? If not you can simply use horizontal and vertical spacers.
-
As said in question I have a widget named "center" which has some more widget. The maximum and minimum size of that widget is ''300 x 300.''
So How can we align it in center of that window. I tried using margins but it is specific to only specific size. It has vertical layout. It doesn't work for window resizing. Are there any method to fix it.
Yes there are many pages for positioning but I can't find the working solution.Thank You in Advance
You have 2 options:
The better one, use QGridLayout
the "bad" one override the resize event#include <QWidget> #include <QResizeEvent> class hackWidget : public QWidget { Q_OBJECT QWidget *center2; public: explicit hackWidget(QWidget *parent = nullptr) : QWidget(parent), center2(new QWidget(this)) { center2->setFixedSize(300,300); center2->setStyleSheet("background:red;"); } protected: void resizeEvent(QResizeEvent *event) override { const int w = event->size().width(); const int h = event->size().height(); const int x = ( w - center2->width()) / 2; const int y = (h - center2->height()) / 2; center2->move(x,y); } }; #include <QGridLayout> int main (int argc, char *argv[]) { QApplication app(argc, argv); QWidget mainWidget1; mainWidget1.resize(500,500); mainWidget1.show(); auto gLay {new QGridLayout(&mainWidget1)}; gLay->addItem(new QSpacerItem(0,0, QSizePolicy::Expanding, QSizePolicy::Expanding),0,0); gLay->addItem(new QSpacerItem(0,0, QSizePolicy::Expanding, QSizePolicy::Expanding),2,2); QWidget center1; center1.setFixedSize(300,300); center1.setStyleSheet("background: green;"); gLay->addWidget(¢er1,1,1); //// hackWidget mainWidget2; mainWidget2.resize(500,500); mainWidget2.show(); return app.exec(); } #include "main.moc"
-
@Thank-You Do you actually have any other widgets at left/right or /top/bottom side of that central widget? If not you can simply use horizontal and vertical spacers.
-
@Thank-You I'm not talking about layouts inside the center widget. You want to center the center widget itself, or do I misunderstand your use case?
-
@Thank-You I'm not talking about layouts inside the center widget. You want to center the center widget itself, or do I misunderstand your use case?