Search code examples
qtqlineedit

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..


Solution

  • 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 ) ) );