Solved 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?Thanks
-
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(""); }); vboxLayout->addWidget(edit); vboxLayout->addWidget(submitButton); myWidget->show();
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
-
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
Hi
I have tried this but the icon does not show upm_action = new QAction(this); QIcon icon(":/cross.png"); m_action->setIcon(icon); ui->nameEdit->addAction(m_action);
-
Should be something like:
QIcon icon(":/cross.png"); m_action = ui->nameEdit->addAction(icon, QLineEdit::TrailingPosition);
-
Thanks got it now