How to prevent changing selection in QtableView, if the data is invalid?



  • Hi,
    When adding new data to QtableView/QRelationalTableModel, I need to verify the data entered by the user. The verification is done by using QValidator, but how can I prevent changing from that cell if the data is invalid, and to instruct user to correct the data?
    Thanks in advance,
    Ras



  • Hi, ~ras123!

    If I correctly understand what do you want - you should use proxy model. It's allow to set some kind of controll between model and view.

    Sorry if I misunderstood.



  • Proxy doesn't give the result. I need to validate data, at the instant on user submitted it. If it is valid, then move to next column, otherwise stay there. I think this is related to the QTableViw rather than the associated model.



  • Reimplement the focusOutEvent for the input widget and setFocus() if there is no acceptable input. You can then use a custom delegate to show your input widget.

    @
    class LineEdit : public QLineEdit
    {
    Q_OBJECT

    public:
    LineEdit(QWidget *parent = 0) : QLineEdit(parent) {}

    protected:
    void focusOutEvent(QFocusEvent *event)
    {
    if (((currentFocus_ == this) || (currentFocus_ == 0)) &&
    (hasAcceptableInput() == false))
    {
    currentFocus_ = this;
    setFocus();
    }
    else
    {
    currentFocus_ = 0;
    QLineEdit::focusOutEvent(event);
    }
    }

    private:
    static LineEdit *currentFocus_;
    };

    LineEdit *LineEdit::currentFocus_ = 0;
    @
    Brain to terminal. Not tested.



  • Thanks, I am looking for a similar solution. Since it is a table, and its validation depends on the current column, how to find the column number from the above approach? Or is it possible to similar programming for QtableView?


Log in to reply
 

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