Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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


  • 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
    Hi
    I have tried this but the icon does not show up

    	m_action = new QAction(this);
    
    	QIcon icon(":/cross.png");
    	m_action->setIcon(icon);
    
    	ui->nameEdit->addAction(m_action);
    

  • 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