Im trying to create a fail safe integer input in QT but am getting some errors.
If my code is way off the ball park let me know with your coments
Code:
#ifndef QTINTEGERINPUT_H
#define QTINTEGERINPUT_H
#include <qwidget.h>
#include <qlineedit.h>
class QTIntegerInput : public QWidget
{
Q_OBJECT
public:
QTIntegerInput(QWidget * parent);
private:
int valid_integer;
QLineEdit *number_box;
signals:
void handle_overflow(const QString & unvalidated_integer);
private slots:
void overflow();
public:
int get_integer();
};
#endif
next the implementation
Code:
#include <iostream>
using std::cerr;
#include <qlayout.h>
#include <qlabel.h>
#include <qvalidator.h>
#include <qwidget.h>
#include "QTIntegerInput.h"
QTIntegerInput::QTIntegerInput(QWidget * parent) : QWidget(parent)
{
QHBoxLayout *input_layout_box = new QHBoxLayout(this);
QLabel *label = new QLabel(tr("Please &enter an integer:"),this);
label->setAutoResize(true);
number_box = new QLineEdit(this);
QRegExp isDigit("[0-9]{16}");
number_box->setValidator(new QRegExpValidator(isDigit,number_box));
label->setBuddy(number_box);
input_layout_box->addWidget(label);
input_layout_box->addWidget(number_box);
//input_layout_box->show();
QObject::connect(number_box, SIGNAL(returnPressed()),this, SLOT(overflow()));
}
void QTIntegerInput::overflow()
{
try
{
bool ok;
int unvalidated_integer = number_box->text().toInt( &ok, 10 ); // dec == 0, ok == FALS
if (ok==FALSE) throw "overflow exception";
valid_integer = unvalidated_integer;
}
catch (char * exception)
{
emit handle_overflow(number_box->text());
}
}
;
void QTIntegerInput::handle_overflow(const QString &unvalidated){ cerr << "Overflow";}
int QTIntegerInput::get_integer(){return 0;}
errors:
moc_QTIntegerInput.o(.text+0x1ec): In function `QTIntegerInput::handle_overflow(QString const&)':
/home/curlious/qtenternumber/src/moc_QTIntegerInput.cpp:87: multiple definition of `QTIntegerInput::handle_overflow(QString const&)'
QTIntegerInput.o(.text+0x0):/usr/lib64/qt-3.3/include/qglist.h:150: first defined here
/usr/bin/ld: Warning: size of symbol `QTIntegerInput::handle_overflow(QString const&)' changed from 15 in QTIntegerInput.o to 112 in moc_QTIntegerInput.o
collect2: ld returned 1 exit status
gmake[1]: *** [../bin/qtenternumber] Error 1
gmake: *** [sub-src] Error 2
*** Exited with status: 2 ***