Solved

Qt: Set size of child QWidget same as parent widget

Posted on 2010-09-18
12
3,266 Views
Last Modified: 2013-11-23
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.
0
Comment
Question by:DarkyCoder
  • 7
  • 5
12 Comments
 
LVL 12

Expert Comment

by:HappyCactus
ID: 33710683
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
 

Author Comment

by:DarkyCoder
ID: 33716635
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
 
LVL 12

Expert Comment

by:HappyCactus
ID: 33716731
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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:DarkyCoder
ID: 33718759
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
 
LVL 12

Expert Comment

by:HappyCactus
ID: 33719694
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
 

Author Comment

by:DarkyCoder
ID: 33723746
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
 
LVL 12

Expert Comment

by:HappyCactus
ID: 33723752
Can you provide a complete, very small, project (as zip) ?
0
 

Author Comment

by:DarkyCoder
ID: 33723862
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
 
LVL 12

Assisted Solution

by:HappyCactus
HappyCactus earned 500 total points
ID: 33723938
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
 

Author Comment

by:DarkyCoder
ID: 33724033
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
 
LVL 12

Accepted Solution

by:
HappyCactus earned 500 total points
ID: 33724058
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
 
LVL 12

Expert Comment

by:HappyCactus
ID: 33724064
Ah, the setLayout is not necessary because the layout are created with the parent as an argument.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question