- 
UID:111063 
 
- 
- 注册时间2011-01-12
 
- 最后登录2012-01-11
 
- 在线时间89小时
 
 
 
- 
- 发帖88
 
- 搜Ta的帖子
 
- 精华0
 
- 金钱880
 - 威望98
 - 贡献值0
 - 好评度88
 
 
 
- 
访问TA的空间加好友用道具
 
 
 
 
 
 
  
 
 | 
 
 
最近再看《The book of Qt4》的第二章,按照书上写的一个例子,输出有 问题,但是我觉得是对的,不知道哪里有问题。  错误现象:  在十进制的QLineEdit中输入0,1可以 显示,输入 其他不能显示,并且不论输入啥,二进制都显示一大串数字。而十六进制和二进制的QLineEdit不能输入。  下面的代码,我给出了完整的,但是基本可以忽略,只需要看ByteConverterDialog.cpp中的输入控制器下面那一些代码:  - #ifndef BYTECONVERTERDIALOG_H
 - #define BYTECONVERTERDIALOG_H
 - #include <QDialog>
 - class QLineEdit;
 - class ByteConverterDialog : public QDialog
 - {
 -     Q_OBJECT
 - public:
 -     ByteConverterDialog();
 - private:
 -     QLineEdit *decEdit;
 -     QLineEdit *hexEdit;
 -     QLineEdit *binEdit;
 - private slots:
 -     void decChanged(const QString &);
 -     void hexChanged(const QString &);
 -     void binChanged(const QString &);
 - };
 - #endif // BYTECONVERTERDIALOG_H
 
 - // byteConverter/main.cpp
 - #include <QApplication>
 - #include "ByteConverterDialog.h"
 - int main(int argc, char *argv[])
 - {
 -     QApplication a(argc, argv);
 -     ByteConverterDialog bc;
 -     bc.setAttribute(Qt::WA_QuitOnClose);
 -     bc.show();
 -     return a.exec();
 - }
 
 - // byteConverter/ ByteConverterDialog.cpp
 - #include "ByteConverterDialog.h"
 - #include <QLabel>
 - #include <QLineEdit>
 - #include <QPushButton>
 - #include <QVBoxLayout>
 - #include <QHBoxLayout>
 - #include <QGridLayout>
 - #include <QIntValidator>
 - ByteConverterDialog::ByteConverterDialog()
 - {
 -     // Generate the necessary layouts
 -     QVBoxLayout *mainLayout = new QVBoxLayout(this);
 -     QGridLayout *editLayout = new QGridLayout;
 -     QHBoxLayout *buttonLayout = new QHBoxLayout;
 -     mainLayout->addLayout(editLayout);
 -     mainLayout->addStretch();
 -     mainLayout->addLayout(buttonLayout);
 -     //Generate the labels and line-edits and add them
 -     // to the object pointed at by editLayout
 -     QLabel *decLabel = new QLabel(tr("Decimal"));
 -     QLabel *hexLabel = new QLabel(tr("Hex"));
 -     QLabel *binLabel = new QLabel(tr("Binary"));
 -     decEdit = new QLineEdit;
 -     hexEdit = new QLineEdit;
 -     binEdit = new QLineEdit;
 -     editLayout->addWidget(decLabel, 0, 0);
 -     editLayout->addWidget(decEdit, 0, 1);
 -     editLayout->addWidget(hexLabel, 1, 0);
 -     editLayout->addWidget(hexEdit, 1, 1);
 -     editLayout->addWidget(binLabel, 2, 0);
 -     editLayout->addWidget(binEdit, 2, 1);
 -     // Create the Quit button and add it to the object pointed
 -     // at by buttonLayout
 -     QPushButton *exitButton = new QPushButton(tr("Quit"));
 -     buttonLayout->addStretch();
 -     buttonLayout->addWidget(exitButton);
 -     exitButton->setDefault(true);
 -     // Limit input to valid values
 -     QIntValidator* decValidator =
 -             new QIntValidator(0, 255, decEdit);
 -     decEdit->setValidator(decValidator);
 -     QRegExpValidator *hexValidator =
 -             new QRegExpValidator(QRegExp("[0-9A-Fa-f]{1, 2}"), hexEdit);
 -     hexEdit->setValidator(hexValidator);
 -     QRegExpValidator *binValidator =
 -             new QRegExpValidator(QRegExp("[0-1], {1, 8}"), binEdit);
 -     binEdit->setValidator(binValidator);
 -     setWindowTitle(tr("Byte Converter"));
 -     connect(exitButton, SIGNAL(clicked()), this, SLOT(accept()));
 -     connect(decEdit, SIGNAL(textChanged(QString)),
 -             this, SLOT(decChanged(QString)));
 -     connect(hexEdit, SIGNAL(textChanged(QString)),
 -             this, SLOT(hexChanged(QString)));
 -     connect(binEdit, SIGNAL(textChanged(QString)),
 -             this, SLOT(binChanged(QString)));
 - }
 - void ByteConverterDialog::decChanged(const QString &newValue)
 - {
 -     bool ok;
 -     int num = newValue.toInt(&ok);
 -     if(ok){
 -         hexEdit->setText(QString::number(num, 16));
 -         binEdit->setText(QString::number(num, 2));
 -     }
 -     else{
 -         hexEdit->setText("");
 -         binEdit->setText("");
 -     }
 - }
 - void ByteConverterDialog::hexChanged(const QString &newValue)
 - {
 -     bool ok;
 -     int num = newValue.toInt(&ok);
 -     if(ok){
 -         decEdit->setText(QString::number(num));
 -         binEdit->setText(QString::number(num, 2));
 -     }
 -     else{
 -         decEdit->setText("");
 -         binEdit->setText("");
 -     }
 - }
 - void ByteConverterDialog::binChanged(const QString &newValue)
 - {
 -     bool ok;
 -     int num = newValue.toInt(&ok);
 -     if(ok){
 -         hexEdit->setText(QString::number(num, 16));
 -         decEdit->setText(QString::number(num));
 -     }
 -     else{
 -         hexEdit->setText("");
 -         decEdit->setText("");
 -     }
 - }
 
 [ 此帖被17611904在2011-04-12 21:51重新编辑 ] 
 
 
 |