2012-02-22 71 views
0

我在将值输入QLineEdit时遇到问题。这更符合逻辑问题。我的要求是,我想要限制用户输入60到150之间的值,包括该范围内的十进制值,即65.5或140.5等。如果输入的值超出此范围,我想向用户显示消息。在Qt中输入特定数据到QLineEdit时发生问题

我的方法是这样的:我在UI设计器中将此“QLineEdit”的“maxLength”属性设置为5,以便用户只能输入5个字符,如140.5。然后在“框TextChanged”插槽我写了下面的代码:

void MyWidget::on_lineEdit_textChanged(QString text) 
{ 
    if ((text.size() >= 3) && (text.toFloat() > 150 || text.toFloat() < 60)) 
    { 
     QMessageBox::information(this, "Info", "Range should be between 60 and 150.", QMessageBox::Ok); 
     ui->lineEdit->setText(""); 
    } 
    else 
    { 
     ui->lineEdit->setText(text); 
    } 
} 

还用下面的代码如验证:

QRegExp rx("^[-+]?[0-9]*\\.?[0-9]+$"); 
QValidator *validator = new QRegExpValidator(rx, this); 
ui->lineEdit->setValidator(validator); 

上面的代码工作正常。唯一的问题是,如果值先小于60,那么消息不会显示,因为我正在检查条件[B](text.size()> = 3)[/ B]。消息仅在text.size()> = 3时显示。我无法为此情况设置正确的逻辑。是否可以更改逻辑以显示用户满足的范围。 请让我知道。 谢谢。

+0

我会认为它非常破碎和令人讨厌的UI,如果消息框弹出在我的脸上,因为我键入。更好地显示警告标签和/或禁用确认按钮和/或使用QDoubleValidator防止无效输入。或者使用QDoubleSpinBox。 – 2012-02-22 18:19:44

回答

0
void number_test::Slot(const QString& str) 

{ 
QString sttt = str; 
bool bb = sttt.contains(QChar('.')); 
if(!bb) 
{ 
int nSize = sttt.size(); 
if(nSize == 1) 
{ 
if(sttt.toInt() < 6 && sttt.toInt() != 1) 
{ 
QMessageBox::information(this, "Info", "Range should be between 60 and 150.", QMessageBox::Ok); 
clear(); 
} 
} 
else if(nSize == 2) 
{ 
if(sttt.toFloat() < 60.0) 
{ 
QMessageBox::information(this, "Info", "Range should be between 60 and 150.", QMessageBox::Ok); 
clear(); 
} 
} 
else if(nSize >= 3) 
{ 
if(sttt.toFloat() > 150.0) 
{ 
QMessageBox::information(this, "Info", "Range should be between 60 and 150.", QMessageBox::Ok); 
clear(); 
} 
} 
} 
else 
{ 
int nSize = sttt.size(); 
if(nSize == 1) 
{ 
if(sttt.toInt() < 6 && sttt.toInt() != 1) 
{ 
QMessageBox::information(this, "Info", "Range should be between 60 and 150.", QMessageBox::Ok); 
clear(); 
} 
} 
else 
{ 
if(sttt.toFloat() > 150.0 || sttt.toFloat() < 60.0) 
{ 
QMessageBox::information(this, "Info", "Range should be between 60 and 150.", QMessageBox::Ok); 
clear(); 
} 
} 
} 
} 

请试试这些代码为您的问题。

+0

它不按照要求工作。当你只输入2位数时,它显示信息,即当你输入数字11时,显示信息,这是不期望的,因为用户可能试图输入数字112. – user1182217 2012-02-22 10:59:49

+0

如果你连接到textChanged信号,测试将每次发射用户改变文字。例如从'1'到'11'。如果您只想在用户完成输入文本时触发测试,则可以连接到textEdited信号。 – 2012-02-22 13:06:47

+0

它可以在我的电脑上工作。也许有些错误不会发生在我的测试用例中,我会修改它,谢谢! – 2012-02-22 13:43:56

0

这看起来像QDoubleValidator的情况下!

http://developer.qt.nokia.com/doc/qt-4.8/qdoublevalidator.html

QDoubleValidator *v = new QDoubleValidator(60.0,150.0); 
ui->lineEdit->setValidator(v); 

编辑:如果你想有消息框弹出无效文本,你可以使用你已经在使用槽内的验证。

void MyWidget::on_lineEdit_textChanged(QString text) 
{ 
     QDoubleValidator v(60.0,150.0); 
     if(v.validate(text) != QValidator::Acceptable) 
     { 
      QMessageBox::information(this, "Info", "Range should be between 60 and 150.", QMessageBox::Ok); 
     } 
} 

我还没有测试过这个,但我认为它应该或多或少的工作,因为OP也想要它。

0

已解决。我已经执行的代码如下:

void MyWidget::on_lineEdit_textChanged(QString text) 
{ 
    if(text.startsWith('.')) 
    { 
     QMessageBox::information(this, "Info", "Range should be between 60 and 150.", QMessageBox::Ok); 
     ui->lineEdit->setText(""); 
     ui->lineEdit->setFocus(); 
     return; 
    } 
    if(text.at(2) == '.') 
    { 
     QMessageBox::information(this, "Info", "Range should be between 60 and 150.", QMessageBox::Ok); 
     ui->lineEdit->setText(""); 
     ui->lineEdit->setFocus(); 
     return; 
    } 
    if ((text.size() >= 3) && (text.toFloat() > 150 || text.toFloat() < 60)) 
    { 
     QMessageBox::information(this, "Info", "Range should be between 60 and 150.", QMessageBox::Ok); 
     ui->lineEdit->setText(""); 
     ui->lineEdit->setFocus(); 
     return; 
    } 
    else 
    { 
     ui->lineEdit->setText(text); 
    } 
} 

void MyWidget::on_lineEdit_editingFinished() 
{ 
    QString text = ui->lineEdit->text(); 
    if (text.toFloat() < 60) 
    { 
     QMessageBox::information(this, "Info", "Range should be between 60 and 150.", QMessageBox::Ok); 
     ui->lineEdit->setText(""); 
     ui->lineEdit->setFocus(); 
    } 
    else 
    { 
     ui->lineEdit->setText(text); 
    } 
}