Вот такой у меня получился класс.
MyInputBox.h
#include <QtGui>
#define ONLY_DIGITS 1
#define MONEY 2
#define ALL_KEYS 3
class MyInputBox :public QDialog
{
Q_OBJECT
public:
MyInputBox(QDialog *parent = 0);
void setLabelText(QString label);
void setMaxLength(int maxLength);
void setInputMask(int inputMask);
void setText(QString text);
QString text();
bool getResult();
static QString getText(const QString title, const QString label, const QString text="", int mask=1, bool *ok=0, int maxLength=255);
~MyInputBox();
private:
int _InputMask;
bool _buttonResult;
QFont *mainFont;
QVBoxLayout *layoutVmain;
QHBoxLayout *layoutHbutton;
QLabel *labelString;
QLineEdit *lineeditInput;
QPushButton *buttonOk;
QPushButton *buttonCancel;
bool eventFilter(QObject *target, QEvent *event);
public slots:
void buttonOkSlot();
void buttonCancelSlot();
};
MyInputBox.cpp
MyInputBox::MyInputBox(QDialog *parent)
:QDialog(parent)
{
setMinimumWidth(160);
setMinimumHeight(100);
mainFont = new QFont;
mainFont->setPixelSize(16);
labelString = new QLabel("Input text:");
lineeditInput = new QLineEdit;
buttonOk = new QPushButton("Ok");
buttonCancel = new QPushButton("Cancel");
layoutHbutton = new QHBoxLayout;
layoutHbutton->addWidget(buttonOk);
layoutHbutton->addWidget(buttonCancel);
layoutVmain = new QVBoxLayout(this);
layoutVmain->addWidget(labelString);
layoutVmain->addWidget(lineeditInput);
layoutVmain->addLayout(layoutHbutton);
layoutVmain->addStretch();
setFont(*mainFont);
setWindowTitle("MyInputBox");
_InputMask=ONLY_DIGITS;
_buttonResult=false;
connect(buttonOk, SIGNAL(clicked()), this, SLOT(buttonOkSlot()));
connect(buttonCancel, SIGNAL(clicked()), this, SLOT(buttonCancelSlot()));
lineeditInput->installEventFilter(this);
}
void MyInputBox::setLabelText(QString label)
{
labelString->setText(label);
}
void MyInputBox::setMaxLength(int maxLength)
{
lineeditInput->setMaxLength(maxLength);
}
void MyInputBox::buttonOkSlot()
{
_buttonResult=true;
close();
}
void MyInputBox::buttonCancelSlot()
{
lineeditInput->setText("");
close();
}
void MyInputBox::setInputMask(int inputMask)
{
if ((inputMask>0)&&(inputMask<4))_InputMask=inputMask;
}
QString MyInputBox::text()
{
return lineeditInput->text();
}
void MyInputBox::setText(QString text)
{
lineeditInput->setText(text);
}
bool MyInputBox::getResult()
{
return _buttonResult;
}
QString MyInputBox::getText(const QString title, const QString label, const QString text, int mask, bool *ok, int maxLength)
{
QString TextReturn;
MyInputBox Input;
Input.setWindowTitle(title);
Input.setLabelText(label);
Input.setText(text);
Input.setInputMask(mask);
Input.setMaxLength(maxLength);
Input.exec();
*ok=Input.getResult();
TextReturn=Input.text();
return TextReturn;
}
bool MyInputBox::eventFilter(QObject *target, QEvent *event)
{
if (target == lineeditInput)
{
if (event->type() == QEvent::KeyPress)
{
if (_InputMask==ALL_KEYS)
{
lineeditInput->event(event);
return true;
}
else if (_InputMask==ONLY_DIGITS)
{
QKeyEvent *keyEvent = (QKeyEvent *)event;
if ((keyEvent->key() == Qt::Key_0)||
(keyEvent->key() == Qt::Key_1)||
(keyEvent->key() == Qt::Key_2)||
(keyEvent->key() == Qt::Key_3)||
(keyEvent->key() == Qt::Key_4)||
(keyEvent->key() == Qt::Key_5)||
(keyEvent->key() == Qt::Key_6)||
(keyEvent->key() == Qt::Key_7)||
(keyEvent->key() == Qt::Key_8)||
(keyEvent->key() == Qt::Key_9)||
(keyEvent->key() == Qt::Key_Tab)||
(keyEvent->key() == Qt::Key_Backspace)||
(keyEvent->key() == Qt::Key_Delete)||
(keyEvent->key() == Qt::Key_Right)||
(keyEvent->key() == Qt::Key_Left))
lineeditInput->event(event);
return true;
}
else if (_InputMask==MONEY)
{
QKeyEvent *keyEvent = (QKeyEvent *)event;
if ((keyEvent->key() == Qt::Key_Tab)||
(keyEvent->key() == Qt::Key_Backspace)||
(keyEvent->key() == Qt::Key_Delete)||
(keyEvent->key() == Qt::Key_Right)||
(keyEvent->key() == Qt::Key_Left))
lineeditInput->event(event);
else if ((keyEvent->key() == Qt::Key_Comma)||(keyEvent->key() == Qt::Key_Period))
{
if (lineeditInput->text().indexOf(',')<0) lineeditInput->insert(",");
}
else if ((keyEvent->key() == Qt::Key_0)||
(keyEvent->key() == Qt::Key_1)||
(keyEvent->key() == Qt::Key_2)||
(keyEvent->key() == Qt::Key_3)||
(keyEvent->key() == Qt::Key_4)||
(keyEvent->key() == Qt::Key_5)||
(keyEvent->key() == Qt::Key_6)||
(keyEvent->key() == Qt::Key_7)||
(keyEvent->key() == Qt::Key_8)||
(keyEvent->key() == Qt::Key_9))
{
if (lineeditInput->text().indexOf(',')<0) lineeditInput->event(event);
else
{
if (lineeditInput->text().length()-(lineeditInput->text().indexOf(',')+1)<2) lineeditInput->event(event);
else
{
if (lineeditInput->text().indexOf(',')>=lineeditInput->cursorPosition()) lineeditInput->event(event);
}
}
}
return true;
}
}
}
return QDialog::eventFilter(target, event);
}
MyInputBox::~MyInputBox()
{
delete mainFont;
delete labelString;
delete lineeditInput;
delete buttonOk;
delete buttonCancel;
delete layoutHbutton;
delete layoutVmain;
}
Вызов:
bool bOk;
MyInputBox::getText("Ввод:", "ВВЕДИТЕ СУММУ ОТЧЕТА", "", 2, &bOk);
Прошу высказывать предложения по оптимизации.