Results 1 to 7 of 7

Thread: Prevent a QLabel resizing the parent QWidget

  1. #1
    Join Date
    Oct 2009
    Posts
    22
    Thanked 1 Time in 1 Post

    Default Prevent a QLabel resizing the parent QWidget

    Hi,

    This is probably a very basic question but I can't seem to find the correct way to make it work. I have a QWidget with a horizontal layout, in the layout among other controls is a QLabel. When the text of this label is longer than the widget of the widget, the parent widget is resized to accommodate the text. What I would like is the widget to remain the same size and the text just overflow.

    I can set the parent QWidget size policy to 'Fixed' but the parent QWidget sits in another layout that can be resized.

    So in essence, what I need to do is force the child QLabel width to never expand beyond the parent QWidget width.

    How can I achieve that?

    Thanks.

  2. #2
    Join Date
    Feb 2008
    Posts
    4
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Prevent a QLabel resizing the parent QWidget

    I'm not sure of the "best way", but one a quick way would be to set the maximum height of the QLabel to the correct size to hold one line of text for the font used (look up QWidget::fontMetrics to help with this), then enable word wrapping.

    This will give the effect you describe, for a nicer effect have a further look at QFontMetrics, specifically the elidedText() function.

    Hope this helps.

  3. #3
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Prevent a QLabel resizing the parent QWidget

    Quote Originally Posted by danc81 View Post
    When the text of this label is longer than the widget of the widget, the parent widget
    Sorry, thats just a widget overflow

    If your trying to do what I think your trying to do, why not just use the minimumSize and maximumSize properties?

  4. #4
    Join Date
    Oct 2009
    Posts
    22
    Thanked 1 Time in 1 Post

    Default Re: Prevent a QLabel resizing the parent QWidget

    I've tried setting maximum height and enabling word wrap but the QLabel still expands vertically when the text wraps.

    Do you mean set minimumSize and maximumSize on the container widget? If so, that won't work as the container widget needs to resize according to it's parent (QScrollArea). I just need the QLabel not to overflow the parent QWidget but still allow the parent QWidget to adjust according to it's container QScrollArea.

  5. #5
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Prevent a QLabel resizing the parent QWidget

    So put the label in another container which can't resize?

  6. #6
    Join Date
    Oct 2009
    Posts
    22
    Thanked 1 Time in 1 Post

    Default Re: Prevent a QLabel resizing the parent QWidget

    I'm not sure that would solve anything, the QLabel needs to resize but not beyond the width of its parent. It's parent can resize to any width possible within the bounds of the screen, there is no fixed size to adhere to.

  7. #7
    Join Date
    Oct 2009
    Posts
    22
    Thanked 1 Time in 1 Post

    Default Re: Prevent a QLabel resizing the parent QWidget

    In the end I just resized the child QLabel to fit the parent widget in the resizeEvent(), works as expected but I just thought there must be some design concept I was missing somewhere to achieve it using the correct size properties.

Similar Threads

  1. Promoting the parent QWidget of a QWidget form
    By extrakun in forum Qt Tools
    Replies: 6
    Last Post: 16th April 2010, 14:19
  2. Replies: 1
    Last Post: 15th June 2009, 17:22
  3. Problem with Parent QWidget and Buttons not working
    By VireX in forum Qt Programming
    Replies: 7
    Last Post: 11th May 2007, 22:24
  4. How to prevent Event Propagation to Parent?
    By viking in forum Qt Programming
    Replies: 6
    Last Post: 9th May 2007, 06:29
  5. Prevent from resizing a QMainWindow
    By Flier in forum Qt Tools
    Replies: 5
    Last Post: 14th April 2006, 17:11

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.