If you set a widget using QMainWindow::setCentralWidget, the main window will handle resizing the widget to fit the window contents. If it doesn't happen, it means that the widget you set for the central widget doesn't have a layout applied. If you apply the layout, it will work out of the box. Consider the following code:
int main(int argc, char **argv){
for(int i=0;i<5;i++){
// te->setGeometry(0, i*40, 200, 35); // %
l->addWidget(te); // *
}
mw.setCentralWidget(w);
mw.show();
return app.exec();
}
int main(int argc, char **argv){
QApplication app(argc, argv);
QMainWindow mw;
QWidget *w = new QWidget;
QVBoxLayout *l = new QVBoxLayout(w); // *
for(int i=0;i<5;i++){
QTextEdit *te = new QTextEdit(w);
// te->setGeometry(0, i*40, 200, 35); // %
l->addWidget(te); // *
}
mw.setCentralWidget(w);
mw.show();
return app.exec();
}
To copy to clipboard, switch view to plain text mode
Try it and then comment out lines that are marked with asterisks and remove the comment from the line marked with the percent sign and try again, you'll see the difference while resizing.
Bookmarks