Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Issues with subclassing a QSqlTableModel
Forum Updated to NodeBB v4.3 + New Features

Issues with subclassing a QSqlTableModel

Scheduled Pinned Locked Moved Unsolved Qt for Python
7 Posts 3 Posters 887 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.
  • C Offline
    C Offline
    cibas
    wrote on 3 Mar 2021, 06:43 last edited by cibas 3 Mar 2021, 07:06
    #1

    Hello, I subclassed a QSqlTableModel (rowcount, columncount, data) but when I call the tableview.model().data(index.siblingAtColumn(0) an error occours (all the data are displayed correctly and the column 0 has never a NULL value, it contains an id)

    def data(self, index, role):
    if role == qtc.Qt.ForegroundRole:
    if index.column() == 3
    return qtg.QColor('red')
    else:
    return super().data(index, role)

    What am I doing wrong?

    E 1 Reply Last reply 3 Mar 2021, 07:13
    0
    • C cibas
      3 Mar 2021, 06:43

      Hello, I subclassed a QSqlTableModel (rowcount, columncount, data) but when I call the tableview.model().data(index.siblingAtColumn(0) an error occours (all the data are displayed correctly and the column 0 has never a NULL value, it contains an id)

      def data(self, index, role):
      if role == qtc.Qt.ForegroundRole:
      if index.column() == 3
      return qtg.QColor('red')
      else:
      return super().data(index, role)

      What am I doing wrong?

      E Offline
      E Offline
      eyllanesc
      wrote on 3 Mar 2021, 07:13 last edited by
      #2

      @cibas please provide a minimal and reproducible example

      If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

      1 Reply Last reply
      0
      • C Offline
        C Offline
        cibas
        wrote on 3 Mar 2021, 07:40 last edited by cibas 3 Mar 2021, 07:42
        #3

        I wrote the example code above (sorry for the bad indentation).
        Meanwhile I discovered that when I try to access at "self.tableView.model().data(index)" this is the error:

        data() missing 1 required positional argument: 'role'

        This is not happening with the super class QSqlTableWidget (I never passed the role parameter)

        class TableMagModel(QSqlTableModel):
        def init(self):
        super().init()

        def data(self, index, role):
            if role == Qt.ForegroundRole:
                if index.column() == 3:
                    return QColor('red')
            return QSqlTableModel.data(self, index, role)
        
        J 1 Reply Last reply 3 Mar 2021, 08:34
        0
        • C cibas
          3 Mar 2021, 07:40

          I wrote the example code above (sorry for the bad indentation).
          Meanwhile I discovered that when I try to access at "self.tableView.model().data(index)" this is the error:

          data() missing 1 required positional argument: 'role'

          This is not happening with the super class QSqlTableWidget (I never passed the role parameter)

          class TableMagModel(QSqlTableModel):
          def init(self):
          super().init()

          def data(self, index, role):
              if role == Qt.ForegroundRole:
                  if index.column() == 3:
                      return QColor('red')
              return QSqlTableModel.data(self, index, role)
          
          J Offline
          J Offline
          JonB
          wrote on 3 Mar 2021, 08:34 last edited by JonB 3 Mar 2021, 08:39
          #4

          @cibas
          I do not understand. You seem to quote an error message you receive, yet show code which does not cause the error to be raised?

          There is nothing wrong with self.tableView.model().data(index).

          However, you do not state whether you are using PyQt5, PyQt6, PySide2 or PySide6. Without that information it is not possible to give a definitive answer.

          On a separate note, in your subclass of QSqlTableModel where you define the __init__() method you would be better advised to define it with the extra optional parameters as per https://doc.qt.io/qtforpython-5/PySide2/QtSql/QSqlTableModel.html#PySide2.QtSql.PySide2.QtSql.QSqlTableModel.

          C 1 Reply Last reply 3 Mar 2021, 09:06
          0
          • J JonB
            3 Mar 2021, 08:34

            @cibas
            I do not understand. You seem to quote an error message you receive, yet show code which does not cause the error to be raised?

            There is nothing wrong with self.tableView.model().data(index).

            However, you do not state whether you are using PyQt5, PyQt6, PySide2 or PySide6. Without that information it is not possible to give a definitive answer.

            On a separate note, in your subclass of QSqlTableModel where you define the __init__() method you would be better advised to define it with the extra optional parameters as per https://doc.qt.io/qtforpython-5/PySide2/QtSql/QSqlTableModel.html#PySide2.QtSql.PySide2.QtSql.QSqlTableModel.

            C Offline
            C Offline
            cibas
            wrote on 3 Mar 2021, 09:06 last edited by cibas 3 Mar 2021, 09:08
            #5

            @JonB sorry I'm using PyQt5

            The error message I get is raised by the line self.tableView.model().data(index)

            With the class QSqlTableModel this line works properly but though I subclassed it (I wrote the code) that line raise me an error

            J 1 Reply Last reply 3 Mar 2021, 09:15
            0
            • C cibas
              3 Mar 2021, 09:06

              @JonB sorry I'm using PyQt5

              The error message I get is raised by the line self.tableView.model().data(index)

              With the class QSqlTableModel this line works properly but though I subclassed it (I wrote the code) that line raise me an error

              J Offline
              J Offline
              JonB
              wrote on 3 Mar 2021, 09:15 last edited by JonB 3 Mar 2021, 09:23
              #6

              @cibas
              OK, got it! This is because in your subclass you have chosen to write:

              def data(self, index, role):
              

              Here you have made the role parameter compulsory, because you do not supply a default value, hence the error message. This is the same sort of issue as I mentioned with your __init__() definition. You should be more careful to look at the method(s) you are overriding and declare your overload signature to be exactly the same as the base method you are overriding. Thus here

              class TableMagModel(QSqlTableModel):
                  def data(self, index, role=Qt.DisplayRole):
              

              which is how the base QSqlTableModel.data() is defined. Now the role parameter from the caller is optional, and will default to Qt.DisplayRole if not supplied.

              C 1 Reply Last reply 3 Mar 2021, 09:16
              2
              • J JonB
                3 Mar 2021, 09:15

                @cibas
                OK, got it! This is because in your subclass you have chosen to write:

                def data(self, index, role):
                

                Here you have made the role parameter compulsory, because you do not supply a default value, hence the error message. This is the same sort of issue as I mentioned with your __init__() definition. You should be more careful to look at the method(s) you are overriding and declare your overload signature to be exactly the same as the base method you are overriding. Thus here

                class TableMagModel(QSqlTableModel):
                    def data(self, index, role=Qt.DisplayRole):
                

                which is how the base QSqlTableModel.data() is defined. Now the role parameter from the caller is optional, and will default to Qt.DisplayRole if not supplied.

                C Offline
                C Offline
                cibas
                wrote on 3 Mar 2021, 09:16 last edited by
                #7

                @JonB Thank You!!!
                I will listen you advices

                1 Reply Last reply
                0

                1/7

                3 Mar 2021, 06:43

                • Login

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