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. What signal is raised by QLineEdit when you click on the 'clearButton'?

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

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 4 Posters 2.3k Views
  • 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.
  • D Offline
    D Offline
    Dougf9
    wrote on last edited by
    #1

    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?

    J.HilkJ 1 Reply Last reply
    0
    • D Dougf9

      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?

      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #2

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

      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      1 Reply Last reply
      1
      • D Offline
        D Offline
        Dougf9
        wrote on last edited by
        #3

        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!

        Pablo J. RoginaP 1 Reply Last reply
        0
        • D Dougf9

          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!

          Pablo J. RoginaP Offline
          Pablo J. RoginaP Offline
          Pablo J. Rogina
          wrote on last edited by
          #4

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

          Upvote the answer(s) that helped you solve the issue
          Use "Topic Tools" button to mark your post as Solved
          Add screenshots via postimage.org
          Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          0
          • Cobra91151C Offline
            Cobra91151C Offline
            Cobra91151
            wrote on last edited by Cobra91151
            #5

            @J-Hilk

            Hello!

            I came across this thread to find a better solution to clearBtn click event. Previously, I used textChanged signal and check if data is empty to execute my code. Your code works well but I have changed it. Instead of qobject_cast I find only QAction.

            Code Example 1:

              QList<QAction*> actionList = myLineEdit->findChildren<QAction*>();
            
               for (QAction *clearBtnAction : actionList) {
                    if (clearBtnAction) {
                        connect(clearBtnAction, &QAction::triggered, this, [this]() {
                            qDebug() << "Clicked!";
                        });
                    }
               }
            

            My code works, but it is still a bit odd. Since, I have checked the actionList.count(); and it returns only 1 item in the list. Here is my second code example.

            Code Example 2:

            QList<QAction*> actionList = myLineEdit->findChildren<QAction*>();
            
            if (!actionList.isEmpty()) {
                connect(actionList.first(), &QAction::triggered, this, [this]() {
            	qDebug() << "Clicked!";
                });
            }
            

            So, in my second example, it only takes the 1st item from the list and no need to set for loop anymore. Happy coding!

            J.HilkJ 1 Reply Last reply
            2
            • Cobra91151C Cobra91151

              @J-Hilk

              Hello!

              I came across this thread to find a better solution to clearBtn click event. Previously, I used textChanged signal and check if data is empty to execute my code. Your code works well but I have changed it. Instead of qobject_cast I find only QAction.

              Code Example 1:

                QList<QAction*> actionList = myLineEdit->findChildren<QAction*>();
              
                 for (QAction *clearBtnAction : actionList) {
                      if (clearBtnAction) {
                          connect(clearBtnAction, &QAction::triggered, this, [this]() {
                              qDebug() << "Clicked!";
                          });
                      }
                 }
              

              My code works, but it is still a bit odd. Since, I have checked the actionList.count(); and it returns only 1 item in the list. Here is my second code example.

              Code Example 2:

              QList<QAction*> actionList = myLineEdit->findChildren<QAction*>();
              
              if (!actionList.isEmpty()) {
                  connect(actionList.first(), &QAction::triggered, this, [this]() {
              	qDebug() << "Clicked!";
                  });
              }
              

              So, in my second example, it only takes the 1st item from the list and no need to set for loop anymore. Happy coding!

              J.HilkJ Offline
              J.HilkJ Offline
              J.Hilk
              Moderators
              wrote on last edited by
              #6

              @Cobra91151 probably the better solution :D

              however, I would advice to firstly check if the returned list has an entry. If (for what ever reason) it doesn't, your program will crash on the call to first()


              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              Cobra91151C 1 Reply Last reply
              2
              • J.HilkJ J.Hilk

                @Cobra91151 probably the better solution :D

                however, I would advice to firstly check if the returned list has an entry. If (for what ever reason) it doesn't, your program will crash on the call to first()

                Cobra91151C Offline
                Cobra91151C Offline
                Cobra91151
                wrote on last edited by
                #7

                @J-Hilk

                Yes, you are right. I changed the Code Example 2. Now, it checks if the actionList has an entry. Thanks.

                1 Reply Last reply
                1

                • Login

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