There is a lineEdit needs to be done so that when you click on it, it is dropped, and you can enter a new text into it.

Here is my example, but there is a problem, I want to make one click, and it turns out only from the 2nd.

 connect::(ui->lineEdit_password, SIGNAL(selectionChanged()), this, SLOT(clearPassword())); void MainWindow::clearPassword() { ui->lineEdit_password->clear(); ui->lineEdit_password->setEchoMode(QLineEdit::PasswordEchoOnEdit); ui->lineEdit_password->setReadOnly(0); } 

    3 answers 3

    You can override mousePressEvent.

     #include <QLineEdit> class LineEdit : public QLineEdit { public: LineEdit(QWidget *parent = 0): QLineEdit(parent) {} protected: virtual void mousePressEvent(QMouseEvent* /*event*/) { clear(); } }; 

      Try overriding focusInEvent

       #include <QLineEdit> class LineEdit : public QLineEdit{ public: LineEdit(QWidget *parent = 0): QLineEdit(parent) {} protected: void focusInEvent(QFocusEvent *event){ clear(); QLineEdit::focusInEvent(event); } }; 

        QWidget :: mousePressEvent

        QLineEdit :: clear

        • Please try to publish detailed answers containing a specific example of the minimum solution, supplementing them with a link to the source. Answers –references (as well as comments) do not add knowledge to the Runet. - Nicolas Chabanovsky ♦
        • @NicolasChabanovsky Kopipasta doesn’t add knowledge either, but at the same time it is obsolete and does not give a complete picture. - free_ze
        • Copying does not add, recommendations based on personal expertise are unique in most cases. I am sure the author of the question can easily find the answer, consisting of links, in any search engine. - Nicolas Chabanovsky ♦
        • @NicolasChabanovsky Personal expertise is best kept to a minimum if there is detailed documentation on this issue (literally). The task of the resource is to help the questioner find the answer, which I did. It might have been worthwhile to emphasize the connection between the method and the event in words, but this in no way pulls down the daevvoot. - free_ze
        • The system does not welcome answers –references . - Nicolas Chabanovsky ♦