Results 1 to 4 of 4

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

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Feb 2017
    Posts
    19
    Thanks
    5
    Qt products
    Qt5

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

    I 'm trying to change the color of rows in a QTableView which has a QSqlQueryModel as it's model.

    Here is a compilable code sample in python:
    Qt Code:
    1. import sys
    2. from PyQt4 import QtGui, QtCore, QtSql
    3.  
    4. def main():
    5. app = QtGui.QApplication(sys.argv)
    6. w = MyWindow()
    7. w.show()
    8. sys.exit(app.exec_())
    9.  
    10. class MyWindow(QtGui.QTableView):
    11. def __init__(self, *args):
    12. QtGui.QTableView.__init__(self, *args)
    13.  
    14. # connect to db (if doesn't exist, it's auto-created)
    15. self.db = QtSql.QSqlDatabase.addDatabase('QSQLITE')
    16. self.db.setDatabaseName('test.db')
    17. self.db.open()
    18.  
    19. #create a table in db and add some data
    20. query = QtSql.QSqlQuery()
    21. query.exec_("DROP TABLE IF EXISTS games")
    22. query.exec_("CREATE TABLE games(id INTEGER PRIMARY KEY, hometeam TEXT, visitorteam TEXT) ")
    23. query.exec("INSERT INTO games (hometeam, visitorteam) VALUES ('Star', 'Eagles')")
    24. query.exec("INSERT INTO games (hometeam, visitorteam) VALUES ('Best team', 'Reds');")
    25.  
    26. # set the model
    27. model = QtSql.QSqlQueryModel(self)#QtGui.QStandardItemModel(0, 2)
    28. self.setModel(model)
    29. model.setQuery("SELECT * FROM games")
    30.  
    31. # paint first two rows
    32. for i in range(0, 2):
    33. model.setData(model.index(i, 0), QtGui.QBrush(QtCore.Qt.red), QtCore.Qt.BackgroundRole)
    34. model.setData(model.index(i, 1), QtGui.QBrush(QtCore.Qt.red), QtCore.Qt.BackgroundRole)
    35.  
    36.  
    37. if __name__ == "__main__":
    38. main()
    To copy to clipboard, switch view to plain text mode 

    What am I doing wrong?

  2. #2
    Join Date
    Feb 2017
    Posts
    19
    Thanks
    5
    Qt products
    Qt5

    Default Re: Change color of a row of a QSqlQueryModel (QTableView)?

    I subclassed it:
    Qt Code:
    1. class ColorfullSqlQueryModel(QtSql.QSqlQueryModel):
    2. def __init__(self, dbcursor=None):
    3. super(ColorfullSqlQueryModel, self).__init__()
    4.  
    5. def data(self, QModelIndex, role=None):
    6. v = QtSql.QSqlQueryModel.data(self, QModelIndex, role);
    7. if role == QtCore.Qt.BackgroundRole:
    8. return QtGui.QColor(QtCore.Qt.yellow)
    9. return (v);
    To copy to clipboard, switch view to plain text mode 
    It works!! Turns all rows to yellow!
    But how can I make it paint only the rows I want?
    How can I pass the color to function data?

  3. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,330
    Thanks
    317
    Thanked 871 Times in 858 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Change color of a row of a QSqlQueryModel (QTableView)?

    But how can I make it paint only the rows I want?
    How can I pass the color to function data?
    You can't pass any additional parameters to the data() method. If you need to set the color on an item-by-item basis, then you need to look at the row and column of the QModelIndex argument. Unless you can decide based on the row and column alone, generally you will need to implement an extra data structure in your custom model where you can look up the color for the index.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Change SqlQueryModel QTableView's Row Color
    By toadybarker in forum Qt Programming
    Replies: 9
    Last Post: 17th September 2014, 18:03
  2. Replies: 7
    Last Post: 25th July 2013, 21:47
  3. Replies: 7
    Last Post: 21st May 2013, 22:17
  4. how to change text color in QTableView?
    By phillip_Qt in forum Qt Programming
    Replies: 2
    Last Post: 28th April 2008, 10:03
  5. QTableView change color of current Cell
    By raphaelf in forum Qt Programming
    Replies: 4
    Last Post: 4th March 2006, 11:22

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.