PDA

View Full Version : Resize widget towards Right



uz_qt
1st October 2013, 17:22
Hello,

Please have a look at the attachment (shot.jpg). I have two widgets. Widget1 is placed first and its size is fixed (as desired).
The Widget2 is places next to it using the addStrech() method.

Following are the lines relevant to placement of Widget2:


QHBoxLayout *mLayout = new QHBoxLayout(this);
mWidget2 = new Widget2(this); //assigning pointer to Widget2

mLayout->addStretch(0);
mLayout ->addWidget(mWidget2);

When I resize the window, I would want to resize Widget2 towards right while keeping it fixed at the left (also shown in attachement).

How can I achieve this?

anda_skoa
2nd October 2013, 10:39
addStretch() adds an empty area that stretches when the parent widget is resized.
What you want is to assign a stretch value to widget2



mLayout ->addWidget(mWidget2, 1); // any value other than 0


Cheers,
_

uz_qt
2nd October 2013, 12:34
@anda: I have tried what you suggested. But when I remove addStretch() and assign a strech value to widget2, then widget2 spreads from left to right over the widget1.
I have attached a picture aswell of whats happening after I implement your advice.

anda_skoa
2nd October 2013, 13:43
Are you sure widget1 is properly added to the layout?
It should look like something like this


QWIdget *widget1 = new Widget1(this);
QWidget *widget2 = new Widget2(this);

QHBoxLayout *layout = new QHBoxLayout(this);
layout->addWidget(widget1);
layout->addWidget(widget2, 1);


Cheers,
_

uz_qt
2nd October 2013, 14:11
Actually the implementation is not so straightforward. Widget1 does not belong to the layout. It is placed inside scroll area as a seperate layout.
Like as shown:


mScrollArea = new QScrollArea(this); //mScrollArea is fixed (desired)

mWidget = new QWidget(mScrollArea);

mScrollArea->setWidget(mWidget);
mScrollArea->setWidgetResizable(true);

mSubLayoutScroll = new QVBoxLayout(mWidget);
mWidget->setLayout(mSubLayoutScroll);

mWidget1= new Widget1(this, mSubLayoutScroll);

Now, next to mSubLayoutScroll, widget2 should be placed. Both layouts are independent.

anda_skoa
2nd October 2013, 17:13
In this case the scroll area goes into the first slot of the hbox layout


QWidget *widget2 = new Widget2(this);

QHBoxLayout *layout = new QHBoxLayout(this);
layout->addWidget(mScrollArea);
layout->addWidget(widget2, 1);


Cheers,
_

uz_qt
3rd October 2013, 22:40
Hi @anda,

I have no idea why mScrollArea is being added to main window without me explicitly specifying to add it in a layout (like u mentioned).

I mean when I run the lines 1 to 11 in mentioned in my previous reply, the mScrollArea is being added and displayed in the main window. Inspite of me not mentioning anywhere a line such as:

layout->addWidget(mScrollArea);

Can you please point out what lines (1 to 11) in my code are responsible for this behavior?

uz_qt
4th October 2013, 07:50
After inspection, I found out that just the line
mScrollArea = new QScrollArea(this); automatically adds the mScrollArea widget to the main window (because of this pointer.
Ofcourse, its area dimensions needs to be specified which I did as:

mScrollArea->setGeometry(QRect(10, 10, 350, 190)); //set fixed (desired)

Therefore, I just initialised mScrollArea as:

mScrollArea = new QScrollArea; and then did:

layout->addWidget(mScrollArea);
layout->addWidget(widget2, 1);

It still does not work! :-(

Please help

anda_skoa
4th October 2013, 08:54
setGeometry for a child widget is always wrong, it has to be in a layout.

If you have a QMainWindow subclass then you need a central widget.
If the scroll area and widget2 are supposed to be side-by-side in that widget, use a QHBoxLayout on that widget and add the two child widgets accordingly.

Cheers,
_

uz_qt
4th October 2013, 09:10
@anda: Thanks a lot for your suggestion! It was really helpful.
I have implemented as per your your advice and it works as desired..! :-)