加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

QLineEdit 设置最大输入字符个数

(2012-06-28 16:10:04)
标签:

qlineedit

最大

字符

个数

it

分类: Qt
QLineEdit 没有提供限制字符最大输入个数的接口,所以如果需要的话,需要自己实现,以下是实现代码(以QLineEdit继承类中实现为例):

首先要关联信号:
connect(this, SIGNAL(textChanged()), this, SLOT(CheckInputText()));
    即当有字符输入的时候用槽函数CheckInputText()来核查是否超过了最大个数,如果超出了,则删掉刚刚输入的字符。

其次是实现槽函数:
void CheckInputText()
{
    QString currText = this->text();
    int currCount = currText.count();

    if(currCount > MAXINPUTSIZE)
    {
        int position = this->textCursor().position();
        QTextCursor textCursor = this->textCursor(); //  此处需加this
        currText.remove(position-(currCount-MAXINPUTSIZE), currCount-MAXINPUTSIZE);
        this->setText(currText );
        textCursor.setPosition(position-(currCount-MAXINPUTSIZE));
        this->setTextCursor(textCursor);
    }
}

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有