c++ - qlineedit with clearbutton only when mouse is over the QLineEdit -


i have qlineedit shows clearbutton when mouse on qlineedit (and of course field not empty). i've captured enter- , leave- events set property respectively. works fine exception needs initial enter , leave of qlineedit manually mouse. how can initiate qlineedit correctly, works fine beginning? trying simulate initial mouse movemnts did not have 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); } 

yes, mouse tracking on (otherwise wouldn' enter- , leave-events). rewrote code, implementing setclearbuttonenabled() myself. it's working. 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()); } 

Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

firemonkey - How do I make a beep sound in Android using Delphi and the API? -