Solved 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?
-
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.
-
Hello!
I came across this thread to find a better solution to
clearBtn
click event. Previously, I usedtextChanged
signal and check if data is empty to execute my code. Your code works well but I have changed it. Instead ofqobject_cast
I find onlyQAction
.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! -
@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()
-
Yes, you are right. I changed the Code Example 2. Now, it checks if the
actionList
has an entry. Thanks.