Anjali
Anjali

Reputation: 25

customizing QlineEdit in Qt

I am making a name field using QlineEdit. I want the entry in this field is entered such that the first character of every word is always uppercase. I don't know how to set the inputmask for that, could anyone please help me out.. thnx in advance..

Upvotes: 1

Views: 2950

Answers (3)

Exa
Exa

Reputation: 4110

That's just a quick solution which I came up with and there are better solutions of course (Implementing your own line edit for example), but this works as I tested.

This is a SLOT:

void main_window::on_line_edit_0_text_changed( QString text )
{
    QString tmp = text;

    tmp.truncate( 1 ); // tmp is now first char of your text
    tmp = tmp.toUpper();

    if( text.size() > 1 )
    {
        text.remove( 0, 1 );
        text = text.toLower();
        text.prepend( tmp );
        line_edit_0->setText( text );
    }
    else
    {
        line_edit_0->setText( tmp );
    }
}

The connect:

connect( line_edit_0, SIGNAL( textChanged( QString ) ), this, SLOT( on_line_edit_0_text_changed( QString ) ) );

Upvotes: 0

spraff
spraff

Reputation: 33385

You can subclass QLineEdit and override keyPressEvent. QValidator is primarily for forbidding bad inputs as opposed to generating good ones, but for this simple case, fixup will probably do.

Upvotes: 0

Donotalo
Donotalo

Reputation: 13025

I'm not sure about the inputMask, but you can do it by subclassing QValidator, or you can use QRegExpValidator.

Upvotes: 4

Related Questions