4

How can I put a static text in QLineEdit in Qt C++ so that it can not be deleted and when I write to QLineEdit, it should not be spaced.

Ezee
  • 4,214
  • 1
  • 14
  • 29
Programming
  • 143
  • 1
  • 2
  • 10

2 Answers2

8

There is no regular way to put a prefix or postfix in QLabel.

Placeholder

As far as you can get with QLineEdit is to set a text which will be displayed when there is no text indide - see QLineEdit::placeholderText.

enter image description here

InputMask

Another way to do it with QLineEdit is to set inputMask but it will change a cursor and will require a specific amount of letters.

enter image description here

Postfix with QLabel

If you know maximum amount of symbols and want to make a postfix, you can get it with another QLabel:
1. Limit length of the text to have a certain free space at right.
2. Place QLabel to the right side of the QLineEdit and enter a postfix text into it.

NOTE: you won't be able to put QLabel on QLineEdit in QtDesigner if the QLineEdit is inside of a layout. In this case you can add QWidget instead of QLineEdit in the layout and put QLineEdit and QLabel within this widget which doesn't have a layout. Also you can create QLabel in the code:

QLabel* label = new QLabel(this);
label->setText("kg");
label->move(lineEdit->rect().right() - label->width() - 3, lineEdit->rect().center().y() - label->height() / 2);

enter image description here

Custom Widget

Most flexible way to add a postfix and a prefix is to create a new class inherited from QWidget add two QLabels (prefix and postfix) info it, add QLineEdit between them and use css to make them look like a single QLineEdit.

enter image description here

On this image: Prefix and postfix are QLabels. _some_text_ is QLineEdit named lineEdit and all of them are inside a QWidget named complexLineEdit in a horizontal layout.

Here is a css I used for the image above:

QWidget#complexLineEdit
{
  border-top: 1px solid #CCCCCC;
  border-left: 1px solid #DDDDDD;
  border-right: 1px solid #DDDDDD;
  border-bottom: 1px solid #DDDDDD; 
  background-color: white;
}

QWidget#complexLineEdit QLineEdit#lineEdit
{
   border: 0px;
}

You can play with it to make it even more similar to QLineEdit.

Ezee
  • 4,214
  • 1
  • 14
  • 29
0

I would just dynamically re-add the suffix after each text change.

My answer is in Python, but hopefully you get the idea behind it and it can help users from the future.

def __init__(self):

    ...
    # Connect the .textChanged signal of your QLineEdit to the method below.
    self.ui.weight_le.textChanged.connect(self.enforce_line_edit_suffix)
    ...


def enforce_line_edit_suffix(self):
    """This method parses the QLineEdit text and makes sure the desired suffix is there."""

    text = self.ui.weight_le.getText()

    if not text.endswith("kg"):
        self.ui.weight_le.blockSignals(True) # Prevent recursive calls to this method.
        self.ui.weight_le.setText(text + "kg")
        self.ui.weight_le.blockSignals(False)

Alternatively, if you need several QLineEdits to behave like that, you could use:

def __init__(self):

    ...
    # Connect the .textChanged signal of your QLineEdit's to the method below
    # and force the "suffix" parameter.
    self.ui.weight_le.textChanged.connect(lambda: self.enforce_line_edit_suffix(suffix="kg"))
    self.ui.bias_le.textChanged.connect(lambda: self.enforce_line_edit_suffix(suffix="V"))
    self.ui.capacitance_le.textChanged.connect(lambda: self.enforce_line_edit_suffix(suffix="µF"))
    ...


def enforce_line_edit_suffix(self, suffix):
    """This method parses the QLineEdit text and makes sure the desired suffix is there."""

    line_edit = self.sender() # A reference to which QLineEdit triggered the method.
    text = line_edit.getText()

    if not text.endswith(suffix):
        line_edit.blockSignals(True) # Prevent recursive calls to this method.
        line_edit.setText(text + suffix)
        line_edit.blockSignals(False)

For extra safety, we could also imagine checking whether the text (once stripped of the suffix) contains only +-.,0123456789, otherwise your user could type 1.23 kgkg and that would still work.

Guimoute
  • 4,407
  • 3
  • 12
  • 28