1 Attachment(s)
Resize widget towards Right
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:
Code:
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?
Re: Resize widget towards Right
addStretch() adds an empty area that stretches when the parent widget is resized.
What you want is to assign a stretch value to widget2
Code:
mLayout ->addWidget(mWidget2, 1); // any value other than 0
Cheers,
_
1 Attachment(s)
Re: Resize widget towards Right
@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.
Re: Resize widget towards Right
Are you sure widget1 is properly added to the layout?
It should look like something like this
Code:
QWIdget *widget1 = new Widget1(this);
QWidget *widget2
= new Widget2
(this);
layout->addWidget(widget1);
layout->addWidget(widget2, 1);
Cheers,
_
Re: Resize widget towards Right
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:
Code:
mScrollArea
= new QScrollArea(this);
//mScrollArea is fixed (desired)
mWidget
= new QWidget(mScrollArea
);
mScrollArea->setWidget(mWidget);
mScrollArea->setWidgetResizable(true);
mWidget->setLayout(mSubLayoutScroll);
mWidget1= new Widget1(this, mSubLayoutScroll);
Now, next to mSubLayoutScroll, widget2 should be placed. Both layouts are independent.
Re: Resize widget towards Right
In this case the scroll area goes into the first slot of the hbox layout
Code:
QWidget *widget2
= new Widget2
(this);
layout->addWidget(mScrollArea);
layout->addWidget(widget2, 1);
Cheers,
_
Re: Resize widget towards Right
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:
Code:
layout->addWidget(mScrollArea);
Can you please point out what lines (1 to 11) in my code are responsible for this behavior?
Re: Resize widget towards Right
After inspection, I found out that just the line 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:
Code:
mScrollArea
->setGeometry
(QRect(10,
10,
350,
190));
//set fixed (desired)
Therefore, I just initialised mScrollArea as:
and then did:
Code:
layout->addWidget(mScrollArea);
layout->addWidget(widget2, 1);
It still does not work! :-(
Please help
Re: Resize widget towards Right
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,
_
Re: Resize widget towards Right
@anda: Thanks a lot for your suggestion! It was really helpful.
I have implemented as per your your advice and it works as desired..! :-)