开发笔记

  • 首页
  • 工具箱
三味线的博客
  1. 首页
  2. Qt
  3. 正文

QLineEdit限制部分输入

2019-03-18 1515点热度 1人点赞 0条评论

我这里要显示页码,如 1/10,只能修改 "/" 前面的,而总页数不能修改。

1. 设置正则表达式

QRegExp rx("(^[0-9]{0,3}/[0-9]{0,3}$)");
QRegExpValidator *reg=new QRegExpValidator(rx);
m_EdtCurPage->setValidator(reg);

2. 限制输入

connect(m_EdtCurPage,SIGNAL(cursorPositionChanged(int,int)),this,SLOT(cursorChangedSlot(int,int)));
void MainWindow::cursorChangedSlot(int oldPos,int newPos)
{
    Q_UNUSED(oldPos)
    QString nowValue=m_EdtCurPage->text();
    int index=nowValue.indexOf("/");
    if(index<newPos) m_EdtCurPage->setReadOnly(true);
    else m_EdtCurPage->setReadOnly(false);
    int page=nowValue.split("/").first().toInt();
    if(page>m_TotalPages) m_EdtCurPage->backspace();
}

3. 响应修改

connect(m_EdtCurPage,SIGNAL(returnPressed()),this,SLOT(edtCurPageSlot()));
connect(m_EdtCurPage,SIGNAL(editingFinished()),this,SLOT(edtCurPageSlot()));
void MainWindow::edtCurPageSlot()
{
    QString nowValue=m_EdtCurPage->text();
    int page=nowValue.split("/").first().toInt();
    if(page>0&&page<=m_TotalPages) m_CurPage=page;
    actRefSlot();
}
标签: Qt
最后更新:2020-06-06

三味线

不吃咸鱼的喵

点赞
< 上一篇
下一篇 >

文章评论

razz evil exclaim smile redface biggrin eek confused idea lol mad twisted rolleyes wink cool arrow neutral cry mrgreen drooling persevering
取消回复

Captcha Code

COPYRIGHT © 2022 voidcat.cn. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang

蜀ICP备18010095号-1