[Qt-interest] Subclassing QLineEdit

Alexandre Beraud aberaud at infflux.com
Thu Feb 18 17:52:11 CET 2010


Hi,

Imho it would be better to subclass QValidator and set an instance of 
your validator to the QLineEdit through QLineEdit::setValidator(const 
QValidator *). Then reimplement the validate() method of QValidator. 
Since it takes as argument a reference to the string, you can modify 
this string inside this method and at the same time check whether the 
input string is correct or not.

Regards,

Alex


A. S. Budden a écrit :
> 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;
> }
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest
>
>   


-- 
Alexandre BERAUD
Ingénieur Développement

Infflux - Informatique & Flux
Tel: 01 49 57 92 00 - Fax : 01 49 57 92 01
Mail: aberaud at infflux.com
Visitez notre site :  www.infflux.com




More information about the Qt-interest-old mailing list