1

In the following code, characterLabel is small a bit. But characterLabelEntire is too big. How to create QLabel which just fits its text?

from PySide.QtCore import *
from PySide.QtGui import *    

targetChar = unichr(0x0e0e)

# Setting
font = QFont()
font.setPointSize(50)
sizePolicy = QSizePolicy(QSizePolicy.Maximum, QSizePolicy.Maximum)

# Following code will create a character label.
# But it seems the size of the label is small a bit.
# Becasue some of the region of the character is lost.
characterLabel = QLabel(targetChar)
characterLabel.setFont(font)
characterLabel.setSizePolicy(sizePolicy)

# Following code will create a character label, but it's too big.
characterLabelEntire = QLabel(targetChar)
characterLabelEntire.setFont(font)
characterLabelEntire.setFixedSize(100, 100)
characterLabel.setSizePolicy(sizePolicy)

# Show and Raise
characterLabel.show()
characterLabelEntire.show()

characterLabel.raise_()
characterLabelEntire.raise_()

try:
    app = QApplication([])
    app.exec_()
except:
    pass

It seems Exotic character designer break font rule?

I found the following article.

http://doc.qt.io/qt-4.8/qfontmetrics.html#descent

Anyway, in my environment(OS X 10.11), larger descent is needed to show the entire character image.

from PySide.QtCore import *
from PySide.QtGui import *

try:
    app = QApplication([])
except:
    pass

targetChar = unichr(0x0e0e)

# Setting
font = QFont()
font.setPointSize(100)

# Following code will create a character label.
characterLabel = QLabel(targetChar)
characterLabel.setFont(font)

fm = characterLabel.fontMetrics()
pixelsWide = fm.width(targetChar)
pixelsHigh = fm.ascent() + fm.descent() * 3
characterLabel.setFixedSize(pixelsWide, pixelsHigh)

characterLabel.show()
characterLabel.raise_()

try:
    app.exec_()
except:
    pass
ymoreau
  • 3,402
  • 1
  • 22
  • 60
Kei Minagawa
  • 4,395
  • 3
  • 25
  • 43
  • Possible duplicate of [Scaling QLabel to accommodate the contained text and nothing more](http://stackoverflow.com/questions/9224960/scaling-qlabel-to-accommodate-the-contained-text-and-nothing-more) – hyde Mar 21 '16 at 10:05
  • 1
    @hyde assumed that you need change font size so the text fill given space (size). For me you need change size of the widget in such way that it matches space required by given text with specific font. So what exactly do you need? – Marek R Mar 21 '16 at 10:22
  • @MarekR: I don't want to change font size. I want to change size of the widget so that it fits its own text. – Kei Minagawa Mar 21 '16 at 10:28
  • than try: `sizePolicy = QSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)`. This means treat sizeHint (which gives perfect size) as a fixed size of the widget. When you used value `Maximum` than you saying that perfect size should be maximal possible value. – Marek R Mar 21 '16 at 11:43

0 Answers0