Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QlineEdit with some default text for which cursor should not be moved?

In QT, a created lineEdit shows a text using the setText() method.

  1. But the cursor is movable for the default text. I want the cursor should not be movable for the default text.

  2. My lineEdit type has been set as password. Hence the default text('Password') is also displayed as '********'. Whenever user types the type has to be changed as password and when there is no text or until the user have not typed any text, the lineEdit should display the plain text 'password'

Any idea to fix the above two issues? enter image description here

like image 951
Mathan Kumar Avatar asked May 21 '12 06:05

Mathan Kumar


People also ask

What is QLineEdit in Python?

QLineEdit : It allows the user to enter and edit a single line of plain text with a useful collection of editing functions, including undo and redo, cut and paste, and drag and drop.

How do I edit text in QLineEdit?

You can change the text with setText() or insert() .

How do I create a text box in Qt?

You can use the Text Edit and Text Input components to add text fields where users can enter text. The Text Input component displays a single line of editable plain text, whereas the Text Edit component displays a block of editable, formatted text. Both components are used to accept text input.


2 Answers

In the constructor put

ui->lineEdit->setPlaceholderText("password");
ui->lineEdit->setReadOnly(1);

And in on_lineEdit_selectionChanged() SLOT, put

ui->lineEdit->setText("");
ui->lineEdit->setEchoMode(QLineEdit::Password);
ui->lineEdit->setReadOnly(0);
like image 142
ScarCode Avatar answered Sep 21 '22 14:09

ScarCode


For question 1, in Qt 5.0 and higher, setPlaceholderText does what you want. https://codereview.qt-project.org/#change,45326

like image 37
Jarrod Lombardo Avatar answered Sep 21 '22 14:09

Jarrod Lombardo