Issue
I need to start a QLineEdit with just one 0 inside it, but 2 conditions are needed:
- The user cannot delete that 0 never.
- The user is capable to change that 0 for a bigger number.
for me the best solution was using inputMasks but as you have to limit them, thats not the solution. Then I tried using RegEx but I could'nt make it work.
Solution
If a currency symbol is all that you need, and the input is indeed essentially numerical, then instead of QLineEdit, it might be easier to use a QSpinBox and then set a currency character as the prefix:
QSpinBox* pSpinBoxCurrency = new QSpinBox( this );
pSpinBoxCurrency->setRange( 0, 9999 );
pSpinBoxCurrency->setPrefix("$");
This is a quick and simple method. If you need more sophisticated input/output capabilities for that widget but it's still essentially numerical, then the next best step is to subclass QSpinBox and re implement validate(), valueFromText(), textFromValue(), etc. It's still an int or float under the hood, but you have more control over how it appears and what input it accepts.
There have been cases where that still wasn't enough for what I needed, and in those instances I had the most success writing a new widget that shows the numbers/data exactly how I wanted and just popping up a transient pre-populated QSpinbox to allow number entry. But I suggest trying to get the built-in classes to do most of what you want first.
Answered By - Yasser Malaika
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.