1

我正在使用 QlineEdit 创建一个名称字段。我希望输入此字段中的条目,以便每个单词的第一个字符始终为大写。我不知道如何为此设置输入掩码,任何人都可以帮助我.. thnx提前..

4

3 回答 3

4

我不确定inputMask,但你可以通过继承 QValidator来做到这一点,或者你可以使用QRegExpValidator

于 2011-07-05T10:40:58.000 回答
0

您可以继承 QLineEdit 并覆盖keyPressEvent。QValidator 主要用于禁止错误输入而不是生成好的输入,但对于这种简单的情况,fixup可能会这样做。

于 2011-07-05T13:20:23.600 回答
0

这只是我想出的一个快速解决方案,当然还有更好的解决方案(例如实现您自己的行编辑),但这在我测试时有效。

这是一个插槽

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

连接:_

connect( line_edit_0, SIGNAL( textChanged( QString ) ), this, SLOT( on_line_edit_0_text_changed( QString ) ) );
于 2011-07-06T13:29:15.790 回答