[Qt-interest] QLineEdit - textChanged signal - determine old value

Colin S. Miller no-spam-thank-you at csmiller.demon.co.uk
Mon Sep 14 16:25:49 CEST 2009


R. Reucher wrote:
> On Mon, 14 Sep 2009 13:39:25 +0100, Colin S. Miller wrote:
So, something like this? This class also emit when it gains/loses focus.
All comments on the code are welcome.

class EnhancedLineEdit : public QLineEdit
{
   Q_OBJECT

private:
   QString oldValue;

protected:
  virtual bool event   (QEvent * e);       /* overrides parent */
  virtual void setText (const QString &n); /* overrides parent */

public:
     EnhancedLineEdit(QWidget *parent); /* and the rest of the constructors */

   enum FocusState
   {
       Lost = 0,
       Gained
   };

signals:
     /** signal sent when this item gains/loses focus. slot receives the new
         focus state */
     void focusChanged(EnhancedLineEdit::FocusState);
     void textChangedWithOld(const QString&n, const QString&old);

private slots:
      void selfChanged(const QString&n);
};


EnhancedLineEdit::EnhancedLineEdit(QWidget *parent)
     QLineEdit(parent), oldValue("")
{
     connect(this,
             SIGNAL(textChanged(const QString&)),
             SLOT(selfChanged(const QString&)));
};

bool EnhancedLineEdit::event(QEvent *evt)
{
     bool res;

     /*
      * We don't want to modify our base's behaviour, only catch
      * when our focus changes, so give the event straight to our parent
      */
     res = QLineEdit::event(evt);

     if (evt->type() == QEvent::FocusIn)
         emit focusChanged(Gained);
     else if (evt->type() == QEvent::FocusOut)
         emit focusChanged(Lost);

     return res;
}

void EnhancedLineEdit::selfChanged(const QString&n)
{
     emit textChangedWithOld(n, oldValue);
     oldValue=n;
}

void EnhancedLineEdit::setText(const QString &n)
{
     oldValue = n;
     QLineEdit::setText(n);
}








More information about the Qt-interest-old mailing list