Results 1 to 2 of 2

Thread: Disable QDoubleSpinBox validator

  1. #1
    Join Date
    Jun 2011
    Posts
    10
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Question Disable QDoubleSpinBox validator

    Hi.
    I' have a delegate for a QTableWidget which is a QDoubleSpinBox. Its purpose is editing the table.
    I want to disable the validator for QDoubleSpinBox because i'm conneting the valueChanged() signal from the QDoubleSpinBox to a function this way:
    Qt Code:
    1. // connecting signal
    2. connect(editor,SIGNAL(valueChanged(double)),this,SLOT(editing(double)));
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. void DoubleSpinDelegate::editing(double value)
    2. {
    3. QDoubleSpinBox *spin = static_cast<QDoubleSpinBox*>(sender());
    4. emit commitData(spin);
    5. }
    To copy to clipboard, switch view to plain text mode 

    I'm doing this for enabling a dynamic modification of the table. However, every time I commitData because the value is changed in the spinbox, it looks like a signal is sended to the spinbox for using the validator. For instance, I have in my table the value 1,45. I have a spinbox with 3 decimals so, when I try to edit the spinbox delegate opens with 1,450. Now if I edit any value after the comma the validator runs and just lets me edit 1 digit per time, so, if I wanna write 1,452 and I have 0,000 in the spinbox I have to write the 1, then highlight the first 0 and replace it by a 4, then I get 1,400... then, I have to highlight the second 0 and replace by 5 and so on...
    I know that this is because the QDoubleValidator of the QLineEdit present in the QDoubleSpinBox... however I would like to disable it... If anyone knows how to do it I would appreciate it.

    Just for having a context this is my class reimplementation of the delegate:

    QDynSpinDelegate.h
    Qt Code:
    1. #ifndef QDYNSPINDELEGATE_H
    2. #define QDYNSPINDELEGATE_H
    3.  
    4. #include <QStyledItemDelegate>
    5. #include <QDoubleSpinBox>
    6. #include <QLineEdit>
    7.  
    8. class QDynSpinDelegate : public QStyledItemDelegate
    9. {
    10. Q_OBJECT
    11.  
    12. public:
    13. QDynSpinDelegate(QObject* parent);
    14. QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;
    15. void setEditorData(QWidget *editor, const QModelIndex &index) const;
    16. void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const;
    17. void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const;
    18. private slots:
    19. void editing(double value);
    20. };
    21.  
    22. #endif // QDYNSPINDELEGATE_H
    To copy to clipboard, switch view to plain text mode 

    QDynSpinDelegate.cpp
    Qt Code:
    1. #include "QDynSpinDelegate.h"
    2.  
    3. QDynSpinDelegate::QDynSpinDelegate(QObject *parent) : QStyledItemDelegate(parent)
    4. {
    5. }
    6.  
    7. QWidget* QDynSpinDelegate::createEditor(QWidget* parent,const QStyleOptionViewItem &option,const QModelIndex &index) const
    8. {
    9.  
    10. if(index.column() == 0)
    11. {
    12. QLineEdit* editor = new QLineEdit(parent);
    13. return editor;
    14. }
    15. else
    16. {
    17. QDoubleSpinBox *editor = new QDoubleSpinBox(parent);
    18. editor->setAccelerated(true);
    19. editor->setFrame(false);
    20. connect(editor,SIGNAL(valueChanged(double)),this,SLOT(editing(double)));
    21. if(index.column() >=1 && index.column() <=3)
    22. {
    23. editor->setMinimum(-1000);
    24. editor->setMaximum(1000);
    25. editor->setDecimals(3);
    26. editor->setSuffix(" m");
    27. editor->setSingleStep(0.05);
    28. }
    29. else
    30. {
    31. editor->setMinimum(-360);
    32. editor->setMaximum(360);
    33. editor->setDecimals(3);
    34. editor->setSuffix(" °");
    35. editor->setSingleStep(0.5);
    36. }
    37. return editor;
    38. }
    39.  
    40. }
    41. void QDynSpinDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
    42. {
    43. if(index.column() == 0)
    44. {
    45. QString value = index.model()->data(index,Qt::EditRole).toString();
    46. QLineEdit* line = static_cast<QLineEdit*>(editor);
    47. line->setText(value);
    48. }
    49. else
    50. {
    51. double value = index.model()->data(index,Qt::EditRole).toDouble();
    52. QDoubleSpinBox *spin = static_cast<QDoubleSpinBox*>(editor);
    53. spin->setValue(value);
    54. }
    55. }
    56. void QDynSpinDelegate::setModelData(QWidget* editor,QAbstractItemModel* model,const QModelIndex &index) const
    57. {
    58. if(index.column() == 0)
    59. {
    60. QLineEdit* line = static_cast<QLineEdit*>(editor);
    61. QString value = line->text();
    62. model->setData(index,value);
    63. }
    64. else
    65. {
    66. QDoubleSpinBox* spin = static_cast<QDoubleSpinBox*>(editor);
    67. double value = spin->value();
    68. model->setData(index,value);
    69. }
    70.  
    71. }
    72. void QDynSpinDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
    73. {
    74. editor->setGeometry(option.rect);
    75. }
    76.  
    77. void QDynSpinDelegate::editing(double value)
    78. {
    79. QDoubleSpinBox *spin = static_cast<QDoubleSpinBox*>(sender());
    80. emit commitData(spin);
    81. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Disable QDoubleSpinBox validator

    You can subclass QDoubleSpinBox, and then override the virtual function validate()

    Qt Code:
    1. class MyDoubleSpinBox : public QDoubleSpinBox
    2. {
    3. ...
    4. public:
    5. virtual QValidator::State validate ( QString & text, int & pos ) const
    6. {
    7. return QValidator::Acceptable;
    8. }
    9. ...
    10. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. QSpinBox validator problem
    By batileon in forum Newbie
    Replies: 3
    Last Post: 29th September 2011, 15:54
  2. Validator
    By tinysoft in forum Newbie
    Replies: 5
    Last Post: 13th April 2011, 16:00
  3. Validator for QDoubleSpinBox
    By ehamberg in forum Qt Programming
    Replies: 3
    Last Post: 15th March 2008, 11:37
  4. QRegExp validator
    By mattia in forum Newbie
    Replies: 2
    Last Post: 28th November 2007, 08:15
  5. what's wrong with my validator?
    By TheKedge in forum Qt Programming
    Replies: 5
    Last Post: 26th January 2006, 21:41

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.