Menu

Main Menu
Talk Get Daily Search

Member's Online

    User Name
    Password

    QT - My way to force numeric input

    Reply
    shep | # 1 | 2010-02-15, 22:15 | Report

    This is probably lame, but I thought I'd share my method for making a lineEdit into a numeric only input.

    Code:
    void MainWindow::on_lineEdit_textChanged(QString )
    {
        QString qNewValue = ui->lineEdit->text();
        bool ok;
        double dTest = qNewValue.toDouble(&ok);
        int dotCnt=0;
        if (!ok)
        {
            QString qStripped;
    
            for (int i=0; i< qNewValue.length(); i++ )
            {
                QString subStr = qNewValue.at(i);
                QString sValid = "1234567890.";
                if (subStr.toLower() == ":")
                {
                    subStr = ".";
                }
                if (subStr.toLower() == "?")
                {
                    subStr = ".";
                }
    
                if (subStr==".")
                {
                    dotCnt++;
                }
                if (subStr.toLower() == "q")
                {
                    subStr = "1";
                }
                if (subStr.toLower() == "w")
                {
                    subStr = "2";
                }
                if (subStr.toLower() == "e")
                {
                    subStr = "3";
                }
                if (subStr.toLower() == "r")
                {
                    subStr = "4";
                }
                if (subStr.toLower() == "t")
                {
                    subStr = "5";
                }
                if (subStr.toLower() == "y")
                {
                    subStr = "6";
                }
                if (subStr.toLower() == "u")
                {
                    subStr = "7";
                }
                if (subStr.toLower() == "i")
                {
                    subStr = "8";
                }
                if (subStr.toLower() == "o")
                {
                    subStr = "9";
                }
                if (subStr.toLower() == "p")
                {
                    subStr = "0";
                }
                if ( (sValid.contains( subStr)) && (dotCnt <= 1))
                {
                    qStripped.append(subStr);
                }
            }
            ui->lineEdit->setText(qStripped);
        }
    
    }
    Hopes this helps someone.

    Edit | Forward | Quote | Quick Reply | Thanks

    Last edited by shep; 2010-02-15 at 22:21.
    The Following User Says Thank You to shep For This Useful Post:
    qwerty12

     
    b666m | # 2 | 2010-02-15, 22:17 | Report

    set your code into the code-tag [.code] [./code] (of course without the . )

    Edit | Forward | Quote | Quick Reply | Thanks
    The Following User Says Thank You to b666m For This Useful Post:
    shep

     
    shep | # 3 | 2010-02-15, 22:21 | Report

    Originally Posted by b666m View Post
    set your code into the code-tag [.code] [./code] (of course without the . )
    Done, thanks!

    Edit | Forward | Quote | Quick Reply | Thanks

     
    qwerty12 | # 4 | 2010-02-15, 22:30 | Report

    What about ze Germans with their QWERTZ keyboards? (Being qwerty12, I have to boo at the Germans for using the QWERTZ layout on their keyboards anyway, but... )

    Edit | Forward | Quote | Quick Reply | Thanks

    Last edited by qwerty12; 2010-02-15 at 22:38.
    The Following User Says Thank You to qwerty12 For This Useful Post:
    shep

     
    w00t | # 5 | 2010-02-15, 22:31 | Report

    I'd suggest looking at:

    http://doc.trolltech.com/4.6/qwidget...thodHints-prop

    and

    http://doc.trolltech.com/4.6/qlineed...inputMask-prop

    and

    http://doc.trolltech.com/4.6/qlineed...l#setValidator

    Hope this helps.

    Edit | Forward | Quote | Quick Reply | Thanks
    The Following 2 Users Say Thank You to w00t For This Useful Post:
    krk969, shep

     
    shep | # 6 | 2010-02-15, 22:40 | Report

    Originally Posted by qwerty12 View Post
    What about ze Germans with their QWERTZ keyboards? (Being qwerty12, I have to boo at the Germans for using the QWERTZ layout on their keyboards anyway, but... )
    I did say it sucked

    If you forget about the qwertyuiop to 1234567890 translation then the basic principle is sound! Just did the translation for lazy old me.

    Shep

    Edit | Forward | Quote | Quick Reply | Thanks

     
    shep | # 7 | 2010-02-15, 22:42 | Report

    Originally Posted by w00t View Post
    I'd suggest looking at...
    Thanks woot, unfortunately the inputHints don't work using Madde at the moment that's why I went looking for an alt in the first place.

    Thanks for the tips though

    Edit | Forward | Quote | Quick Reply | Thanks

     
    w00t | # 8 | 2010-02-15, 23:46 | Report

    Originally Posted by shep View Post
    Thanks woot, unfortunately the inputHints don't work using Madde at the moment that's why I went looking for an alt in the first place.

    Thanks for the tips though
    Ah. Yeah, they're new to Qt 4.6, and MADDE has 4.5 at the moment... *but*, input hints are only for the virtual keyboard. The actual restriction of input can be done through validators

    Edit | Forward | Quote | Quick Reply | Thanks

     
vBulletin® Version 3.8.8
Normal Logout