4
votes

The code below creates a single QLineEdit with its font size set to 9. I would like to make sure there is no spacing between the text and the edge of the LineEdit.

What attribute controls the mentioned spacing?

enter image description here

from PyQt5.QtWidgets import *
app = QApplication(list())
line = QLineEdit()
font = line.font()
font.setPointSize(9)
line.setFont(font)
line.show()
app.exec_()
1

1 Answers

2
votes

The only way that space does not appear is that the height of the QLineEdit is fixed, and to calculate that height QFontMetrics should be used:

from PyQt5.QtWidgets import *
from PyQt5.QtGui import *

app = QApplication(list())
line = QLineEdit()

font = line.font()
font.setPointSize(9)
line.setFont(font)

fm = QFontMetrics(line.font())
line.setFixedHeight(fm.height())

line.show()
app.exec_()