Connecting the clear button of a QLineEdit to a custom slot



  • Hi :-)

    Is it possible to connect a QLineEdit's clear button that can be displayed via QLineEdit::setClearButtonEnabled() to a custom slot? I found https://bugreports.qt.io/browse/QTBUG-36257 which contains the following code to do so:

    for (int i(0); i < myLineEdit->children().size(); ++i) {
        QAction *myClearAction(qobject_cast<QAction *>(myLineEdit->children().at(i)));
        if (myClearAction) {
            connect(myClearAction, &QAction::triggered,
                myLineEdit, &QLineEdit::returnPressed,
                Qt::QueuedConnection);
        }
    }
    

    but I wondered if anything has been changed concerning this since then. The solution posted will probably work, but looks a bit like a hack to me ;-)


  • Qt Champions 2017

    @l3u_
    Hi
    Should still work the same.
    And yes its a bit of hack since we look up its child but
    on the other hand, it wont crash if not there and
    we just hook up to your slot.
    So i would use it, even if not pretty.


  • Lifetime Qt Champion

    Hi,

    The alternative is for you to add your own action to the QLineEdit so you ensure that you have full control over it.



  • @SGaist Hey, that's actually a very good idea I did not think about yet – seems like I could simply as well add my own "clear" button with the same icon ;-) Thanks for the hint … sometimes, you miss the forest for the trees!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.