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

What signal is raised by QLineEdit when you click on the 'clearButton'?



  • Using Qt 5.12 on Windows 10
    I have a QLineEdit widget with setClearButtonEnabled(true).
    I want to do something when the user clicks on the clear button. I can find nothing in the documents about a signal. I see there is a reference to a QAction, so I added one to capture the triggered signal, but none came.

    There is a signal textChanged which probably gets sent but I only want the act on 'returnPressed", not a keystroke. How can I capture clearButton pressed?


  • Moderators

    hi @dougf9 and welcome

    I can find nothing in the documents about a signal. I see there is a reference to a QAction, so I added one to capture the triggered signal, but none came.

    How did you do that exactly, because from what I can see, a QAction is added automatically by the setClearButtonEnabled

    I'm not quite sure, how you would explicitly search/get the auto created QAction. but until someone else suggest something different, this could work:

    auto list = myLineEdit->findChildren();
    for(QObject *obj : list){
         QAction *a = qobject_cast<QAction*>(obj);
         if(a){
            your connect goes here, not sure if there are other QActions already on the QLineEdit or not. Some more testing required I guess.
         }
    }
    


  • Thanks for the suggestion. I coded this:

    auto list = m_search->children();  //  findChildren();
    qDebug() << "LE list size:" << list.size();
    for(QObject *obj : list)
    {
        QAction *a = qobject_cast<QAction*>(obj);
        qDebug() << "LE child: obj/a" << obj << a;
        if (a)
        {
            //your connect goes here, not sure if there are other QActions already on the QLineEdit or not. Some more testing required I guess.
            connect(a, &QAction::triggered,
                    [this]()
            {
                qDebug()<<"LE triggered  .text:" << m_search->text();
                m_search->setText(tr(""));
                emit m_search->returnPressed();;
            });
            break;
        }
    }
    

    When I ran it, here is the debug output:

    LE list size: 3
    LE child: obj/a QWidgetLineControl(0x207691e05f0) QAction(0x00)
    LE child: obj/a QAction(0x2076920ebd0, name = "_q_qlineeditclearaction") QAction(0x2076920ebd0 text="" menuRole=TextHeuristicRole visible=true)
    LE child: obj/a QLineEditIconButton(0x20768e447d0) QAction(0x00)

    When the action is triggered, the text() is unchanged (much surprise!). So I cleared it and generated the returnPressed() signal. That worked like a charm!

    However did you know to look for actions? Did you write the Qt code? Why did they provide a button to push and no (obvious) was to capture the button press?

    Please close this issue as SOLVED. Thanks!



  • @dougf9 said in What signal is raised by QLineEdit when you click on the 'clearButton'?:

    Please close this issue as SOLVED. Thanks!

    You need to do that please. Thanks.


Log in to reply