Results 1 to 20 of 46

Thread: Display only numbers at Last

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Display only numbers at Last

    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.

  2. #2
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Display only numbers at Last

    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.
    Qt Code:
    1. QString str = "IN12AP1212";
    2.  
    3. bool ok = false;
    4. int num = str.right(4).toInt(&ok);
    5.  
    6. if( !ok || !num )
    7. {
    8. qDebug() << "Invalid";
    9. }
    10. else
    11. {
    12. qDebug() << "Valid!!" << num;
    13. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Display only numbers at Last

    Quote Originally Posted by nish View Post
    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.
    "We can't solve problems by using the same kind of thinking we used when we created them." -- Einstein
    If you are posting code then please use [code] [/code] tags around it - makes addressing the problem easier.

  4. #4
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Display only numbers at Last

    Quote Originally Posted by ChrisW67 View Post
    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.

  5. #5
    Join Date
    Jul 2012
    Location
    Hyderabad
    Posts
    82
    Thanks
    5
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Display only numbers at Last

    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......


    Qt Code:
    1. public class test {
    2. public static void main(String[] args) {
    3. String s = "abcd1000231231232131212";
    4. System.out.println(check(s));
    5. }
    6.  
    7.  
    8. public static boolean check(String s ) {
    9.  
    10. if(s.length()>=4){
    11. String s1 = s.substring(s.length()-4, s.length());
    12. System.out.println(s1);
    13. try {
    14. Integer.parseInt(s1);
    15.  
    16. if(Integer.parseInt(s1) == 0){
    17. return false;
    18. }
    19. return true;
    20. } catch (NumberFormatException e) {
    21. return false;
    22. }
    23.  
    24. }
    25. return false;
    26. }
    27.  
    28. }
    To copy to clipboard, switch view to plain text mode 


    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

  6. #6
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 453 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Display only numbers at Last

    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.

    Qt Code:
    1. #include <iostream>
    2. #include <QString>
    3.  
    4. using namespace std;
    5.  
    6. bool check(QString s)
    7. {
    8. if(s.length() >= 4)
    9. {
    10. QString s1 = s.mid(s.length()-4, s.length());
    11. cout << s1.toStdString().c_str() << endl;
    12.  
    13. bool ok = false;
    14. qulonglong Integer = s1.toULongLong(&ok);
    15.  
    16. if(ok)
    17. {
    18. return true;
    19. }
    20. else
    21. {
    22. return false;
    23. }
    24. }
    25. return false;
    26. }
    27.  
    28. int main(int argc, char *argv[])
    29. {
    30. QString s = "abcd1000231231232131212";
    31. cout << check(s) << endl;
    32. }
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  7. #7
    Join Date
    Jul 2012
    Location
    Hyderabad
    Posts
    82
    Thanks
    5
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Smile Re: Display only numbers at Last

    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,

  8. #8
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 453 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Display only numbers at Last

    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
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  9. #9
    Join Date
    Jul 2012
    Location
    Hyderabad
    Posts
    82
    Thanks
    5
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Display only numbers at Last

    Dear Santosh,

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

    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include <QSqlDatabase>
    4. #include <stdio.h>
    5. #include <QTextStream>
    6. #include <QMessageBox>
    7. #include <QSqlRelationalTableModel>
    8. #include <QSqlQuery>
    9. #include <entry.h>
    10. #include <QRegExpValidator>
    11. #include <QLineEdit>
    12. #include <QValidator>
    13. #include <QComboBox>
    14. #include <qdebug.h>
    15. #include <QRegExp>
    16. #include <QValidator>
    17. #include <QRegExpValidator>
    18. #include <QPixmap>
    19. #include <QPalette>
    20. #include <QProcess>
    21. #include <QFile>
    22.  
    23. #include <Q_INT32>
    24.  
    25. MainWindow::MainWindow(QWidget *parent) :
    26. QMainWindow(parent),
    27. ui(new Ui::MainWindow)
    28. {
    29. ui->setupUi(this);
    30.  
    31. ui->lineEditpassword->setEchoMode(QLineEdit::Password);
    32. ui->lineEditUsername->setMaxLength(15);
    33. ui->lineEditpassword->setMaxLength(15);
    34.  
    35. QPixmap bg(":/images/G2G.jpg");
    36. QPalette p(palette());
    37. p.setBrush(QPalette::Background, bg);
    38. setAutoFillBackground(true);
    39. setPalette(p);
    40.  
    41. setWindowFlags(Qt::FramelessWindowHint);
    42. }
    43.  
    44. void MainWindow::changeEvent(QEvent *e)
    45. {
    46. QMainWindow::changeEvent(e);
    47.  
    48. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    49. db.setDatabaseName("/mnt/jffs2/venus.sqlite");
    50. if (!db.open()) {
    51. QMessageBox::critical(0, tr("Cannot open database"),
    52. tr("Unable to establish a database connection.\n"
    53. "This example needs SQLite support. Please read "
    54. "the Qt SQL driver documentation for information how "
    55. "to build it."), QMessageBox::Cancel);
    56. }
    57. QSqlQuery query;
    58. bool checkquery=query.exec("create table apmc (id INTEGER PRIMARY KEY AUTOINCREMENT,username varchar(20), password password, apmc varchar(20))");
    59. qDebug()<<checkquery;
    60. switch (e->type()) {
    61. case QEvent::LanguageChange:
    62. ui->retranslateUi(this);
    63. break;
    64. default:
    65. break;
    66. db.close();
    67. }
    68. }
    69.  
    70.  
    71. void MainWindow::on_pushButtonLogin_clicked()
    72. {
    73. QSqlQuery query;
    74. QMessageBox* msgR=new QMessageBox(this);
    75.  
    76. QString UserName = ui->lineEditUsername->displayText();
    77. QString Password = ui->lineEditpassword->text();
    78. QString apmc=ui->comboBoxApmc->currentText();
    79. qDebug()<<UserName.isEmpty();
    80. qDebug()<<Password.isEmpty();
    81.  
    82.  
    83.  
    84.  
    85. if(UserName.isEmpty())
    86. {
    87. QMessageBox::critical(0, tr("UserName"),
    88. tr("Please Enter the UserName"), QMessageBox::Cancel);
    89. }
    90.  
    91. else if(UserName.length()>=4)
    92. {
    93.  
    94. QString s = ui->lineEditUsername->displayText();
    95.  
    96. QString s1 = s.mid(s.length()-4, s.length());
    97. qDebug()<<s1.toStdString().c_str() << "What is the value of s1";
    98.  
    99. qDebug()<<"The Value of s1 is : "<<s1;
    100.  
    101. bool ok = false;
    102. qulonglong Integer = s1.toULongLong(&ok);
    103.  
    104. qDebug()<<"Print for me"<<Integer;
    105. if(ok)
    106. {
    107. qDebug()<<"True";
    108. }
    109. else
    110. {
    111. qDebug()<<"False";
    112.  
    113. QMessageBox::critical(0, tr("UserName"),
    114. tr("Please Enter the UserName in right format"), QMessageBox::Cancel);
    115.  
    116.  
    117. ui->lineEditUsername->clear();
    118. ui->lineEditpassword->clear();
    119.  
    120. }
    121.  
    122. }
    123. else if(Password.isEmpty())
    124. {
    125. QMessageBox::critical(0, tr("Password"),
    126. tr("Please Enter the Password"), QMessageBox::Cancel);
    127. }
    128.  
    129. else if(apmc=="None")
    130. {
    131. QMessageBox::critical(0, tr("Apmc"),
    132. tr("Please Enter the Apmc"), QMessageBox::Cancel);
    133. }
    134. else
    135. {
    136.  
    137. qDebug()<<ui->lineEditUsername->displayText();
    138.  
    139. bool check=query.exec("insert into apmc (username,password,apmc) values('" + ui->lineEditUsername->displayText() + "','" + ui->lineEditpassword->text() + "' ,'" + ui->comboBoxApmc->currentText()+"')");
    140. qDebug()<<"insertino chekc-----------------"<<check;
    141. msgR->setText("Inserted Successfully");
    142.  
    143. QPixmap bg("/home/venugopal/MyQTProjects/G2G/images/G2G.jpg");
    144. QPalette p(palette());
    145.  
    146. p.setBrush(QPalette::Background, bg);
    147. setAutoFillBackground(true);
    148. msgR->setPalette(p);
    149. msgR->exec();
    150. this->close();
    151. Entry* e=new Entry();
    152. e->show();
    153. }
    154.  
    155. }
    156.  
    157. void MainWindow::on_Delete_clicked()
    158. {
    159. system("rm -Rf /home/venugopal/sample/venu.xml");
    160. }
    To copy to clipboard, switch view to plain text mode 


    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,

  10. #10
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Display only numbers at Last

    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.

  11. #11
    Join Date
    Jul 2012
    Location
    Hyderabad
    Posts
    82
    Thanks
    5
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Smile Re: Display only numbers at Last

    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,

  12. #12
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 453 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Display only numbers at Last

    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.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  13. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,372
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Display only numbers at Last

    It's the first time ever that I see anyone open a database in changeEvent()... That's... disturbing...
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 2
    Last Post: 26th January 2012, 15:31
  2. How to display the numbers in a QList<QString> ?
    By harish in forum Qt Programming
    Replies: 4
    Last Post: 3rd January 2012, 04:26
  3. Replies: 1
    Last Post: 18th June 2011, 18:28
  4. Replies: 3
    Last Post: 17th April 2010, 21:35
  5. How to use QLCDnumber to display negative numbers?
    By grissiom in forum Qt Programming
    Replies: 1
    Last Post: 19th March 2010, 06:23

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.