[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Qt: Set size of child QWidget same as parent widget

Posted on 2010-09-18
12
Medium Priority
?
4,360 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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 2000 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 2000 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

829 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