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. QSqlTableModel - Check for invalid cells in QTableView?
Forum Updated to NodeBB v4.3 + New Features

QSqlTableModel - Check for invalid cells in QTableView?

Scheduled Pinned Locked Moved Unsolved General and Desktop
qsqltablemodelqtableviewqsqlrecord
4 Posts 3 Posters 531 Views 2 Watching
  • 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
    R-P-H
    wrote on 20 Jul 2023, 07:32 last edited by R-P-H
    #1

    Hi, I have a QSqlTableModel set on a QTableView. When adding or updating a row I do not want to allow empty text in a specific column.

    On submit, I would like a messagebox to show and the focus set back on the invalid cell.

    I haven't been able to find a simplified way to do this. I am aware of the beforeInsert() and beforeUpdate() signals where I could check if the data is valid, however I don't know how to stop the insertion/update if record is invalid.

    J A 2 Replies Last reply 20 Jul 2023, 07:50
    0
    • R R-P-H
      20 Jul 2023, 07:32

      Hi, I have a QSqlTableModel set on a QTableView. When adding or updating a row I do not want to allow empty text in a specific column.

      On submit, I would like a messagebox to show and the focus set back on the invalid cell.

      I haven't been able to find a simplified way to do this. I am aware of the beforeInsert() and beforeUpdate() signals where I could check if the data is valid, however I don't know how to stop the insertion/update if record is invalid.

      J Offline
      J Offline
      JonB
      wrote on 20 Jul 2023, 07:50 last edited by
      #2

      @R-P-H
      I think there are various possibilities depending on exactly what you want. Here are some suggestions:

      • For the specific case of "don't allow empty" you could place a validator on the generated edit widget.
      • To refuse a value for a certain index you could override setData() and return false.
      • To validate all data you could override submit()/submitAll() and have them return false. However with these I don't think you know which things have changed.
      • Editing from a QTableView is done via an item delegate. Normally it generates a suitable delegate widget. You can provide your own subclassed delegate (QStyledItemDelegate). From https://doc.qt.io/qt-6/qabstractitemdelegate.html you can implement your own setModelData() which refuses to submit the data to the model if it considers it invalid. This might be a suitable place to show the user a message box.
      1 Reply Last reply
      1
      • R R-P-H
        20 Jul 2023, 07:32

        Hi, I have a QSqlTableModel set on a QTableView. When adding or updating a row I do not want to allow empty text in a specific column.

        On submit, I would like a messagebox to show and the focus set back on the invalid cell.

        I haven't been able to find a simplified way to do this. I am aware of the beforeInsert() and beforeUpdate() signals where I could check if the data is valid, however I don't know how to stop the insertion/update if record is invalid.

        A Offline
        A Offline
        artwaw
        wrote on 20 Jul 2023, 08:18 last edited by
        #3

        @R-P-H Dive into model to verify data input from the view is unnecessary in this case. Validator is the way to go, like @JonB suggested.

        For more information please re-read.

        Kind Regards,
        Artur

        R 1 Reply Last reply 25 Jul 2023, 15:36
        0
        • A artwaw
          20 Jul 2023, 08:18

          @R-P-H Dive into model to verify data input from the view is unnecessary in this case. Validator is the way to go, like @JonB suggested.

          R Offline
          R Offline
          R-P-H
          wrote on 25 Jul 2023, 15:36 last edited by
          #4

          From what I've seen online, the QValidator approach requires subclassing QItemDelegate and it's setData() method. How is this different from the last point suggested ?

          1 Reply Last reply
          0
          • M Mark81 referenced this topic on 27 Nov 2024, 07:23

          1/4

          20 Jul 2023, 07:32

          • Login

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