Qt Layout on QMainWindow

孤者浪人 提交于 2019-11-28 22:29:28
Georg Schölly

You don't have to create a QVBoxLayout manually. Just select your central QWidget and press a make-layout button.

Jaime Ivan Cervantes

If you want to do it with code instead of using QtCreator, you could set the layout in a QWidget and then set the QWidget as the central widget of the main window like this:

#include <QtGui>
#include <QWidget>
#include <QHBoxLayout>
#include "mainwindow.h"

MainWindow::MainWindow() {  

        // Set layout
        QHBoxLayout *layout = new QHBoxLayout;
        layout->addWidget(myWidget1);
        layout->addWidget(myWidget2);

        // Set layout in QWidget
        QWidget *window = new QWidget();
        window->setLayout(layout);

        // Set QWidget as the central layout of the main window
        setCentralWidget(window);

}
Patrice Bernassola

Add at least one widget on your MainWindow. Then select your window by clicking on it and click on the VerticalLayout Button at the top of QTCreator. You Vertical Layout is automatically added to the central widget and fills all the surface.

This is already answered, but I personally prefer to keep all control elements and layouts added manually to the form. I do not add controls in the class files, I merely hook up the signals/slots to hide/show widgets relevant to the logic in the class, within the class.

To manually add a layout to any widget you must first add at least one child widget/control. That wasn't totally clear to me and I was trying to add the layout first.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!