Reputation: 207
I created some QLineEdit
, which have different sizes.
I want to set the max length of those to suit the width of them.
To be more specific: for example, with a width of 50, I will only allow to enter about 7 characters, because the size of each character is different.
How can I exactly calculate the maximum length needed?
Upvotes: 2
Views: 3193
Reputation: 8313
You can set a limit based on the width of myLineEdit
such that it will be able to fit all characters up to that limit by doing this:
#include <QFontMetrics>
#include <QLineEdit>
QLineEdit myLineEdit;
// get max character width of the line edit's font
int maxWidth = QFontMetrics(myLineEdit->font()).maxWidth();
// find the character limit based on the max width
int charlimit = myLineEdit.width() / maxWidth;
// set the character limit on the line edit
myLineEdit->setMaxLength(charlimit);
However, here are some reasons you probably don't want to in production code:
Upvotes: 2
Reputation: 2210
Use a QFontMetrics
:
QFontMetrics fm(QApplication::font()); // You can use any font here.
int stringWidth = fm.width("Some string");
EDIT:
At first you have to know when your QLineEdit
is resized. So you can either derive your own class and reimplement the resizeEvent()
method, or you can use an event filter.
Then create a special validator:
class CLengthValidator : public QValidator
{
public:
CLenghtValidator(QObject* parent, const QFont & font)
: QValidator(parent), m_maxw(0), m_fm(font)
{}
void setMaxValidWidth(int width)
{
m_maxw = width;
}
State validate(QString & input, int & pos) const override
{
int stringWidth = m_fm.width(input);
if (stringWidth <= m_maxw)
return Acceptable;
else
return Invalid;
}
private:
int m_maxw;
QFontMetrics m_fm;
}
Set the validator to your line edit (using the QLineEdit::setValidator()
method):
lineEdit->setValidator(new CLengthValidator(lineEdit, lineEdit->font()));
Now, every time the line edit is resized, you have to call CLengthValidator::setMaxValidWidth()
method with the current line edit width as a parameter. You will do that in your reimplemented QLineEdit::resizeEvent()
method or in your event filter.
This way you will get a line edit accepting only string which width is not greater than width of actual line edit.
Upvotes: 2