PDA

View Full Version : only some of the widgets are resizing with parent



yi gi
13th October 2015, 06:42
Hi all,
I have created a custom widget, A, which I assign as the central widget.
In its constructor I create and assign to it two other custom widgets - B and C.
in A's constructor I put B and C inside a QVBoxLayout and add this layout to widget A.

When I run the application and resize the window the B widget resizes properly but the C widget does not.
I am attaching the redacted .h files and constructors of all these widgets.

Any help will be appreciated.

A.h:


class A : public QWidget
{
Q_OBJECT

public:
explicit A(QWidget *parent = 0);
~A();
virtual int heightForWidth ( int w ) const { return w*535/1645;}

private:
Ui::A *ui;
B* header_widget;
C* body_widget;
QVBoxLayout* main_layout;
void setMainLayout();
};


A.cpp:


A::A(QWidget *parent) : QWidget(parent), ui(new Ui::A)
{
ui->setupUi(this);
setMainLayout();
this->setLayout(main_layout);
}

A::~A()
{
delete ui;
}


void A::setMainLayout(){
main_layout = new QVBoxLayout();

header_widget = new B(this);
body_widget = new C(this);

main_layout->addWidget(B);
main_layout->addWidget(C);
}


B.h:


class B : public QWidget
{
Q_OBJECT

public:
explicit B(QWidget *parent = 0);
~B();
virtual int heightForWidth ( int w ) const { return w*535/1645;}


private:
Ui::B *ui;
};


C.h:


class C : public QStackedWidget
{
Q_OBJECT

public:
explicit C(QWidget *parent = 0);
~C();
virtual int heightForWidth ( int w ) const { return w*535/1645;}

private:
Ui::C *ui;
A *widgetParent;
};

anda_skoa
13th October 2015, 09:09
Hmm.
Since you create the content of A in code, why do you call ui->setUi(this)?

Cheers,
_

yi gi
13th October 2015, 13:51
Because it is auto-generated and I'd rather not mess with things I'm not sure about.
Also when I remove it nothing changes.

anda_skoa
13th October 2015, 14:04
Because it is auto-generated and I'd rather not mess with things I'm not sure about.
Also when I remove it nothing changes.

I would definitely remove anything that could be interfering and is not used at all.
Leaving it in just makes it likely that you accidentally modify the designer file at some point and then wonder why your widget is messed up.

So, what exactly happens right now?
What does "not resize properly" mean exactly? what is C's resize behavior?

Cheers,
_

yi gi
13th October 2015, 14:38
Hi anda_skoa,
I'm sorry but apparently I was mistaken in the original post.
As you can see widget C is a QStackedWidget with a QStackedLayout to which I add 4 different custom widgets, I.e. the hierarchy is:
A->B,C
C->D1,D2,D3,D4

Now when I resize the window both B and C resize accordingly (i.e. expand/contract with A), but D1...D4 stay exactly the same.
It might also be interesting to note that when I remove
ui->setupUi(this); from one of the stacked widgets (D1..D4) their content disappears.

anda_skoa
13th October 2015, 15:37
Ok, so B and C resize correcty, but the pages of C do not.

Did you add the pages in designer (are they part of Ui::C) or did you add them manually using QStackedWidget::addWidget()?

Cheers,
_

yi gi
13th October 2015, 16:33
Everything is done programmatically.
In C I created a QStackedLayout, added each page using addWidget() and added the layout to C.

anda_skoa
13th October 2015, 16:56
Why did you create a stack layout?

C is a subclass of QStackedWidget, there is no need for that.

Also why do you have a designer form class if you are not using designer?

Cheers,
_

newcfd
14th October 2015, 01:52
be aware that all destructors have to be virtual

anda_skoa
14th October 2015, 10:32
be aware that all destructors have to be virtual

There is no new base class here, all classes are derived from QWidget and therefore QObject, which already has a virtual destructor.

That comment does not contribute to this thread at all.

Cheers,
_

yi gi
15th October 2015, 18:12
anda_skoa,

First off - thank you very much. removing the stackedLayout and adding the widgets with addWidget really solved the problem. I also removed all references to the UI from the project.
I now face a different resize problem - the widgets inside the QStackedWidget all have a similar structure:

QWidget header, QWidget content, QWidget footer.
all of whom are places inside a QGridLayout.

The header widget has inner content and is set fine.
The footer is currently empty (and will actually only be used in some of the pages)
The content varies from page to page.

For example in the first page content widget I have 3 QPushButtons set in this layout:


home_layout = new QGridLayout();
home_layout->addWidget(btn1,0,1,1,1);
home_layout->addWidget(btn2,1,1,1,1);
home_layout->addWidget(btn3,2,3,1,1);

home_layout->setColumnStretch(0,1);
home_layout->setColumnStretch(1,2);
home_layout->setColumnStretch(2,1);

home_layout->setRowStretch(0,1);
home_layout->setRowStretch(1,1);
home_layout->setRowStretch(2,1);


with the layout being added to the content afterwards.

The problem is that while the header shows up and is resized as expected when resizing the parent - the content stays a small rectangle in the middle of the page.
The goal is to have the page fill up the entire row it occupies. Any ideas what can I do about it?

yi gi
15th October 2015, 20:50
To make things a bit easier to understand I made a PageWidget that sums it up.
All pages inherit from this class and in all cases the content of the "content" and "footer" widgets is attached using "setLayout" in the subclass.


PageWidget.h:



class PageWidget : public QWidget
{
Q_OBJECT

public:
explicit PageWidget(QWidget *parent = 0, QString& HeaderTitle = QString());
~PageWidget();

HeaderWidget* header;
QWidget* content;
QWidget* footer;
QGridLayout* layout;
};


PageWidget.cpp:


#include "pagewidget.h"

PageWidget::PageWidget(QWidget *parent, QString &HeaderTitle) :
QWidget(parent)
{
header = new HeaderWidget(this, HeaderTitle);
content = new QWidget(this);
footer = new QWidget(this);

layout = new QGridLayout(this);
layout->addWidget(header,0,0,1,1,Qt::AlignCenter);
layout->addWidget(content,1,0,5,1,Qt::AlignCenter);
layout->addWidget(footer,2,0,1,1,Qt::AlignCenter);

setLayout(layout);
}

PageWidget::~PageWidget(){}

anda_skoa
16th October 2015, 08:35
For the PageWidget I would suggest just using a QVBoxLayout and using a non zero value as the second argument to addWidget for "content".

Cheers,
_

yi gi
16th October 2015, 10:42
That did the trick. Thanks again!