[Qt-interest] Subclassing QLineEdit
A. S. Budden
abudden at gmail.com
Thu Feb 18 17:01:20 CET 2010
Dear all,
I'm currently investigating creating a re-usable subclass of QLineEdit
to allow customisation of the way the characters are shown.
Unfortunately, the impression I get from reading the documentation is
that the validators and input masks are not able to do what I want.
What I'd like to do is have a function that takes whatever is entered
into the line edit (either programmatically or by the user) and mangle
the text to show it in a different form. I can do this fairly easily
by connecting a slot to the textChanged signal in the subclass.
However, if the user of the widget connects textEdited or textChanged
to their own handler (which is a reasonable thing for them to do), I
want the text that is passed as an argument to be the mangled version,
NOT the original text.
Is there any way to achieve this? My testing has shown that simply
connecting the textChanged slot in the constructor (so that the
connection is made before the user makes their textEdited connection)
doesn't work: the user's textEdited connection gets called with the
raw text. Example code for the class below.
Many thanks in advance for any help.
Al
// Header
#include <QtGui/QLineEdit>
class LineEditSubClass : public QLineEdit
{
Q_OBJECT
public:
LineEditSubClass(QWidget *parent = 0);
LineEditSubClass(const QString &contents, QWidget *parent = 0);
public slots:
void OnTextChanged(QString NewText);
private:
bool recursing;
void Init();
};
// Source
LineEditSubClass::LineEditSubClass(QWidget *parent)
: QLineEdit(parent)
{
Init();
}
LineEditSubClass::LineEditSubClass(const QString &contents, QWidget *parent)
: QLineEdit(contents, parent)
{
Init();
}
void LineEditSubClass::Init()
{
recursing = false;
connect(this, SIGNAL(textChanged(QString)),
this, SLOT(OnTextChanged(QString)));
}
void LineEditSubClass::OnTextChanged(QString NewText)
{
if (recursing)
{
return;
}
recursing = true;
QString Text = Mangle(NewText);
setText(Text);
recursing = false;
}
More information about the Qt-interest-old
mailing list