Results 1 to 5 of 5

Thread: QLabels not getting resizeEvent--how to invoke?

  1. #1
    Join Date
    Jun 2012
    Posts
    219
    Thanks
    28
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default QLabels not getting resizeEvent--how to invoke?

    I have a QLabel derived objects in a layout, in a centralWidget, in a QMainWindow. I want them to resize whenever the statusBar, menuBar, or toolBar are hidden/shown. (I have code that chooses a font based on the QLabels "contentsRect")

    I expected their resizeEvent methods to be called when the bars are hidden and shown, but they are not. It looks like the frame that contains the QLabels expands when the bars are hidden (and vice-versa) but the QLabel resizeEvent is not called.

    What is the correct way to explicitly invoke the resizeEvent() methods for these QLabel derived objects?

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QLabels not getting resizeEvent--how to invoke?

    I expected their resizeEvent methods to be called when the bars are hidden and shown, but they are not.
    I am guessing that the layout is calling QWidget::resize() directly on your label, and that triggers a repaint of the widget. As an experiment, you could replace your custom QLabel with a custom widget derived directly from QWidget and see what happens there. If you do that, be sure to implement the sizeHint() (and maybe minimumSizeHint()) method so the layout has some guidance.
    Last edited by d_stranz; 3rd February 2023 at 18:14.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. The following user says thank you to d_stranz for this useful post:

    davethomaspilot (3rd February 2023)

  4. #3
    Join Date
    Jun 2012
    Posts
    219
    Thanks
    28
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QLabels not getting resizeEvent--how to invoke?

    Here's the code that implements the resizeEvent method in the deriveed class, ScaleTextLabel:

    Qt Code:
    1. void ScaleTextlabel::resizeEvent(QResizeEvent* event)
    2. {
    3. qDebug() << "resizing";
    4. QLabel::resizeEvent(event);
    5. m_resizeTimer->start();
    6. }
    To copy to clipboard, switch view to plain text mode 

    resizeEvent DOES get called, but not when the bars are hidden/shown.

    Not sure how I'd do the experiment. You mean, just change the base class to QWidget but with nothing else? What would the experiment reveal?

  5. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QLabels not getting resizeEvent--how to invoke?

    resizeEvent DOES get called, but not when the bars are hidden/shown.
    Sorry, I misread your original post as saying the event was not called at all.

    QLabel and similar widgets have a preferred size and a size policy. They won't necessarily grow or shrink if the size policy won't let them. So the layout might be just adding / removing space around them instead of resizing.

    Then there is this from QWidget::sizePolicy():

    If there is a QLayout that manages this widget's children, the size policy specified by that layout is used. If there is no such QLayout, the result of this function is used.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  6. #5
    Join Date
    Jun 2012
    Posts
    219
    Thanks
    28
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QLabels not getting resizeEvent--how to invoke?

    Quote Originally Posted by d_stranz View Post
    Sorry, I misread your original post as saying the event was not called at all.

    QLabel and similar widgets have a preferred size and a size policy. They won't necessarily grow or shrink if the size policy won't let them. So the layout might be just adding / removing space around them instead of resizing.

    Then there is this from QWidget::sizePolicy():
    The QLabel and it's containing QFrame are definitely shrinking and expanding as other widgets in the same verticalLayout are hidden and shown.
    I know this, because the font size used in the QLabel scales based on the contentsRect. And all works fine when I explicitly call the resize method instead of depending on it getting invoked from the resizeEvent handler.

    So, I can add the explict call whenever I do something that will result in a resize, but it seems like like the resizeEvent handler should be called.

Similar Threads

  1. Carousel with QLabels
    By ZajtiM in forum Newbie
    Replies: 1
    Last Post: 19th September 2021, 01:38
  2. Two QLabels transparent to each other
    By augusbas in forum Qt Programming
    Replies: 3
    Last Post: 3rd November 2010, 07:19
  3. How to show QLabels
    By Pavel Abrosimov in forum Qt Programming
    Replies: 24
    Last Post: 9th July 2009, 10:39
  4. Dynamic QLabels
    By rajeshs in forum Qt Programming
    Replies: 3
    Last Post: 30th May 2008, 12:26
  5. QSpinBox or QLabels
    By ToddAtWSU in forum Qt Programming
    Replies: 10
    Last Post: 27th November 2007, 21:10

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.