Results 1 to 10 of 10

Thread: Resize widget towards Right

  1. #1
    Join Date
    Aug 2013
    Posts
    41
    Thanks
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default 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:

    Qt Code:
    1. QHBoxLayout *mLayout = new QHBoxLayout(this);
    2. mWidget2 = new Widget2(this); //assigning pointer to Widget2
    3.  
    4. mLayout->addStretch(0);
    5. mLayout ->addWidget(mWidget2);
    To copy to clipboard, switch view to plain text mode 

    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?
    Attached Images Attached Images

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default 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

    Qt Code:
    1. mLayout ->addWidget(mWidget2, 1); // any value other than 0
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  3. #3
    Join Date
    Aug 2013
    Posts
    41
    Thanks
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default 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.
    Attached Images Attached Images
    Last edited by uz_qt; 2nd October 2013 at 12:41.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Resize widget towards Right

    Are you sure widget1 is properly added to the layout?
    It should look like something like this
    Qt Code:
    1. QWIdget *widget1 = new Widget1(this);
    2. QWidget *widget2 = new Widget2(this);
    3.  
    4. QHBoxLayout *layout = new QHBoxLayout(this);
    5. layout->addWidget(widget1);
    6. layout->addWidget(widget2, 1);
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  5. #5
    Join Date
    Aug 2013
    Posts
    41
    Thanks
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default 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:

    Qt Code:
    1. mScrollArea = new QScrollArea(this); //mScrollArea is fixed (desired)
    2.  
    3. mWidget = new QWidget(mScrollArea);
    4.  
    5. mScrollArea->setWidget(mWidget);
    6. mScrollArea->setWidgetResizable(true);
    7.  
    8. mSubLayoutScroll = new QVBoxLayout(mWidget);
    9. mWidget->setLayout(mSubLayoutScroll);
    10.  
    11. mWidget1= new Widget1(this, mSubLayoutScroll);
    To copy to clipboard, switch view to plain text mode 

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

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Resize widget towards Right

    In this case the scroll area goes into the first slot of the hbox layout
    Qt Code:
    1. QWidget *widget2 = new Widget2(this);
    2.  
    3. QHBoxLayout *layout = new QHBoxLayout(this);
    4. layout->addWidget(mScrollArea);
    5. layout->addWidget(widget2, 1);
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  7. #7
    Join Date
    Aug 2013
    Posts
    41
    Thanks
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default 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:
    Qt Code:
    1. layout->addWidget(mScrollArea);
    To copy to clipboard, switch view to plain text mode 

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

  8. #8
    Join Date
    Aug 2013
    Posts
    41
    Thanks
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Resize widget towards Right

    After inspection, I found out that just the line
    Qt Code:
    1. mScrollArea = new QScrollArea(this);
    To copy to clipboard, switch view to plain text mode 
    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:
    Qt Code:
    1. mScrollArea->setGeometry(QRect(10, 10, 350, 190)); //set fixed (desired)
    To copy to clipboard, switch view to plain text mode 

    Therefore, I just initialised mScrollArea as:
    Qt Code:
    1. mScrollArea = new QScrollArea;
    To copy to clipboard, switch view to plain text mode 
    and then did:
    Qt Code:
    1. layout->addWidget(mScrollArea);
    2. layout->addWidget(widget2, 1);
    To copy to clipboard, switch view to plain text mode 

    It still does not work! :-(

    Please help
    Last edited by uz_qt; 4th October 2013 at 09:05.

  9. #9
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default 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,
    _

  10. The following user says thank you to anda_skoa for this useful post:

    uz_qt (4th October 2013)

  11. #10
    Join Date
    Aug 2013
    Posts
    41
    Thanks
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default 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..! :-)
    Last edited by uz_qt; 4th October 2013 at 09:24.

Similar Threads

  1. Replies: 5
    Last Post: 20th June 2012, 19:02
  2. How to resize parent widget when child widget is resized
    By ainne810329 in forum Qt Programming
    Replies: 4
    Last Post: 29th November 2011, 07:47
  3. child widget resize to parent widget
    By bobFromAccounting in forum Newbie
    Replies: 10
    Last Post: 11th February 2011, 02:53
  4. Widget Resize
    By manojmka in forum Qt Programming
    Replies: 9
    Last Post: 21st May 2010, 12:32
  5. Replies: 1
    Last Post: 1st May 2010, 23:03

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.