
Originally Posted by
eleanor
How can I accept the number from QLineEdit and then multiply it by 4 and then output it with QLabel?
To restrict the user to input only numbers, you can use a QIntValidator:
// the edit lineedit will only accept integers between 100 and 999
edit->setValidator(validator);
QValidator *validator = new QIntValidator(100, 999, this);
QLineEdit *edit = new QLineEdit(this);
// the edit lineedit will only accept integers between 100 and 999
edit->setValidator(validator);
To copy to clipboard, switch view to plain text mode
or you can use input mask:
// the edit lineedit will only accept 0-3 digit chars
edit->setInputMask("000");
QLineEdit *edit = new QLineEdit(this);
// the edit lineedit will only accept 0-3 digit chars
edit->setInputMask("000");
To copy to clipboard, switch view to plain text mode
For multiplying, you need a custom slot:
// class declaration
class mojClass : public ...
{
...
private slots:
void customSlot(const QString& input);
...
};
// class implementation
mojClass::mojClass()
{
...
connect(lineedit1,SIGNAL(textChanged(const QString&)),this,SLO T(customSlot(const QString&)));
}
// the custom slot
void mojClass::customSlot(const QString& input)
{
int num = 4 * input.toInt();
label3->setNum(num);
}
// class declaration
class mojClass : public ...
{
...
private slots:
void customSlot(const QString& input);
...
};
// class implementation
mojClass::mojClass()
{
...
connect(lineedit1,SIGNAL(textChanged(const QString&)),this,SLO T(customSlot(const QString&)));
}
// the custom slot
void mojClass::customSlot(const QString& input)
{
int num = 4 * input.toInt();
label3->setNum(num);
}
To copy to clipboard, switch view to plain text mode
Bookmarks