1

I want to underline the contents of a QLineEdit when shown on a UI -- the number happens to be a formatted (dollar) number, like $14,577

I do NOT want to put in a "border-bottom" across the length of the entire line edit, NOR do I want to use the QT Designer to set the font/underline because I want to underline only in certain circumstances.

Here is the line of code I use to show and format the number. Can it be changed to include underlining?

self.ui.car1.setText("${:,}".format(self.car1))
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Dennis
  • 269
  • 1
  • 13

1 Answers1

0

QLineEdit does not allow you to implement what you want so a workaround is to configure a QTextEdit with a single line (combining some answers of this question) and set the underline using html or QTextCharFormat:

import sys
from PyQt5 import QtCore, QtGui, QtWidgets


class LineEdit(QtWidgets.QTextEdit):
    returnPressed = QtCore.pyqtSignal()

    def __init__(self, parent=None):
        super().__init__(parent)
        # single line configuration
        self.setLineWrapMode(QtWidgets.QTextEdit.NoWrap)
        self.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
        self.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
        self.setFixedHeight(
            self.document().documentLayout().documentSize().height()
            + self.height()
            - self.viewport().height()
        )

    def keyPressEvent(self, event):
        if event.key() in (QtCore.Qt.Key_Return, QtCore.Qt.Key_Enter):
            self.returnPressed.emit()
            return
        super().keyPressEvent(event)


if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    w = QtWidgets.QWidget()

    textedit = LineEdit()

    textedit.setHtml("""This is a <u>parragraph</u> :-)""")

    lay = QtWidgets.QVBoxLayout(w)
    lay.addWidget(textedit)
    lay.addStretch()
    w.show()
    sys.exit(app.exec_())

Output:

enter image description here

eyllanesc
  • 235,170
  • 19
  • 170
  • 241