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:

#include "qevent.h"

class MouseFilter : public QObject
{
    Q_OBJECT

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

            QRect r(pW->mapToGlobal(QPoint(0,0)),pW->mapToGlobal(QPoint(pW->width(),pW->height())));
            bool bClickOutside = !r.contains(pME->globalPos());
            qDebug << bClickOutside;
        }

        return QObject::eventFilter(pObj,pEvent);
    }

public:
    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:

MouseFilter mf;
...
mf.setWidget(ui->lineEdit);
qApp->installEventFilter(&mf);
...


Rgrds Henry


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.

Sean


_______________________________________________
Interest mailing list
Interest@qt-project.org
https://lists.qt-project.org/listinfo/interest

Reply via email to