QTableView QValidator.



  • Hi,

    I am displaying some editable data with a QTableView.
    I want to validate some data with a QValidator. This should be possible.
    but i cant find the needed functions.

    @Qtableview tableView;
    tableView.setmodel( ... );

    QIntValidator* intValidator(0,11,tableView);

    // tableView.setvalidator(column 2, validator intValidator);@

    Any help is appreciated, thanks.


  • Moderators

    what exactly do you want to do?
    Validate the existing data of a table or the data the user can edit in the table?



  • Data input from the user.


  • Lifetime Qt Champion

    Then you'll have to make a "custom item delegate":http://qt-project.org/doc/qt-4.8/qstyleditemdelegate.html setting up the validator when creating the editor



  • which function creates the editor?
    @QWidget * QStyledItemDelegate::createEditor ( QWidget * parent, const QStyleOptionViewItem & option, const QModelIndex & index ) const@

    But how will i set the validator to the widget?

    @class tableValidator : public QStyledItemDelegate
    {
    Q_OBJECT
    public:
    explicit tableValidator(QValidator* validator=0, QObject *parent = 0) : QStyledItemDelegate(parent)
    {
    Validator = validator;
    }

    virtual ~tableValidator()
    {
        
    }
    

    signals:

    public slots:

    private:
    QValidator* Validator;

    };@


  • Moderators

    easiest way would be probably this:
    @
    QWidget * MyDelegate::createEditor ( QWidget * parent, const QStyleOptionViewItem & option, const QModelIndex & index ) const
    {
    QWidget* editor = QStyledItemDelegate::createEditor(parent, option, index);
    QLineEdit* lineEditEditor = qobject_cast<QLineEdit*>(editor);
    if( lineEditEditor )
    lineEditEditor->setValidator(myValidator);

     return editor;
    

    }
    @



  • Thanx guys.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.