[Interest] Finish QLineEdit editing when user clicks anywhere outside the QLineEdit
henry at tungware.se
Tue Aug 13 20:58:32 CEST 2019
Hi, had a similar problem, couldn't find any other solution than using
an event filter, but it turned out being not so bad, made a small class:
class MouseFilter : public QObject
QWidget* pW; // ptr to widget to check for inside/outside mouse clicks
bool eventFilter(QObject* pObj, QEvent* pEvent)
if (QEvent::MouseButtonPress == pEvent->type())
QMouseEvent* pME = static_cast<QMouseEvent*>(pEvent);
bool bClickOutside = !r.contains(pME->globalPos());
qDebug << bClickOutside;
void setWidget(QWidget* pW)
this->pW = pW;
In my MainWindow I have an instance of that class, and in MainWindows's
ctor I initialize it:
On 2019-08-13 16:47, Murphy, Sean wrote:
> I'm trying to create a custom QLineEdit where while editing, the user can click anywhere *outside* the QLineEdit to finish editing.
> The default QLineEdit behavior appears to be that once the user has clicked inside the QLineEdit and begins editing the text, if the user then clicks on some other widget that *will* accept focus via a mouse click, the editing finished signal is emitted and the cursor/focus moves to whatever they clicked on. What I'm struggling with is that if the user clicks on something that *doesn't* normally accept focus via a mouse click, like any whitespace in my app, the cursor stays within the QLineEdit and editing is still active.
> I thought maybe I could use focusOutEvent(), but in the case where the user clicks on whitespace, the QLineEdit doesn't receive a focusOutEvent(). So that doesn't appear to be useful, unless I'm missing something.
> Ideally I want a solution that is completely contained within my custom QLineEdit-inherited class - with all the other widgets unaware that this behavior is happening. The only solution I'm finding via Google is doing things like installing event filters or setting the focus policy on other widgets to then force the QLineEdit to give up focus or end editing.
More information about the Interest