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?

user3034039
  • 43
  • 1
  • 5
  • Check if the advice from [this answer](http://stackoverflow.com/a/21316703/1329652) 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. – Kuba hasn't forgotten Monica Mar 19 '14 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! – user3034039 Mar 19 '14 at 14:34
  • 1
    I mean literally what I say. For each width, there's a different height, since the height depends on width. – Kuba hasn't forgotten Monica Mar 19 '14 at 14:37
  • See if this question helps - http://stackoverflow.com/questions/2427103/qt-how-to-force-a-hidden-widget-to-calculate-its-layout – sashoalm Mar 19 '14 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. – Kuba hasn't forgotten Monica Mar 19 '14 at 14:43
  • @KubaOber Can't you later reset the attribute and show the widget? – sashoalm Mar 19 '14 at 14:45
  • @sashoalm That's entirely unnecessary. If you need the size of the widget, you simply need to get it after the widget is shown, or when it's just about to be shown. – Kuba hasn't forgotten Monica Mar 19 '14 at 14:47
  • My experience is that the usual reason for needing a size of a widget is to attempt manual layout. Manual layout should be done by subclassing `QLayout`, that's what it's there for - if you need it at all. user3034039: What is the problem you're trying to solve? Why do you think you need the size of the widget? Never mind that the code example you should give is maybe 15 lines in *C++*. – Kuba hasn't forgotten Monica Mar 19 '14 at 14:50
  • @sashoalm You can't get the size right after the `show()` call, but after the event loop has run after the show() call and all of the events were processed. `QAbstractEventDispatcher::aboutToBlock()` is the signal you can hook. – Kuba hasn't forgotten Monica Mar 19 '14 at 14:53
  • Kuba Ober : I'm trying to figure out why Word Wrapping is screwing my layout (it puts this weird padding of a few hundred pixels over and below the item. The text is word wrapped, but there's a lot of empty spaces above and below it). I'm also using PyQT, but the code I'm working on right now is complex. That could happen from a bunch of stuff, but so far I think it's from the .setWordWrap() – user3034039 Mar 19 '14 at 15:51
  • @user3034039. Can you at least post a screenshot (or a link to one)? – ekhumoro Mar 19 '14 at 21:44
  • Hey guys! Quick update ; I've found out that it's an inherent problem of the logic of the way we create our layouts. I'm going to change it altogether! – user3034039 Mar 20 '14 at 15:58

1 Answers1

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
Anke_Pet
  • 354
  • 1
  • 3
  • 10