A QMainWindow slightly different from QDialog or QWidget in that it has the concept of a "central widget." A window has predefined areas for handling things such as toolbars, menus, and docks, and defines the center widget as the main content for the window. The window itself is usually not assigned a layout. But I suppose that you are adjusting the values ββin the window layout (which will have no effect).
The widget that you install as the central widget will most likely have its own layout. By default, the center widget can now expand to the edges. First consider this example:
#include <QApplication> #include <QMainWindow> #include <QVBoxLayout> #include <QListWidget> int main(int argc, char *argv[]) { QApplication a(argc, argv); QMainWindow *window = new QMainWindow; window->resize(800,600); QListWidget *listWidget = new QListWidget; window->setCentralWidget(listWidget); window->show(); return a.exec(); }
You will see that the list widget is fully expanded to the brim. But in a more realistic example:
#include <QApplication> #include <QMainWindow> #include <QVBoxLayout> #include <QListWidget> int main(int argc, char *argv[]) { QApplication a(argc, argv); QMainWindow *window = new QMainWindow; window->resize(800,600); QWidget *central = new QWidget; QListWidget *listWidget = new QListWidget; QVBoxLayout *layout = new QVBoxLayout; layout->addWidget(listWidget); //Uncomment this following line to remove margins //layout->setContentsMargins(0,0,0,0); central->setLayout(layout); window->setCentralWidget(central); window->show(); return a.exec(); }
You have a container widget, which then consists of a layout and a list widget. The layout of this central widget is the one that introduces the fields.
source share