Python Forum
Change color of a row of a QTableView?
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Change color of a row of a QTableView?
#2
I changed the 'QtCore.Qt.red' to 'QBrush(Qt.red)' and works!

Here is a small working example:

from PyQt4 import QtGui, QtCore
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys

def main():
   app = QApplication(sys.argv)
   w = MyWindow()
   w.show()
   sys.exit(app.exec_())

class MyWindow(QTableView):
   def __init__(self, *args):
       QTableView.__init__(self, *args)

       model = QtGui.QStandardItemModel(0, 2)
       self.setModel(model)

       for i in range(0,6):
           newRow = model.rowCount();
           model.insertRow(newRow);

       # paint first two rows
       for i in range(0, 2):
           model.setData(model.index(i, 0), QBrush(Qt.red), QtCore.Qt.BackgroundRole)
           model.setData(model.index(i, 1), QBrush(Qt.red), QtCore.Qt.BackgroundRole)


if __name__ == "__main__":
   main()
But I do have a question:
when I comment out the line 'from PyQt4.QtCore import *', I get an error 'Qt unresolved reference' even though I have the line 'from PyQt4 import QtGui, QtCore'.
So, what is the proper way to import QtCore without having to import it twice?
Reply


Messages In This Thread
Change color of a row of a QTableView? - by panoss - May-24-2017, 05:21 PM
RE: Change color of a row of a QTableView? - by panoss - May-25-2017, 06:44 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyQt] PyQt5 QTableView SQLite : edit cell HeinKurz 2 2,483 Mar-27-2023, 10:41 AM
Last Post: HeinKurz
  [PyQt] QStyledItemDelegate and QTableView malonn 1 1,692 Feb-07-2023, 07:15 PM
Last Post: malonn
  [PyQt] QTableView set header labels HeinKurz 2 7,052 Jan-23-2023, 08:46 AM
Last Post: HeinKurz
  [PyQt] Determine whether text in QTableView cell is fully visible or not random_nick 0 1,017 Oct-27-2022, 09:29 PM
Last Post: random_nick
  [PyQt] QTableView: scroll to top cell of the screen random_nick 2 2,918 Oct-08-2022, 12:29 AM
Last Post: random_nick
  [PyQt] [Solved]Add a Blank Row To QTableView Extra 3 5,680 Oct-02-2022, 04:53 PM
Last Post: Extra
  [PyQt] [Solved]Change text color of one line in TextBrowser Extra 2 4,995 Aug-23-2022, 09:11 PM
Last Post: Extra
  Tkinter - How can I change the default Notebook border color? TurboC 5 14,884 May-23-2022, 03:44 PM
Last Post: bigmac
Question [Tkinter] Change Treeview column color? water 3 9,753 Mar-04-2022, 11:20 AM
Last Post: Larz60+
  Can't get tkinter button to change color based on changes in data dford 4 3,488 Feb-13-2022, 01:57 PM
Last Post: dford

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020