Qt: Set size of child QWidget same as parent widget

Hi,
I have a QMainWindow which has a QToolBar and a QStackedWidget widget inside. Within that stacked widget there are many other widgets (= windows, only one visible at a time).

However, I always have to set the size of these child widgets with setGeometry() and resize().
There hase to be an easier way to:
1. set the QStackedWidget to full size in the QMainWindow (except the toolbar)
2. set all the widgets within the QStackedWidget to the same size as the QStackedWidget
.. so basically to always set the size of the child widget the same as the parent widget.

Thanks for your help.
DarkyCoderAsked:
Who is Participating?
 
HappyCactusConnect With a Mentor Commented:
Exactly.
The layout has been created (by UIC) with the container as parent, and the controlled widgets are added with addWidget().
You must add the layout to each container, since they are resized to the direct parent sizes. So: the first layout resizes the stackwidget to the main centralwidget; the second layout resizes the tab widget to the stackwidget size. The third one, resizes the textwidget to the tabwidget size.


0
 
HappyCactusCommented:
Apply a Layout to your mainwindow. a QVBoxLayout will place all the widget in a vertical "list", so since you have only one widget (QStacketWidget) it will take all the place available.

See the QT Documentation about widgets and layout: [1]


[1] http://doc.qt.nokia.com/4.6/widgets-and-layouts.html
0
 
DarkyCoderAuthor Commented:
Thanks for your help.
However, it doesn't work so far.

See the code from the QMainWindow constructor.

Any ideas how to get all the windows to the same size as the main window?
Thanks
m_pWindows = new QStackedWidget(this);
m_pWindowHome = new CWindowHome(this); // subclass of QWidget
m_pWindows->addWidget(m_pWindowHome);
// more subclasses of QWidget are created and added

m_pLayout = new QVBoxLayout;
m_pLayout->addWidget(m_pWindows);
// other windows are added...
setLayout(m_pLayout);
//m_pWindows->setLayout(m_pLayout); // also tried this instead, but there's no change either
setCentralWidget(m_pWindows);

Open in new window

0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
HappyCactusCommented:
This is what my QtCreator (uic) implements for a form derived from QMainWindow with a similar structure (QStackedWidget with qwidget derived widget).
centralwidget is a simple QWidget.

This is a machine-created code (by UIC), so I think you could simplify it.

horizontalLayout = new QHBoxLayout(centralwidget);
        horizontalLayout->setObjectName(QString::fromUtf8("horizontalLayout"));
        stackedWidget = new QStackedWidget(centralwidget);
        stackedWidget->setObjectName(QString::fromUtf8("stackedWidget"));
        page = new QWidget();
        page->setObjectName(QString::fromUtf8("page"));
        stackedWidget->addWidget(page);
        page_2 = new QWidget();
        page_2->setObjectName(QString::fromUtf8("page_2"));
        stackedWidget->addWidget(page_2);

        horizontalLayout->addWidget(stackedWidget);

        MainWindow->setCentralWidget(centralwidget);

Open in new window

0
 
DarkyCoderAuthor Commented:
I just tried your approach with QtDesigner mysself. No matter what I do with the QHBoxLayout, it never uses full space for a single child widget (but places it into the top left corner instead).
Am I missing something?
0
 
HappyCactusCommented:
Have you assigned your boxlayout to the stacked widget or the main window? I think you should apply it to both the main window and the stacked widget (two layout).
0
 
DarkyCoderAuthor Commented:
As posted in my code above I tried assigning it to the main window and the widgets. Also with two different layouts.
The windows don't get the size of the main window because of this. Not even when I try this with a clean project and QtDesigner.

So I guess the solution is something else?
0
 
HappyCactusCommented:
Can you provide a complete, very small, project (as zip) ?
0
 
DarkyCoderAuthor Commented:
Of course.
The attached zip file is basically a copy of the layout problem without all the self implemented subclasses (of QWidget).

There's a QMainWindow.
On that QMainWindow is a QVBoxLayout.
Within that layout there's a QStackedWidget and a QTabWidget with a QTextEdit.

Now I want that TextEdit, TabWidget etc all to be the same size as the QMainWindow.


I couldn't upload it here because experts-echange complains about .ui, .qrc etc file extensions.
http://rapidshare.com/files/420327552/GUI.zip
0
 
HappyCactusConnect With a Mentor Commented:
No layout was applied to each container to the GUI inside the designer.
I give you the corrected gui.ui file. (renamed as .txt).
Inside the designer, you must select the container and click a layout icon.


gui.txt
0
 
DarkyCoderAuthor Commented:
Thanks a lot.
The GUI looks like I wanted to have it;)

However, I'm not sure I understand my mistake. When I look at the generated ui_gui.h file I can't find any setLayout() function call.
So what has changed on the code-level? You created 2 horizontal and 1 vertial layout and added a layout to every created widget?
        centralWidget = new QWidget(GUIClass);
        centralWidget->setObjectName(QString::fromUtf8("centralWidget"));
        horizontalLayout = new QHBoxLayout(centralWidget);
        horizontalLayout->setSpacing(6);
        horizontalLayout->setContentsMargins(11, 11, 11, 11);
        horizontalLayout->setObjectName(QString::fromUtf8("horizontalLayout"));
        verticalLayout = new QVBoxLayout();
        verticalLayout->setSpacing(6);
        verticalLayout->setObjectName(QString::fromUtf8("verticalLayout"));
        stackedWidget = new QStackedWidget(centralWidget);
        stackedWidget->setObjectName(QString::fromUtf8("stackedWidget"));
        page = new QWidget();
        page->setObjectName(QString::fromUtf8("page"));
        horizontalLayout_2 = new QHBoxLayout(page);
        horizontalLayout_2->setSpacing(6);
        horizontalLayout_2->setContentsMargins(11, 11, 11, 11);
        horizontalLayout_2->setObjectName(QString::fromUtf8("horizontalLayout_2"));
        tabWidget = new QTabWidget(page);
        tabWidget->setObjectName(QString::fromUtf8("tabWidget"));
        tab = new QWidget();
        tab->setObjectName(QString::fromUtf8("tab"));
        horizontalLayout_3 = new QHBoxLayout(tab);
        horizontalLayout_3->setSpacing(6);
        horizontalLayout_3->setContentsMargins(11, 11, 11, 11);
        horizontalLayout_3->setObjectName(QString::fromUtf8("horizontalLayout_3"));
        textEdit = new QTextEdit(tab);
        textEdit->setObjectName(QString::fromUtf8("textEdit"));

        horizontalLayout_3->addWidget(textEdit);

        tabWidget->addTab(tab, QString());
        tab_2 = new QWidget();
        tab_2->setObjectName(QString::fromUtf8("tab_2"));
        tabWidget->addTab(tab_2, QString());

        horizontalLayout_2->addWidget(tabWidget);

        stackedWidget->addWidget(page);
        page_2 = new QWidget();
        page_2->setObjectName(QString::fromUtf8("page_2"));
        stackedWidget->addWidget(page_2);

        verticalLayout->addWidget(stackedWidget);


        horizontalLayout->addLayout(verticalLayout);

Open in new window

0
 
HappyCactusCommented:
Ah, the setLayout is not necessary because the layout are created with the parent as an argument.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.