qlineedit with clearbutton only when mouse is over the QLineEdit

575 views Asked by At

I would like to have a QLineEdit that shows the clearbutton only when the mouse is over the QLineEdit (and of course the field is not empty). I've captured the enter- and leave- events which set the property respectively. This works fine with the exception that it needs an initial enter and leave of the QLineEdit manually with the mouse. How can I initiate the QLineEdit correctly, so that it works fine from the beginning? Trying to simulate the initial mouse movements did not have the expecting results.

cmplLineEdit.h

class cmplLineEdit : public QLineEdit {
    Q_OBJECT

public:
    explicit cmplLineEdit( QWidget* a_par = 0);
    ~cmplLineEdit();

private:
    void enterEvent( QEvent* a_ev);
    void leaveEvent( QEvent* a_ev);
    void enableClearButton( bool a_set, int a_del = 0);

private slots:
    void initialize( void);
};

cmplLineEdit.cpp

cmplLineEdit::cmplLineEdit( QWidget* a_par) : QLineEdit( a_par) {
    m_completeIt = a_cmpl;
    setClearButtonEnabled( false);
    setFocusPolicy( Qt::StrongFocus);
    QTimer::singleShot( 0, this, [=]( void) { initialize(); });
}

cmplLineEdit::~cmplLineEdit() {
}

bool cmplLineEdit::cursorIsInField() {
    return rect().contains(  mapFromGlobal( QCursor::pos()));
}

void cmplLineEdit::initialize( void) {
    QApplication::postEvent( this, new QEvent( ! cursorIsInField() ? QEvent::Enter : QEvent::Leave));
    QApplication::postEvent( this, new QEvent(   cursorIsInField() ? QEvent::Enter : QEvent::Leave));
}

void cmplLineEdit::enableClearButton( bool a_set, int a_del) {
    if( a_del < 0) {
      setClearButtonEnabled( a_set);
    } else
      QTimer::singleShot( a_del, this, [=]( void) { setClearButtonEnabled( a_set); });
}

void cmplLineEdit::enterEvent( QEvent* a_ev) {
    enableClearButton( true, 0);
}

void cmplLineEdit::leaveEvent( QEvent* a_ev) {
    enableClearButton( false, 0);
}
1

There are 1 answers

0
Mikel-T On

Yes, mouse tracking is on (otherwise I wouldn' get the enter- and leave-events). I rewrote the code, implementing setClearButtonEnabled() by myself. Now it's working. For anybody who is interested:

cmplEdit.h:

#ifndef CMPLLINEEDIT_H
#define CMPLLINEEDIT_H

#include <QWidget>

#include <QLineEdit>
#include <QCompleter>
#include <QAction>

class cmplLineEdit : public QLineEdit {
   Q_OBJECT

public:
   explicit cmplLineEdit( bool a_cmpl = true, QWidget* a_par = 0);
   ~cmplLineEdit();

   static QIcon m_icoOff;
   static QIcon m_icoOn;

private:
   QAction* m_act = nullptr;
   bool m_completeIt = true;
   void enterEvent( QEvent* a_ev);
   void leaveEvent( QEvent* a_ev);
   bool cursorIsInField( void);

private slots:
   void initialize( void);
   void setClearIcon( bool a_set);
   void setClearIcon( const QString& a_txt);
};

#endif // CMPLLINEEDIT_H

cmplEdit.cpp:

#include "cmplLineEdit.h"

#include <QTimer>
#include <QDebug>
#include <QStandardItemModel>
#include <QAbstractItemView>
#include <QEvent>
#include <QApplication>

QIcon cmplLineEdit::m_icoOn;
QIcon cmplLineEdit::m_icoOff;

cmplLineEdit::cmplLineEdit( bool a_cmpl, QWidget* a_par) : QLineEdit( a_par) {
   if( m_icoOn.isNull()) {
      m_icoOn = QIcon( qApp->style()->standardPixmap( QStyle::SP_LineEditClearButton));
   }

   m_completeIt = a_cmpl;
   m_act = addAction( m_icoOn, QLineEdit::ActionPosition::TrailingPosition);
   connect( this, SIGNAL( textChanged( QString)), this, SLOT( setClearIcon( QString)));
   connect( m_act, SIGNAL( triggered( bool)), this, SLOT( clear()));

   QTimer::singleShot( 0, [ this]( void) { initialize(); });
}

cmplLineEdit::~cmplLineEdit() {
}

bool cmplLineEdit::cursorIsInField() {
   return rect().contains( mapFromGlobal( QCursor::pos()));
}

void cmplLineEdit::initialize( void) {
   setClearIcon( cursorIsInField());
}

void cmplLineEdit::enterEvent( QEvent* a_ev) {
   setClearIcon( true);
}

void cmplLineEdit::leaveEvent( QEvent* a_ev) {
   setClearIcon( false);
}

void cmplLineEdit::setClearIcon( bool a_set) {
   if( m_act == nullptr)
      return;

   a_set = a_set && ! text().isEmpty();
   m_act->setIcon( a_set ? m_icoOn : QIcon());
   m_act->setVisible( a_set);
}

void cmplLineEdit::setClearIcon( const QString& a_txt) {
   setClearIcon( ! a_txt.isEmpty());
}