Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Connecting the clear button of a QLineEdit to a custom slot
Qt 6.11 is out! See what's new in the release blog

Connecting the clear button of a QLineEdit to a custom slot

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 1.7k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • l3u_L Offline
    l3u_L Offline
    l3u_
    wrote on last edited by
    #1

    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 ;-)

    mrjjM 1 Reply Last reply
    0
    • l3u_L l3u_

      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 ;-)

      mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @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.

      1 Reply Last reply
      2
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

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

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        l3u_L 1 Reply Last reply
        2
        • SGaistS SGaist

          Hi,

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

          l3u_L Offline
          l3u_L Offline
          l3u_
          wrote on last edited by
          #4

          @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!

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved