Reply
Thread Tools
shep's Avatar
Posts: 85 | Thanked: 65 times | Joined on Jan 2010 @ Ireland
#1
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.

Last edited by shep; 2010-02-15 at 22:21.
 

The Following User Says Thank You to shep For This Useful Post:
b666m's Avatar
Posts: 1,090 | Thanked: 476 times | Joined on Jan 2010 @ Ingolstadt, Germany
#2
set your code into the code-tag [.code] [./code] (of course without the . )
 

The Following User Says Thank You to b666m For This Useful Post:
shep's Avatar
Posts: 85 | Thanked: 65 times | Joined on Jan 2010 @ Ireland
#3
Originally Posted by b666m View Post
set your code into the code-tag [.code] [./code] (of course without the . )
Done, thanks!
 
qwerty12's Avatar
Posts: 4,274 | Thanked: 5,358 times | Joined on Sep 2007 @ Looking at y'all and sighing
#4
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... )

Last edited by qwerty12; 2010-02-15 at 22:38.
 

The Following User Says Thank You to qwerty12 For This Useful Post:
w00t's Avatar
Posts: 1,055 | Thanked: 4,107 times | Joined on Oct 2009 @ Norway
#5
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.
__________________
i'm a Qt expert and former Jolla sailor (forever sailing, in spirit).
if you like, read more about me.
if you find me entertaining, or useful, thank me. if you don't, then tell me why.
 

The Following 2 Users Say Thank You to w00t For This Useful Post:
shep's Avatar
Posts: 85 | Thanked: 65 times | Joined on Jan 2010 @ Ireland
#6
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
 
shep's Avatar
Posts: 85 | Thanked: 65 times | Joined on Jan 2010 @ Ireland
#7
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
 
w00t's Avatar
Posts: 1,055 | Thanked: 4,107 times | Joined on Oct 2009 @ Norway
#8
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
__________________
i'm a Qt expert and former Jolla sailor (forever sailing, in spirit).
if you like, read more about me.
if you find me entertaining, or useful, thank me. if you don't, then tell me why.
 
Reply


 
Forum Jump


All times are GMT. The time now is 16:18.