PDA

View Full Version : Display only numbers at Last



StarRocks
7th August 2012, 14:11
Dear Forums,


I have small doubt in QT..... Guys i have Vehicle number as INAP1212 now what i need is each and everytime the last four should always be digits and should not be 0000.............I need a validation for it.........Can anyone make it out for me...........Any solution would be appreciable....


Regards,

Venugopal.T

yeye_olive
7th August 2012, 15:55
Dear Forums,


I have small doubt in QT..... Guys i have Vehicle number as INAP1212 now what i need is each and everytime the last four should always be digits and should not be 0000.............I need a validation for it.........Can anyone make it out for me...........Any solution would be appreciable....


Regards,

Venugopal.T

I am sorry if it sounds condescending, but you need to understand how this forum works. You have been opening several new topics recently, in which you always ask someone to solve a problem for you, without showing that you actually tried anything on your own. Guess what: the people who help each other here do so for free, in their free time. They will only help you solve your problem, not do it for you. If you are looking for someone to do your work for you, just hire some seasoned Qt developer.

I suggest you try harder, explain what you have tried; I am confident that someone will then help you.

d_stranz
7th August 2012, 19:12
I have small doubt in QT..... Guys i have Vehicle number as INAP1212 now what i need is each and everytime the last four should always be digits and should not be 0000.............I need a validation for it.........Can anyone make it out for me...........Any solution would be appreciable....


You don't explain anything about how or where you want to do this validation. So, if you are trying to validate input into a QLineEdit widget, then I suggest you read about QValidator, QRegExp, and QRegExpValidator. Those classes will allow you to write a validator for the problem you have described.

And like yeye_olive says, don't simply reply with "OK, give me example code for that". Try it on your own, and if you still can't solve it, post your code and ask what you are doing wrong.

StarRocks
8th August 2012, 07:50
Hi Guys,

Thanks for your feedback.........Listen i have posted few queries which i solved by my own noone gave any solutions.......First have a look at my queries im posting queries which i tried a lot and only then im posting to my knowledge that queries are not that easy to solve i guess.........Im not jus posting without trying sorry if i hurt u guys.........

Regards,

d_stranz
8th August 2012, 17:12
I have small doubt in QT..... Guys i have Vehicle number as INAP1212 now what i need is each and everytime the last four should always be digits and should not be 0000.............I need a validation for it.........Can anyone make it out for me...........Any solution would be appreciable....


@StarRocks: Look at what you said here. You did not say anything at all about having tried something. You didn't post any code and ask "Why doesn't this work"? You just said "Here's my problem. Tell me the answer".

So when you post something like that, what are we supposed to think? Next time, tell us what you've tried and what didn't work so we can at least tell that you aren't just begging for free answers. You will be much more likely to get some help instead of abuse in that case.

StarRocks
9th August 2012, 06:18
Dear d_stranz,

Its not like im begging for answers and all..............Please understand that first and i have written a code in Javascript and its working for me but i cant implement the same for C++ right....
As im new to Qt so i want to know how to use that coz its only validation prob ...........For ur reference the below is the code...






function ValidateVehicleNumber(strVarInstance) {
var strResult = "";
var cnt = 0;
var count1 = 0;
if (strVarInstance[1] == '') {
cnt++;
strResult += "";
}
else {
var VehicleNo = strVarInstance[1];

if (VehicleNo.length < 4) {
cnt++;

strResult += "Vehicle Number contains Atleast four digits \n ";
}
else {
var index = VehicleNo.length - 4
var count = 0;
for (i = index; i < VehicleNo.length; i++)
{
var c = VehicleNo.charAt(i);

if (isNaN(c) || c == " " )
{
count++;
}
if (c == 0) {
count1++;
}
}
if (count != 0)
{
cnt++;

strResult += "Last four character of Vehicle Number digits only \n ";
}
if (count1 == 4) {
cnt++;

strResult += "Vehicle Number should not be zeros \n ";

}
}
}
return strResult;
}


Regards

yeye_olive
9th August 2012, 10:50
In order to port this code as-is to Qt you need to do the following (in this order):
1. learn C++;
2. learn some Qt basics;
3. have a look at the documentation for the QString and QChar classes, that you will use to represent strings and characters respectively. All the operations you need are there.

