• 6715阅读
  • 1回复

[提问]一个十进制,十六进制,二进制同步显示的例子,求助 [复制链接]

上一主题 下一主题
离线17611904
 
只看楼主 倒序阅读 楼主  发表于: 2011-04-12
最近再看《The book of Qt4》的第二章,按照书上写的一个例子,输出有问题,但是我觉得是对的,不知道哪里有问题。
错误现象:
在十进制的QLineEdit中输入0,1可以显示,输入其他不能显示,并且不论输入啥,二进制都显示一大串数字。而十六进制和二进制的QLineEdit不能输入。

下面的代码,我给出了完整的,但是基本可以忽略,只需要看ByteConverterDialog.cpp中的输入控制器下面那一些代码:

  1. #ifndef BYTECONVERTERDIALOG_H
  2. #define BYTECONVERTERDIALOG_H
  3. #include <QDialog>
  4. class QLineEdit;
  5. class ByteConverterDialog : public QDialog
  6. {
  7.     Q_OBJECT
  8. public:
  9.     ByteConverterDialog();
  10. private:
  11.     QLineEdit *decEdit;
  12.     QLineEdit *hexEdit;
  13.     QLineEdit *binEdit;
  14. private slots:
  15.     void decChanged(const QString &);
  16.     void hexChanged(const QString &);
  17.     void binChanged(const QString &);
  18. };
  19. #endif // BYTECONVERTERDIALOG_H


  1. // byteConverter/main.cpp
  2. #include <QApplication>
  3. #include "ByteConverterDialog.h"
  4. int main(int argc, char *argv[])
  5. {
  6.     QApplication a(argc, argv);
  7.     ByteConverterDialog bc;
  8.     bc.setAttribute(Qt::WA_QuitOnClose);
  9.     bc.show();
  10.     return a.exec();
  11. }


  1. // byteConverter/ ByteConverterDialog.cpp
  2. #include "ByteConverterDialog.h"
  3. #include <QLabel>
  4. #include <QLineEdit>
  5. #include <QPushButton>
  6. #include <QVBoxLayout>
  7. #include <QHBoxLayout>
  8. #include <QGridLayout>
  9. #include <QIntValidator>
  10. ByteConverterDialog::ByteConverterDialog()
  11. {
  12.     // Generate the necessary layouts
  13.     QVBoxLayout *mainLayout = new QVBoxLayout(this);
  14.     QGridLayout *editLayout = new QGridLayout;
  15.     QHBoxLayout *buttonLayout = new QHBoxLayout;
  16.     mainLayout->addLayout(editLayout);
  17.     mainLayout->addStretch();
  18.     mainLayout->addLayout(buttonLayout);
  19.     //Generate the labels and line-edits and add them
  20.     // to the object pointed at by editLayout
  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.     editLayout->addWidget(decLabel, 0, 0);
  28.     editLayout->addWidget(decEdit, 0, 1);
  29.     editLayout->addWidget(hexLabel, 1, 0);
  30.     editLayout->addWidget(hexEdit, 1, 1);
  31.     editLayout->addWidget(binLabel, 2, 0);
  32.     editLayout->addWidget(binEdit, 2, 1);
  33.     // Create the Quit button and add it to the object pointed
  34.     // at by buttonLayout
  35.     QPushButton *exitButton = new QPushButton(tr("Quit"));
  36.     buttonLayout->addStretch();
  37.     buttonLayout->addWidget(exitButton);
  38.     exitButton->setDefault(true);
  39.     // Limit input to valid values
  40.     QIntValidator* decValidator =
  41.             new QIntValidator(0, 255, decEdit);
  42.     decEdit->setValidator(decValidator);
  43.     QRegExpValidator *hexValidator =
  44.             new QRegExpValidator(QRegExp("[0-9A-Fa-f]{1, 2}"), hexEdit);
  45.     hexEdit->setValidator(hexValidator);
  46.     QRegExpValidator *binValidator =
  47.             new QRegExpValidator(QRegExp("[0-1], {1, 8}"), binEdit);
  48.     binEdit->setValidator(binValidator);
  49.     setWindowTitle(tr("Byte Converter"));
  50.     connect(exitButton, SIGNAL(clicked()), this, SLOT(accept()));
  51.     connect(decEdit, SIGNAL(textChanged(QString)),
  52.             this, SLOT(decChanged(QString)));
  53.     connect(hexEdit, SIGNAL(textChanged(QString)),
  54.             this, SLOT(hexChanged(QString)));
  55.     connect(binEdit, SIGNAL(textChanged(QString)),
  56.             this, SLOT(binChanged(QString)));
  57. }
  58. void ByteConverterDialog::decChanged(const QString &newValue)
  59. {
  60.     bool ok;
  61.     int num = newValue.toInt(&ok);
  62.     if(ok){
  63.         hexEdit->setText(QString::number(num, 16));
  64.         binEdit->setText(QString::number(num, 2));
  65.     }
  66.     else{
  67.         hexEdit->setText("");
  68.         binEdit->setText("");
  69.     }
  70. }
  71. void ByteConverterDialog::hexChanged(const QString &newValue)
  72. {
  73.     bool ok;
  74.     int num = newValue.toInt(&ok);
  75.     if(ok){
  76.         decEdit->setText(QString::number(num));
  77.         binEdit->setText(QString::number(num, 2));
  78.     }
  79.     else{
  80.         decEdit->setText("");
  81.         binEdit->setText("");
  82.     }
  83. }
  84. void ByteConverterDialog::binChanged(const QString &newValue)
  85. {
  86.     bool ok;
  87.     int num = newValue.toInt(&ok);
  88.     if(ok){
  89.         hexEdit->setText(QString::number(num, 16));
  90.         decEdit->setText(QString::number(num));
  91.     }
  92.     else{
  93.         hexEdit->setText("");
  94.         decEdit->setText("");
  95.     }
  96. }
[ 此帖被17611904在2011-04-12 21:51重新编辑 ]
离线17611904
只看该作者 1楼 发表于: 2011-04-14
没人~~~
快速回复
限100 字节
 
上一个 下一个