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. How to set row color in tableview with qsqltablemodel ?
Forum Updated to NodeBB v4.3 + New Features

How to set row color in tableview with qsqltablemodel ?

Scheduled Pinned Locked Moved Unsolved Qt for Python
pyside2qt for pythonpythonpyside
5 Posts 3 Posters 1.1k 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
    Cedric.Niu
    wrote on last edited by
    #1

    Pyside2 has not qvariant, this code only return the color and not data. Thanks!

    class TableModel:
    
        def __init__(self):
            super().__init__()
            self.DB = QSqlDatabase.addDatabase("QSQLITE")
            self.DB.setDatabaseName(r"test.db")
    
            self.query = QSqlQuery(self.DB)
    
            self.tabModel = QSqlTableModel()
            self.tabModel.setTable("TotalTable")
            self.tabModel.data = self.data
            self.tabModel.select()
    
            self.selModel = QItemSelectionModel(self.tabModel)
    
        def data(self, index, role=Qt.DisplayRole):
            if role == Qt.BackgroundRole:
                if index.row() % 2 == 0:
                    return QBrush(Qt.yellow)
                else:
                    return QBrush(Qt.red)
    
    1 Reply Last reply
    0
    • C Offline
      C Offline
      Cedric.Niu
      wrote on last edited by
      #2

      I have solved this issue. But I think It is not perfect. I hope that user can right click the tableview and set the row color.

      class CustomSqlModel(QSqlTableModel):
      
          def data(self, index, role=Qt.DisplayRole):
              if role == Qt.BackgroundRole:
                  if index.row() in [1]:
                      return QBrush(Qt.red)
                  elif index.row() in [2]:
                      return QBrush(Qt.green)
                  elif index.row() in [3]:
                      return QBrush(Qt.blue)
                  elif index.row() in [4]:
                      return QBrush(Qt.yellow)
              return QSqlTableModel.data(self, index, role)
      
      JonBJ 1 Reply Last reply
      0
      • CristianMaureiraC Offline
        CristianMaureiraC Offline
        CristianMaureira
        wrote on last edited by
        #3

        mmm it's not clear to me what you want to achieve. Do you mind explaining a bit more what is the final goal? it seems I'm missing something from the "right click and change color"

        C 1 Reply Last reply
        2
        • C Cedric.Niu

          I have solved this issue. But I think It is not perfect. I hope that user can right click the tableview and set the row color.

          class CustomSqlModel(QSqlTableModel):
          
              def data(self, index, role=Qt.DisplayRole):
                  if role == Qt.BackgroundRole:
                      if index.row() in [1]:
                          return QBrush(Qt.red)
                      elif index.row() in [2]:
                          return QBrush(Qt.green)
                      elif index.row() in [3]:
                          return QBrush(Qt.blue)
                      elif index.row() in [4]:
                          return QBrush(Qt.yellow)
                  return QSqlTableModel.data(self, index, role)
          
          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @Cedric-Niu said in How to set row color in tableview with qsqltablemodel ?:

          I hope that user can right click the tableview and set the row color.

          As @CristianMaureira has said, it is not clear what you mean here, and it is unrelated to what went before. If you do want to do this, you are going to have to store somewhere what row(s) the user has clicked on and what color(s) they selected for the row(s).

          1 Reply Last reply
          0
          • CristianMaureiraC CristianMaureira

            mmm it's not clear to me what you want to achieve. Do you mind explaining a bit more what is the final goal? it seems I'm missing something from the "right click and change color"

            C Offline
            C Offline
            Cedric.Niu
            wrote on last edited by Cedric.Niu
            #5

            @CristianMaureira @JonB

            Sorry...

            I mean

            42941a4f-1ef9-4e18-8363-f93753759167-image.png

            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