However you can probably replace all this code with a simple regular expression. Once you have done steps 1 and 2 above, you can have a look at QRegExp, which d_stranz mentioned above. The documentation of the class explains the syntax of regular expressions and gives some examples (http://qt-project.org/doc/qt-4.8/qregexp.html#code-examples) that among other things show how to match digits in a string.

StarRocks
9th August 2012, 11:09
Dear Forums,


Thanks for the reply .......Will go through the things that you asked me to if any doubts will contact you..........


Regards,

StarRocks
21st January 2013, 09:39
Dear Forums,

As i have posted previously too i have google searched validations for everything i have got most of them but except the one im searching for i.e having numbers at the last i have tried but achieved only in having something like this as "ap12" but i have to get only numbers after that....Hope you got me......example is IN12AP1212.........Thanks in Advance.......Any solution would be appreciable.....


Regards,

Lykurg
21st January 2013, 10:13
Have you tried to check the strings with a regular expression? See QRegularExpression.

StarRocks
21st January 2013, 10:38
Dear Lykurg,

Thanks for the reply....I have gone through the QRegularExpression but dint find any solution through that so posted in the forums again......Any solution would be appreciable.......

Regards,

wysota
21st January 2013, 10:57
Open the QRegularExpression documentation again and instead of looking for a solution there, read the documentation with understanding.

StarRocks
22nd January 2013, 05:08
Dear Wysota,

Thanks for the reply........I have read the documentation i have used most of the syntax in my application but not finding the required....Its not that i have only looked for the solution have even read the docs........Any solution would be appreciable......Thanks in Advance........


Regards,

ChrisW67
22nd January 2013, 06:29
Show us what you have tried to do with those classes rather than just telling us that you have "used most of the syntax in my application" or that you "have even read the docs". We cannot fix or guide based on what we cannot see.

This is how you show us. Copy the following two lines into a reply:




Then, between the two tags, paste your best attempt at coding the function to validate the input and press Submit Reply.

The solution can be had using only the classes pointed out in this thread. I estimate an equivalent of your script routine consists of less than twenty lines. That depends a little on exactly what "strVarInstance" represents because the script code is, well, odd.

StarRocks
22nd January 2013, 06:43
Dear Chris,


Thanks for the reply......Please find the below code about how i used one of the syntax for my application.........




QRegExp rxlen("(\\d+)(?:\\s*)([0-9]|[A-Z])");
int pos = rxlen.indexIn("Length: 1212AP");

if (pos > -1) {
QString value = rxlen.cap(1); // "189"
QString unit = rxlen.cap(2); // "cm"

QString uts = rxlen.cap(3);

qDebug()<<value<<"The Value is";
qDebug()<<unit<<"The Unit is";
qDebug()<<uts<<"The Uts is";




Thanks in Advance.....Any solution would be appreciable........



Regards,

wysota
22nd January 2013, 11:10
Your expression reads: one or more digits followed by a 0 or more white spaces followed by a digit or capital letter. There is no cap(3) here. Did you really think this expression would match a string such as "INAP1212"?


Guys i have Vehicle number as INAP1212 now what i need is each and everytime the last four should always be digits and should not be 0000

StarRocks
22nd January 2013, 11:31
Dear Wysota,


Thanks for the reply......Actually in my code i have commented that line but forgot to do the same in the coding that i have posted........Hope you understood the situation that i have posted.........Any solution would be appreciable.......Thanks in Advance...Actually i am not exactly knowing how to have syntax in that format...


Regards,

wysota
22nd January 2013, 11:44
So what expression did you code if it wasn't the one you posted?

ChrisW67
22nd January 2013, 21:35
Concentrate on getting a regular expression that matches 4 or more digits. Once you have that extend it by forcing the match to only occur at the end of the string and you will be most of the way there. You may find an online regular expression tester (http://regexpal.com/) a useful tool to help learn in conjunction with the Qt docs.

wysota
22nd January 2013, 22:23
This one is also ok: http://www.regexper.com/

StarRocks
23rd January 2013, 11:51
Dear Wysota,


Thanks for the reply.......Actually i have done the validation something like this.........Just look the code phrased below




QRegExp rx("([A-Za-z][A-Za-z][0-9][0-9][A-Za-z][A-Za-z][0-9]+)/([A-Za-z]+)");
QValidator *validator = new QRegExpValidator(rx, this);
ui->lineEditVehicleNumber->setValidator(validator);
qDebug()<<"The Vehicle number is"<<rx;





But actually i am getting the format which i wanted to have numbers at last but im not satisfied with the coding that i have done......Can anyone remodify it......Thanks in Advance......Any solution would be appreciable.


Regards,

wysota
23rd January 2013, 11:55
The expression says: "Two letter upper or lower case followed by two digits followed by two letters and at least one digit then the '/' character and any number of lower or upper case letters". Is that what you wanted?

StarRocks
23rd January 2013, 12:36
Dear Wysota,


Thanks for the reply......Actually i have written the code in the right way what format i need is "AP12QW1234" the last four what ever we are giving should be numbers.......Thanks in advance.........Any solution would be appreciable.......


Regards,

wysota
23rd January 2013, 12:43
The expression you posted will not match this string. It doesn't have a '/' character anywhere nor does it end with letters. Did you come up with this expression on your own?

StarRocks
23rd January 2013, 12:54
Dear Wysota,


Thanks for the reply......yep i have done the validation by myself......Thanks in advance.........Any solution would be appreciable.......


Regards,

Lykurg
23rd January 2013, 13:07
...and you want us to do what? I must admit I lost your question. You simply what to match AP12QW1234? Then you almost there. Alter the expression, use the web sites mentioned and all is good. If you have precise questions ask them, but
.........Any solution would be appreciable.......wont help you. We will surely do not post c&p code for you.

To be not cruel a solution can be:
QRegExp rx("([A][P][1][2][Q][W][1][2][3][4])");
QValidator *validator = new QRegExpValidator(rx, this);
ui->lineEditVehicleNumber->setValidator(validator);

But real, what is your problem right now? QRegExp almost fine, only need some fine tuning.

wysota
23rd January 2013, 13:14
The solution can also be:


QRegExp rx("AP12QW1234");

:)

@StarRocks:

This: [A-Z] matches an uppercase letter,
this: [0-9] matches any single decimal digit.

To match a particular pattern more than once, repeat it an appropriate number of times, e.g. for matching two uppercase letters you'd use "[A-Z][A-Z]". To match three digits and two letters it would be "[0-9][0-9][0-9][A-Z][A-Z]".

Now combine those to create a matching pattern for your case. You don't need any parenthesis or pluses or any other extra characters you don't understand and apparently are not willing to understand, just what I have just posted.

StarRocks
23rd January 2013, 13:29
Dear Lykurg and Wysote,

Thanks for the reply......Actually don't you think this look very odd "QRegExp rx("([A][P][1][2][Q][W][1][2][3][4])");" or repeating of the same things in the validation...So i thought u would be giving me an idea about how to do not you to do.........Thanks in Advance.........


Regards,

wysota
23rd January 2013, 13:32
First get something that works, then think if it looks odd or not. If you want to match only one string then either compare the input to a string (e.g. if(myText == "APblahblahblah") { ... }) or provide a checkbox for the user instead of the text field to let him mark if he wants to use this particular string or not.

StarRocks
24th January 2013, 04:48
Dear Wysota,

Thanks for the reply......Actually the Vehicle numbers can't be same right it changes according to states so it can't be given a single string,the validation what i have written is working perfectly i just want to know repeating of the same code is a good pro-grammatical manner no i guess..So i thought we would have any other option to do this kind of validation.........Thanks in Advance......


Regards,

wysota
24th January 2013, 06:24
So what is the final expression you came up with?

ChrisW67
24th January 2013, 06:25
The "validation" you have given us does nothing like what your originally asked for. You started with "INAP1212" and needed to check that there were 4 digits at the right and they were not "0000", then you gave us another example "IN12AP1212" which at least made sense with respect the the earlier requirement. When you gave us your first attempt at coding a validator you had an expression that matched nothing like your example and you were matching against a string containing other random text i.e. "Length: 1212AP". You then "refined" that to an expression that matched things like "Aq34Qr9/junK" or "xX22Xx123456/purpleMonkeyDishwasher" and claimed to be looking to match "AP12QW1234". That these expressions did not match your proposed input strings would have been obvious without coding anything if you used the online tools Wysota and I pointed out. It seems that ultimately your "validation" could be some form of extracting things that could licence plate numbers from within arbitrary text.. but then again maybe not.

Can you perhaps understand why we have no real idea what you want and little faith that you do either?

If each of the distinct licence plate patterns can be matched with a regular expression then you can simply store QRegExps in a QMap or QList and use one routine to apply the appropriate regular expression (or all of them). This might be as simple as one expression per state, or it might not, we do not know. If the patterns cannot be matched by a regular expression (it is possible) then you may need a list of validator functions/function objects each doing a complex check.

nish
24th January 2013, 10:45
Dear StarRocks,

Topics like these really sinks Indian reputation around the web.
All though senior people here will not appreciate my post but I think OP is not going to get the RegExp thing correctly implemented.

I will take the problem in the first post.

This should be enough for most Strings. You do need some extra checks for length.


QString str = "IN12AP1212";

bool ok = false;
int num = str.right(4).toInt(&ok);

if( !ok || !num )
{
qDebug() << "Invalid";
}
else
{
qDebug() << "Valid!!" << num;
}

ChrisW67
24th January 2013, 20:38
All though senior people here will not appreciate my post but I think OP is not going to get the RegExp thing correctly implemented.
I think people around here, including you, have spent more than enough time on this. Perhaps we are naive to expect that people can define the problem and want to understand why a solution they arrive at works.

nish
25th January 2013, 05:14
Perhaps we are naive to expect that people can define the problem and want to understand why a solution they arrive at works.

The problem is, that programming is a natural skill. You can get better by practice but what i found is that if you dont know it.. you never will. Its just like i cant become a good cricketer no matter how hard i try.

StarRocks
31st January 2013, 11:12
Dear Forums,

Thanks for the reply.....Actually i have coded in Java for the numbers to display at last for sure................Please find the code for your reference......






public class test {
public static void main(String[] args) {
String s = "abcd1000231231232131212";
System.out.println(check(s));
}


public static boolean check(String s ) {

if(s.length()>=4){
String s1 = s.substring(s.length()-4, s.length());
System.out.println(s1);
try {
Integer.parseInt(s1);

if(Integer.parseInt(s1) == 0){
return false;
}
return true;
} catch (NumberFormatException e) {
return false;
}

}
return false;
}

}




Please provide me an idea how can it be done in button Event.......Hope you got an idea looking at this.......Any solution would be appreciable.......Thanks in Advance......


Regards

Santosh Reddy
31st January 2013, 12:08
Well this will be the ported C++ code. Note that exception handling in not required in this case as nothing in this code will raise an exception.



#include <iostream>
#include <QString>

using namespace std;

bool check(QString s)
{
if(s.length() >= 4)
{
QString s1 = s.mid(s.length()-4, s.length());
cout << s1.toStdString().c_str() << endl;

bool ok = false;
qulonglong Integer = s1.toULongLong(&ok);

if(ok)
{
return true;
}
else
{
return false;
}
}
return false;
}

int main(int argc, char *argv[])
{
QString s = "abcd1000231231232131212";
cout << check(s) << endl;
}

StarRocks
31st January 2013, 14:23
Dear Santosh,


Thanks for the reply......Small information that i need it from you is i want the same in the button event if it contains numbers then shld be in the if else format.....Bit confused with the code can you let me know in if-else format ..........Any Solution would be appreciable........Thanks in Advance......


Regards,

Santosh Reddy
31st January 2013, 14:57
Small information that i need it from you is i want the same in the button event ...
I guess you ment button click signal


...if it contains numbers then shld be in the if else format...
What is "it"?


...Bit confused with the code ...
Please show some code which you are confused with.


...can you let me know in if-else format ...
I already gave an example in earlier post.


I suggest start a different thread, as the question is un-related to this threads topic

StarRocks
1st February 2013, 04:53
Dear Santosh,

Thanks for the reply.......This is my .cpp code please have a look for your reference..........




#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QSqlDatabase>
#include <stdio.h>
#include <QTextStream>
#include <QMessageBox>
#include <QSqlRelationalTableModel>
#include <QSqlQuery>
#include <entry.h>
#include <QRegExpValidator>
#include <QLineEdit>
#include <QValidator>
#include <QComboBox>
#include <qdebug.h>
#include <QRegExp>
#include <QValidator>
#include <QRegExpValidator>
#include <QPixmap>
#include <QPalette>
#include <QProcess>
#include <QFile>

#include <Q_INT32>

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);

ui->lineEditpassword->setEchoMode(QLineEdit::Password);
ui->lineEditUsername->setMaxLength(15);
ui->lineEditpassword->setMaxLength(15);

QPixmap bg(":/images/G2G.jpg");
QPalette p(palette());
p.setBrush(QPalette::Background, bg);
setAutoFillBackground(true);
setPalette(p);

setWindowFlags(Qt::FramelessWindowHint);
}

void MainWindow::changeEvent(QEvent *e)
{
QMainWindow::changeEvent(e);

QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("/mnt/jffs2/venus.sqlite");
if (!db.open()) {
QMessageBox::critical(0, tr("Cannot open database"),
tr("Unable to establish a database connection.\n"
"This example needs SQLite support. Please read "
"the Qt SQL driver documentation for information how "
"to build it."), QMessageBox::Cancel);
}
QSqlQuery query;
bool checkquery=query.exec("create table apmc (id INTEGER PRIMARY KEY AUTOINCREMENT,username varchar(20), password password, apmc varchar(20))");
qDebug()<<checkquery;
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
db.close();
}
}


void MainWindow::on_pushButtonLogin_clicked()
{
QSqlQuery query;
QMessageBox* msgR=new QMessageBox(this);

QString UserName = ui->lineEditUsername->displayText();
QString Password = ui->lineEditpassword->text();
QString apmc=ui->comboBoxApmc->currentText();
qDebug()<<UserName.isEmpty();
qDebug()<<Password.isEmpty();




if(UserName.isEmpty())
{
QMessageBox::critical(0, tr("UserName"),
tr("Please Enter the UserName"), QMessageBox::Cancel);
}

else if(UserName.length()>=4)
{

QString s = ui->lineEditUsername->displayText();

QString s1 = s.mid(s.length()-4, s.length());
qDebug()<<s1.toStdString().c_str() << "What is the value of s1";

qDebug()<<"The Value of s1 is : "<<s1;

bool ok = false;
qulonglong Integer = s1.toULongLong(&ok);

qDebug()<<"Print for me"<<Integer;
if(ok)
{
qDebug()<<"True";
}
else
{
qDebug()<<"False";

QMessageBox::critical(0, tr("UserName"),
tr("Please Enter the UserName in right format"), QMessageBox::Cancel);


ui->lineEditUsername->clear();
ui->lineEditpassword->clear();

}

}
else if(Password.isEmpty())
{
QMessageBox::critical(0, tr("Password"),
tr("Please Enter the Password"), QMessageBox::Cancel);
}

else if(apmc=="None")
{
QMessageBox::critical(0, tr("Apmc"),
tr("Please Enter the Apmc"), QMessageBox::Cancel);
}
else
{

qDebug()<<ui->lineEditUsername->displayText();

bool check=query.exec("insert into apmc (username,password,apmc) values('" + ui->lineEditUsername->displayText() + "','" + ui->lineEditpassword->text() + "' ,'" + ui->comboBoxApmc->currentText()+"')");
qDebug()<<"insertino chekc-----------------"<<check;
msgR->setText("Inserted Successfully");

QPixmap bg("/home/venugopal/MyQTProjects/G2G/images/G2G.jpg");
QPalette p(palette());

p.setBrush(QPalette::Background, bg);
setAutoFillBackground(true);
msgR->setPalette(p);
msgR->exec();
this->close();
Entry* e=new Entry();
e->show();
}

}

