Results 1 to 6 of 6

Thread: QLineEdit problem

  1. #1
    Join Date
    Jun 2010
    Posts
    7
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QLineEdit problem

    hi,
    I am writing a simple converter application here when we give a decimal number (0-255) then its convert in hex and binary or hex is convert decimal and binary ..., all are synchronize using signal-slot. i use validator for control use input. but it not showing me desired result.

    Thanks in advance

    ByteConvertorDialog.h
    Qt Code:
    1. #ifndef BYTECONVERTERDIALOG_H
    2. #define BYTECONVERTERDIALOG_H
    3.  
    4. #include <QWidget>
    5.  
    6. class QLineEdit; //forward declartion
    7.  
    8. class ByteConverterDialog : public QWidget
    9. {
    10. Q_OBJECT
    11. public:
    12. ByteConverterDialog();
    13. private:
    14. QLineEdit* decEdit;
    15. QLineEdit* hexEdit;
    16. QLineEdit* binEdit;
    17. private slots:
    18. void decChanged(const QString&);
    19. void hexChanged(const QString&);
    20. void binChanged(const QString&);
    21. };
    22.  
    23. #endif // BYTECONVERTERDIALOG_H
    To copy to clipboard, switch view to plain text mode 

    ByteConvertorDialog.cpp
    Qt Code:
    1. #include <QLineEdit>
    2. #include <QLabel>
    3. #include <QPushButton>
    4. #include <QVBoxLayout>
    5. #include <QHBoxLayout>
    6. #include <QGridLayout>
    7. #include <QIntValidator>
    8. #include <QRegExpValidator>
    9. #include "ByteConverterDialog.h"
    10.  
    11. ByteConverterDialog::ByteConverterDialog()
    12. {
    13. QVBoxLayout *mainLayout = new QVBoxLayout(this);
    14. QGridLayout *editLayout = new QGridLayout;
    15. QHBoxLayout *buttonLayout = new QHBoxLayout;
    16.  
    17. mainLayout->addLayout(editLayout);
    18. mainLayout->addStretch();
    19. mainLayout->addLayout(buttonLayout);
    20.  
    21. QLabel *decLabel = new QLabel(tr("Decimal"));
    22. QLabel *hexLabel = new QLabel(tr("Hex"));
    23. QLabel *binLabel = new QLabel(tr("Binary"));
    24. decEdit = new QLineEdit;
    25. hexEdit = new QLineEdit;
    26. binEdit = new QLineEdit;
    27.  
    28. editLayout->addWidget(decLabel,0,0);
    29. editLayout->addWidget(decEdit,0,1);
    30. editLayout->addWidget(hexLabel,1,0);
    31. editLayout->addWidget(hexEdit,1,1);
    32. editLayout->addWidget(binLabel,2,0);
    33. editLayout->addWidget(binEdit,2,1);
    34.  
    35. QPushButton *exitButton = new QPushButton(tr("Quit"));
    36.  
    37. buttonLayout->addStretch();
    38. buttonLayout->addWidget(exitButton);
    39.  
    40. exitButton->setDefault(true);
    41.  
    42. this->setWindowTitle("Byte Converter");
    43.  
    44. QIntValidator* decValidator = new QIntValidator(0,255,decEdit);
    45. decEdit->setValidator(decValidator);
    46.  
    47. QRegExpValidator* hexValidator = new QRegExpValidator(QRegExp("[0-9A-Fa-f]{1,2}"),hexEdit);
    48. hexEdit->setValidator(hexValidator);
    49.  
    50. QRegExpValidator* binValidator = new QRegExpValidator(QRegExp("[01]{1,8}"),binEdit);
    51. binEdit->setValidator(binValidator);
    52.  
    53. connect(exitButton,SIGNAL(clicked()),this,SLOT(close()));
    54. connect(decEdit,SIGNAL(textChanged(QString)),this,SLOT(decChanged(QString)));
    55. connect(hexEdit,SIGNAL(textChanged(QString)),this,SLOT(hexChanged(QString)));
    56. connect(binEdit,SIGNAL(textChanged(QString)),this,SLOT(binChanged(QString)));
    57. }
    58.  
    59. void ByteConverterDialog::decChanged(const QString& newValue)
    60. {
    61. bool ok;
    62. int num = newValue.toInt(&ok);
    63. if(ok)
    64. {
    65. hexEdit->setText(QString::number(num,16));
    66. binEdit->setText(QString::number(num,2));
    67. }
    68. else
    69. {
    70. hexEdit->setText("");
    71. binEdit->setText("");
    72. }
    73. }
    74.  
    75. void ByteConverterDialog::hexChanged(const QString& newValue)
    76. {
    77. bool ok;
    78. int num = newValue.toInt(&ok);
    79. if(ok)
    80. {
    81. decEdit->setText(QString::number(num));
    82. binEdit->setText(QString::number(num,2));
    83. }
    84. else
    85. {
    86. decEdit->setText("");
    87. binEdit->setText("");
    88. }
    89. }
    90.  
    91. void ByteConverterDialog::binChanged(const QString& newValue)
    92. {
    93. bool ok;
    94. int num = newValue.toInt(&ok);
    95. if(ok)
    96. {
    97. decEdit->setText(QString::number(num));
    98. hexEdit->setText(QString::number(num,16));
    99. }
    100. else
    101. {
    102. decEdit->setText("");
    103. hexEdit->setText("");
    104. }
    105. }
    To copy to clipboard, switch view to plain text mode 

    main.cpp
    Qt Code:
    1. #include <QApplication>
    2. #include "ByteConverterDialog.h"
    3.  
    4. int main(int argc,char *argv[])
    5. {
    6. QApplication app(argc,argv);
    7. ByteConverterDialog bc;
    8. bc.setAttribute(Qt::WA_QuitOnClose);
    9. bc.show();
    10. return app.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QLineEdit problem

    The only problem i see (at first look) is the textChanged() signal, that gets emited even when you don't want to, so use textEdited:
    Qt Code:
    1. connect(decEdit,SIGNAL(textEdited(QString)),this,SLOT(decChanged(QString)));
    2. //...
    To copy to clipboard, switch view to plain text mode 

    LE: The textChanged() is emitted even when when your lineEdit is updated from modify in another lineEdit and you want the signal emitted only when the user writes in that lineEdit
    Last edited by Zlatomir; 12th July 2010 at 05:55.

  3. #3
    Join Date
    Jun 2010
    Posts
    7
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QLineEdit problem

    Quote Originally Posted by Zlatomir View Post
    The only problem i see (at first look) is the textChanged() signal, that gets emited even when you don't want to, so use textEdited:
    Qt Code:
    1. connect(decEdit,SIGNAL(textEdited(QString)),this,SLOT(decChanged(QString)));
    2. //...
    To copy to clipboard, switch view to plain text mode 

    LE: The textChanged() is emitted even when when your lineEdit is updated from modify in another lineEdit and you want the signal emitted only when the user writes in that lineEdit
    Thanks a lot.

  4. #4
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QLineEdit problem

    Another thing, use base 16 when convert from QString into int in hexChanged (because you will get empty string if you write FF or any "letter" in the hex lineEdit)
    Qt Code:
    1. void ByteConverterDialog::hexChanged(const QString& newValue)
    2. {
    3. bool ok;
    4. int num = newValue.toInt(&ok, 16); // Here you need to use base 16
    5.  
    6. if(ok)
    7. {
    8. decEdit->setText(QString::number(num));
    9. binEdit->setText(QString::number(num,2));
    10. }
    11. else
    12. {
    13. decEdit->setText("");
    14. binEdit->setText("");
    15. }
    16. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QLineEdit problem

    Sorry for double post, but i found another little bug:
    the same thing needs to be done with binChanged():
    Qt Code:
    1. void ByteConverterDialog::binChanged(const QString& newValue)
    2. {
    3. bool ok;
    4. int num = newValue.toInt(&ok, 2); //use base 2 to get the results you want
    5. if(ok)
    6. {
    7. decEdit->setText(QString::number(num));
    8. hexEdit->setText(QString::number(num,16));
    9. }
    10. //...
    11. }
    To copy to clipboard, switch view to plain text mode 

  6. The following user says thank you to Zlatomir for this useful post:

    kumarpraveen (12th July 2010)

  7. #6
    Join Date
    Jun 2010
    Posts
    7
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QLineEdit problem

    yeah you are right. Thanks

Similar Threads

  1. QLineEdit Problem
    By newermind in forum Qt Programming
    Replies: 2
    Last Post: 18th August 2009, 14:14
  2. QLineEdit focus problem
    By matteo.cozzaglio in forum Qt Programming
    Replies: 3
    Last Post: 14th July 2009, 14:13
  3. problem with QLineEdit and QPalette
    By impeteperry in forum Qt Programming
    Replies: 4
    Last Post: 15th October 2008, 17:05
  4. QLineEdit keyPressEvent problem
    By impeteperry in forum Qt Programming
    Replies: 6
    Last Post: 27th November 2007, 16:57
  5. Problem with QLineEdit and setText
    By Elmo23x in forum Qt Programming
    Replies: 8
    Last Post: 12th April 2007, 12:35

Tags for this Thread

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.