alphanumeric
alphanumeric

Reputation: 19379

How to change QLineEdit spacing between text and its edge

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_()

Upvotes: 4

Views: 1792

Answers (1)

eyllanesc
eyllanesc

Reputation: 244291

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_()

Upvotes: 2

Related Questions