void MainWindow::on_Delete_clicked()
{
system("rm -Rf /home/venugopal/sample/venu.xml");
}





When i try to perform this action giving only alphabets i am getting the error message that to enter the Username in right format and when i enter the Username with alphabets followed by number its not getting submitted in the button event rather its getting terminated.......Hope you got me........Any Ideas would be appreciable.........Thanks in Advance....


Regards,

ChrisW67
1st February 2013, 05:22
StarRocks, your development environment has a tool called a debugger. Debuggers have the ability to stop program execution when it reaches a given place in the source, inspect the value of variables, and single-step through your code (among many other cool and useful things). You should use your debugger to stop the program when it reaches the if() at line 85 and single step through your code. Doing this will make it obvious why it does not write to a database if the input is all alphabetic characters.

StarRocks
1st February 2013, 06:38
Dear Chris,


Thanks for the reply.....I have used the debugger tool long back as i din't get the right solution so posted back again in the forums regarding my issue....When i debug the program is getting terminated in the middle ...........So hope you got me Chris.........Thanks in Advance............


Regards,

Santosh Reddy
1st February 2013, 08:07
I have used the debugger tool long back as i din't get the right solution so posted back again in the forums regarding my issue

This cannot be a reson to expect some one do the debugging for you!

I think the better way is to "STOP LOOKING FOR RITGHT SOLUTION", and find out WHERE THE PROBLEM IS?


When i debug the program is getting terminated in the middle
What does this mean?
Does the debugger exit?

If you are still looking for someone to de someting, then attach the complete project zip file, and the problems you have with it. I can look at it during the weekend.

StarRocks
1st February 2013, 08:46
Dear Santosh,


Thanks for the reply.......Actually i wanted the solution that you gave,in the if-else format thats the only issue i had......Hope you got me now.....?.......Thanks in Advance..........

Regards,

Santosh Reddy
1st February 2013, 09:36
Actually i wanted the solution that you gave,in the if-else format thats the only issue i had......
Be vigilant. User debugger, step though the code and find out which line of code the program crashes.

wysota
1st February 2013, 11:11
It's the first time ever that I see anyone open a database in changeEvent()... That's... disturbing...