linux上qlineedit无法编辑,QLineEdit限制部分输入

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

f48fba12ce20c65dce030f59935c223f.png

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(indexsetReadOnly(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();

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值