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