我这里要显示页码,如 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(); }
文章评论