2

First of all, I'm sorry because I don't have any code examples to provide (it's quite complex to narrow it down).

Essentially, I have QLabel and I'd like to access the height of the label after the word wrap has been applied.

It seems that it always return me the default value (640x480) instead of the actually height it needs (427 pixels).

It's weird because, without .setWordWrap, I get the correct value (16449 x 13).

Any ideas?

12
  • Check if the advice from this answer would work for you. The size is not guaranteed to be valid before the widgets are laid out, and they are laid out as they are made visible. First step would be to check if you get the correct size after the widget is visible on the screen (say 1 second after show(), but make sure you're not blocking the event loop). Remember that a word wrapped label has height-for-width and it can only give a height for a certain width, not in abstract. Mar 19, 2014 at 14:28
  • Sorry, what did you mean by "a word wrapped label has height for width", because I think it might be important in my case! Mar 19, 2014 at 14:34
  • 1
    I mean literally what I say. For each width, there's a different height, since the height depends on width. Mar 19, 2014 at 14:37
  • See if this question helps - stackoverflow.com/questions/2427103/…
    – sashoalm
    Mar 19, 2014 at 14:37
  • @sashoalm The answer you link to only applies when the widget is meant to stay invisible. It also doesn't address the height-for-width issue. Mar 19, 2014 at 14:43

1 Answer 1

3

Just inherit a custom class from QLabel and reimplement the function "resizeEvent"

class NewLabel(QLabel):
    def __init__(self, text):
        super(NewLabel, self).__init__(text)

    def resizeEvent(self, event):
        width = self.width()
        height = self.height()

        # you can then emit a signal with the size information

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.