Results 1 to 4 of 4

Thread: Validator for QDoubleSpinBox

  1. #1
    Join Date
    May 2007
    Posts
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Validator for QDoubleSpinBox

    Hi,

    I have a QTableView with a QAbstractTableModel and a custom delegate for editing the different fields. For one of the fields I have a QDoubleSpinBox and I want to change the default validator to allow both a comma and a dot to be used as a decimal symbol. However the lineEdit() methond in QDoubleSpinBox is protected, so I get an error. What is the easiest way I can change the spinbox' validator?

    The code:

    Qt Code:
    1. void ExpensesDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
    2. {
    3. if (index.column() == AMOUNT_COLUMN) {
    4. QDoubleSpinBox *amountEditor = qobject_cast<QDoubleSpinBox *>(editor);
    5.  
    6. amountEditor->lineEdit()->setValidator(new QRegExpValidator(QRegExp("-?[0-9]*[.,]?[0-9]*"), amountEditor)); // <---
    7.  
    8. amountEditor->setValue(index.model()->data(index, Qt::EditRole).toDouble());
    9.  
    10. ...
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Validator for QDoubleSpinBox

    Hmm, at least you can use QObject::findChild() to get around the limitation.
    J-P Nurmi

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Validator for QDoubleSpinBox

    I think changing the validator is not enough. The spinbox has its own internal validation method - valueFromText. It's enough if you reimplement it and convert the text to double there, no need for a validator.

  4. #4
    Join Date
    May 2007
    Posts
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Validator for QDoubleSpinBox

    Quote Originally Posted by wysota View Post
    I think changing the validator is not enough. The spinbox has its own internal validation method - valueFromText. It's enough if you reimplement it and convert the text to double there, no need for a validator.
    The problem is just that without altering the validator, one can't type a comma. Here's the solution I ended up with:

    Qt Code:
    1. class AmountSpinBox : public QDoubleSpinBox
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. AmountSpinBox(QWidget *parent = 0) : QDoubleSpinBox(parent)
    7. {
    8. m_decimalSymbol = KGlobal::locale()->monetaryDecimalSymbol();
    9.  
    10. // regexp for the validator:
    11. // 1) optional minus sign then 0 or more digits
    12. // 2) dot or locale decimal symbol
    13. // 3) 0, 1 or 2 digits
    14. lineEdit()->setValidator(new QRegExpValidator(QRegExp("-?[0-9]*[." +
    15. (m_decimalSymbol != "." ? m_decimalSymbol : "") // add locale deciman symbol if it's not a dot
    16. + "]?[0-9]{,2}"), this));
    17.  
    18. setAlignment(Qt::AlignRight);
    19. setButtonSymbols(QDoubleSpinBox::PlusMinus);
    20. setRange(-9999999999.99, 9999999999.99);
    21. }
    22.  
    23. private:
    24. double valueFromText(const QString &text) const
    25. {
    26. QString m_decimalSymbol = KGlobal::locale()->monetaryDecimalSymbol();
    27. QString temp = text;
    28. temp.replace(QRegExp(m_decimalSymbol), "."); // replace locale decimal symbol with dot before toDouble()
    29.  
    30. return temp.toDouble();
    31. }
    32.  
    33. QValidator::State validate ( QString & input, int & pos ) const
    34. {
    35. // let's *really* trust the validator :-)
    36. return QValidator::Acceptable;
    37. }
    38.  
    39. QString m_decimalSymbol;
    40. };
    To copy to clipboard, switch view to plain text mode 
    Last edited by ehamberg; 15th March 2008 at 12:04.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.