×

Discussion Board

Results 1 to 3 of 3
  1. #1
    Regular Contributor
    Join Date
    Aug 2009
    Posts
    331

    Problem entering data to QLineEdit.

    Hi there,

    I am having an issue with entering values into QLineEdit. This is more of a logical issue. My requirement is that I want to restrict user to enter values between 60 to 150, that includes decimal values within this range i.e. 65.5 or 140.5 etc. I want to display a message to the user if he enters values outside this range.

    My approach for this is like: I set the "maxLength" property of this "QLineEdit" to 5 in the ui designer, so that user can can enter only 5 characters like 140.5. Then on the "textChanged" slot I have written the following code:

    Code:
    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);
        }
    }
    Also used the following code for validation like:

    QRegExp rx("^[-+]?[0-9]*\\.?[0-9]+$");
    QValidator *validator = new QRegExpValidator(rx, this);
    ui->lineEdit->setValidator(validator);
    The above code works fine. The only problem is that message is not displayed if value is less that 60 on the first go, as I am checking the condition (text.size() >= 3). Message is only displayed if text.size() >= 3. I am not able to put the proper logic for this case. Is it possible to change the logic to display the user for the range to be satisfied.
    Please let me know.
    Thanks.
    Last edited by RajeevSahu; 2012-02-22 at 09:18.

  2. #2
    Registered User
    Join Date
    Sep 2011
    Posts
    449

    Re: Problem entering data to QLineEdit.

    Hi,

    You can use a doublespinbox for the same requirement.

    Code:
     spinbox.setMinimum(60);
     spinbox.setMaximum(150);
     spinbox.setSingleStep(0.1);
    http://doc.qt.nokia.com/4.7-snapshot...lespinbox.html

    user should be able to manually enter too as per documentation.
    You can use designer to set all the properties.

  3. #3
    Regular Contributor
    Join Date
    Aug 2009
    Posts
    331

    Re: Problem entering data to QLineEdit.

    This is resolved. I have implemented the code as follows:

    void MyWidget:n_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:n_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);
    }
    }
    Thanks...

Similar Threads

  1. App for turning a smartphone touchscreen into a touchpad for entering mouse data
    By Anbalagan in forum [Closed] Publishing to Nokia Store
    Replies: 7
    Last Post: 2011-07-13, 09:58

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •