Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to prevent changing selection in QtableView, if the data is invalid?
QtWS25 Last Chance

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

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 2.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    ras123
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • T Offline
      T Offline
      tucnak
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • R Offline
        R Offline
        ras123
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • L Offline
          L Offline
          lgeyer
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • R Offline
            R Offline
            ras123
            wrote on last edited by
            #5

            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?

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved