Issue
How to place the Triple Line ('\u2261') to the top of the frame. My first frame is in yellow colour and the Qlabel text is "Python" placed on top of the frame. Likewise, I need to place the Unicode Character 'u\2261' at the top of the Second frame (light green top edge).
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
class QLab(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("QLabel Example")
self.frame_top = QFrame()
self.frame_top.setObjectName("ob_frame_top")
self.color_top = "yellow"
self.frame_top.setStyleSheet(f"QFrame#ob_frame_top{{background-color: {self.color_top};}}")
self.frame_top.setFixedHeight(30)
self.frame_mid = QFrame()
self.frame_mid.setObjectName("ob_frame_mid")
self.color_mid = "lightgreen"
self.frame_mid.setStyleSheet(f"QFrame#ob_frame_mid{{background-color: {self.color_mid};}}")
self.frame_mid.setFixedHeight( 100)
self.frame_all = QFrame()
self.lay_top = QVBoxLayout()
self.lay_mid = QVBoxLayout()
self.lay_all = QVBoxLayout()
self.lay_top = QVBoxLayout(self.frame_top)
self.lay_mid = QVBoxLayout(self.frame_mid)
self.lay_all = QVBoxLayout(self.frame_all)
lbl1 = QLabel("Python")
self.lay_top.addWidget(lbl1)
self.lay_top.setContentsMargins(0,0,0,0)
self.lay_top.setAlignment(Qt.AlignTop)
lbl2 = QLabel('\u2261'*8)
my_font = QFont("Arial Black", 15)
lbl2.setFont(my_font)
self.lay_mid.addWidget(lbl2)
self.lay_mid.setContentsMargins(0,0,0,0)
self.lay_mid.setAlignment(Qt.AlignTop )
self.lay_mid.addStretch()
self.lay_all.setSpacing(0)
self.lay_all.addWidget(self.frame_top)
self.lay_all.addWidget(self.frame_mid)
self.lay_all.addStretch()
self.setLayout(self.lay_all)
def main():
app = QApplication(sys.argv)
ex = QLab()
ex.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
Solution
That's the positioning of the character based on the selected font.
Since it's a character that stands on the font baseline and is usually not very tall, there will always be some level of spacing above, just like any other "small" character, like "x" or "a".
QLabel always try to show the full bounding rectangle of a text, so the only option is to manually force the padding to a negative value computed with the QFontMetrics of the current font: you need to subtract the font's xHeight()
by the ascent()
.
Be aware, though, that there's absolutely no guarantee of the result, as it completely depends on how that specific character is drawn by the font: if you use a font that, for some reason, shows that character taller than other small characters, the content will be probably partially hidden; if it is smaller (or shown below the base line), there will still be some margin above.
lbl2 = QLabel('\u2261'*8)
my_font = QFont("Arial Black", 15)
fm = QFontMetrics(my_font)
padding = fm.xHeight() - fm.ascent()
lbl2.setStyleSheet('margin-top: {}px;'.format(padding))
lbl2.setFont(my_font)
Answered By - musicamante
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.