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. Change color of a row of a QSqlQueryModel (QTableView)?
Qt 6.11 is out! See what's new in the release blog

Change color of a row of a QSqlQueryModel (QTableView)?

Scheduled Pinned Locked Moved Unsolved General and Desktop
27 Posts 3 Posters 19.4k 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.
  • P Offline
    P Offline
    Panoss
    wrote on last edited by
    #18

    Yes, you 're right VRonin:

    tableIter = self.m_extraRoles.constFind(hashKey)
    

    This shows no underlined code in Pychrm (if there were any errors, it would be underlined. With red color.)

    But this look very hard to convert to python:

    Q_ASSERT(index.model()==this)
    

    Q_ASSERT?? omg

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #19

      You can actually remove that line altogether, it's just a check that is done only in debug mode

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      0
      • P Offline
        P Offline
        Panoss
        wrote on last edited by Panoss
        #20

        Only one (!!) line is left:

        emit dataChanged(index,index,QVector<int>(1,role));
        

        Translated to:

        self.dataChanged.emit(index,index,QVector(int((1,role)))
        

        But there is no QVector in Python.
        (I mean it doesn't exist in any library like PyQt4.QtCore)

        1 Reply Last reply
        0
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by
          #21

          Leave it with just self.dataChanged.emit(index,index) then. the last one is an optional parameter and even Qt's native models don't use it

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          1 Reply Last reply
          0
          • P Offline
            P Offline
            Panoss
            wrote on last edited by Panoss
            #22

            I set for source of the proxy model (ExtraRolesProxyModel) a QSqlQueryModel.
            But I get an error:
            'tableIter = self.m_extraRoles.constFind(hashKey)
            AttributeError: 'ExtraRolesProxyModel' object has no attribute 'm_extraRoles'

            1 Reply Last reply
            0
            • P Offline
              P Offline
              Panoss
              wrote on last edited by
              #23

              And this is the whole code (of the class):

              class ExtraRolesProxyModel(QIdentityProxyModel ):
                  def __init__(self, dbcursor=None):
                      super(ExtraRolesProxyModel, self).__init__()        
                  def data(self, index, role):
                      hashKey = index.row() << 32 | index.column()
                      tableIter = self.m_extraRoles.constFind(hashKey)
                      if tableIter==self.m_extraRoles.constEnd:
                          return QtSql.QSqlQueryModel.data(index,role);
                      roleIter = tableIter.value().constFind(role)
                      if roleIter==tableIter.value().constEnd():
                          return QtSql.QSqlQueryModel.data(index,role);
                      return roleIter.value()
              
                  def setData(self, index, value, role=None):
                      if not index.isValid():
                          return False
                      hashKey = index.row() << 32 | index.column()
                      if value:
                          self.m_extraRoles[hashKey][role] = value
                          self.dataChanged.emit(index,index)
                          return True;
                      tableIter = self.m_extraRoles.find(hashKey)
                      if tableIter== self.m_extraRoles.end():
                          return False
                      roleIter = tableIter.value().find(role)
                      if roleIter==tableIter.value().end():
                          return False
                      tableIter.value().erase(roleIter)
                      if tableIter.value().isEmpty():
                          self.m_extraRoles.erase(tableIter)
                      self.dataChanged.emit(index, index)
                      return True
              
              VRoninV 1 Reply Last reply
              0
              • P Panoss

                And this is the whole code (of the class):

                class ExtraRolesProxyModel(QIdentityProxyModel ):
                    def __init__(self, dbcursor=None):
                        super(ExtraRolesProxyModel, self).__init__()        
                    def data(self, index, role):
                        hashKey = index.row() << 32 | index.column()
                        tableIter = self.m_extraRoles.constFind(hashKey)
                        if tableIter==self.m_extraRoles.constEnd:
                            return QtSql.QSqlQueryModel.data(index,role);
                        roleIter = tableIter.value().constFind(role)
                        if roleIter==tableIter.value().constEnd():
                            return QtSql.QSqlQueryModel.data(index,role);
                        return roleIter.value()
                
                    def setData(self, index, value, role=None):
                        if not index.isValid():
                            return False
                        hashKey = index.row() << 32 | index.column()
                        if value:
                            self.m_extraRoles[hashKey][role] = value
                            self.dataChanged.emit(index,index)
                            return True;
                        tableIter = self.m_extraRoles.find(hashKey)
                        if tableIter== self.m_extraRoles.end():
                            return False
                        roleIter = tableIter.value().find(role)
                        if roleIter==tableIter.value().end():
                            return False
                        tableIter.value().erase(roleIter)
                        if tableIter.value().isEmpty():
                            self.m_extraRoles.erase(tableIter)
                        self.dataChanged.emit(index, index)
                        return True
                
                VRoninV Offline
                VRoninV Offline
                VRonin
                wrote on last edited by VRonin
                #24

                @Panoss said in Change color of a row of a QSqlQueryModel (QTableView)?:

                AttributeError: 'ExtraRolesProxyModel' object has no attribute 'm_extraRoles'

                You have to declare it in the __init__ method (not sure how you deal with templates in python though)


                @Panoss said in Change color of a row of a QSqlQueryModel (QTableView)?:

                if tableIter==self.m_extraRoles.constEnd:

                Missing ()


                if value:

                Does it work?

                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                ~Napoleon Bonaparte

                On a crusade to banish setIndexWidget() from the holy land of Qt

                1 Reply Last reply
                0
                • P Offline
                  P Offline
                  Panoss
                  wrote on last edited by Panoss
                  #25

                  I made the suggested corrections:

                  if tableIter==self.m_extraRoles.constEnd():
                  

                  And:

                  if value.isValid():
                  

                  But I don't knw how to declare the m_extraRoles, so I declared it as list, which obviously, is incorrect.

                  class ExtraRolesProxyModel(QIdentityProxyModel ):
                      def __init__(self, dbcursor=None):
                          super(ExtraRolesProxyModel, self).__init__()
                          self.m_extraRoles = []
                  

                  (the '= []' means 'define it as list' )

                  1 Reply Last reply
                  0
                  • VRoninV Offline
                    VRoninV Offline
                    VRonin
                    wrote on last edited by VRonin
                    #26

                    you probably have to use a dict. Let's try this:

                    I do not know python so consider this just a guideline

                    class ExtraRolesProxyModel(QIdentityProxyModel ):
                        def __init__(self, dbcursor=None):
                            super(ExtraRolesProxyModel, self).__init__()
                            self.m_extraRoles=dict()
                        def data(self, index, role):
                            hashKey = QByteArray()
                            keyStream = QDataStream(hashKey,QIODevice::WriteOnly)
                            keyStream.writeInt32(index.row())
                            keyStream.writeInt32(index.column())
                            keyStream.writeInt32(role)
                            if self.m_extraRoles.has_key(hashKey):
                                return self.m_extraRoles[hashKey]
                            return QtSql.QSqlQueryModel.data(index,role)            
                    
                        def setData(self, index, value, role=None):
                            if not index.isValid():
                                return False
                            hashKey = QByteArray()
                            keyStream = QDataStream(hashKey,QIODevice::WriteOnly)
                            keyStream.writeInt32(index.row())
                            keyStream.writeInt32(index.column())
                            keyStream.writeInt32(role)
                            if value.isValid():
                                self.m_extraRoles[hashKey]=value
                                self.dataChanged.emit(index,index)
                                return True
                            if self.m_extraRoles.has_key(hashKey):
                                del self.m_extraRoles[hashKey]
                                self.dataChanged.emit(index,index)
                                return True
                            return False
                            
                    

                    "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                    ~Napoleon Bonaparte

                    On a crusade to banish setIndexWidget() from the holy land of Qt

                    1 Reply Last reply
                    0
                    • P Offline
                      P Offline
                      Panoss
                      wrote on last edited by Panoss
                      #27
                      if self.m_extraRoles.has_key(hashKey):
                      

                      AttributeError: 'dict' object has no attribute 'has_key'

                      But I see that dict does have an attributte named 'has_key'.

                      EDIT: in fact, it's been removed since python 3.
                      So I replaced:

                      if self.m_extraRoles.has_key(hashKey):
                      

                      with:

                      if hashKey not in self.m_extraRoles:
                      

                      But now I get another error:
                      return self.m_extraRoles[hashKey]
                      KeyError: PyQt4.QtCore.QByteArray(b'\x00\x00\x00\x04\x00\x00\x00\x02\x00\x00\x00\x08')

                      KeyError? What is this now?

                      EDIT:
                      I think hashKey has to be converted from QString to string.
                      I tried this:

                      hashKey = unicode([PyQt4.QtCore.QString(u'hashKey')])
                      

                      But this line returns me an error:
                      AttributeError: 'module' object has no attribute 'QString'

                      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