Nominate our 2022 Qt Champions!

QLineEdit indicator

  • Hi
    I would like to have an indicator associated with a qlineedit to show when a field on a form has been edited.(similar to the clear button functionality)
    Has anyone any suggestions as to how this can be done?


  • I would suggest to change the stylesheet to perhaps change the color when the user edits the text and clears it when submitted:

        QWidget* myWidget = new QWidget(nullptr);
        QVBoxLayout *vboxLayout = new QVBoxLayout(myWidget);
        QLineEdit* edit = new QLineEdit("Test");
        connect(edit, &QLineEdit::textChanged, [edit]{edit->setStyleSheet("background-color: yellow");});
        QPushButton* submitButton = new QPushButton("Submit");
        connect(submitButton, &QPushButton::clicked, [edit]{edit->setStyleSheet(""); });

    The above code is a very simple example to show the idea. But you can update as you need to.

  • Thanks - something to think about

  • Moderators

    A less manual solution is to add an action to the line edit using addAction(). Then you can just change the action's icon to indicate the state.

  • @Chris-Kawa
    I have tried this but the icon does not show up

    	m_action = new QAction(this);
    	QIcon icon(":/cross.png");

  • Moderators

    Should be something like:

     QIcon icon(":/cross.png");
     m_action = ui->nameEdit->addAction(icon, QLineEdit::TrailingPosition);

  • Thanks got it now

Log in to reply