Results 1 to 10 of 10

Thread: QTableView Repaint/Refresh

  1. #1
    Join Date
    Jul 2007
    Location
    NW Florida
    Posts
    16
    Thanks
    3
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default QTableView Repaint/Refresh

    I have a qtableview connected to my model and everything is working the way it is supposed to (well as far as I know). The only issue that I am having is a cosmetic one. Whenever I scroll or mouseover the qtableview the grdilines, and even the alternating row colors, seems to disappear. The issue also only arises if I am spanning rows and/or columns.

    Is there a method that I have to reimplement in the model to stop this from happening?

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QTableView Repaint/Refresh

    This could be a bug in Qt. Which exact version of Qt are you using?
    J-P Nurmi

  3. #3
    Join Date
    Jul 2007
    Location
    NW Florida
    Posts
    16
    Thanks
    3
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTableView Repaint/Refresh

    I am using v4.3.0

  4. #4
    Join Date
    Jul 2007
    Location
    NW Florida
    Posts
    16
    Thanks
    3
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTableView Repaint/Refresh

    Here is a screenshot to try and explain a little more what it is doing.
    Attached Images Attached Images

  5. #5
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QTableView Repaint/Refresh

    Does something minimal like this work properly?
    Qt Code:
    1. #include <QtGui>
    2.  
    3. int main(int argc, char* argv[])
    4. {
    5. QApplication a(argc, argv);
    6. QTableWidget t(3, 2);
    7. t.setSpan(0, 0, 1, 2);
    8. t.setSpan(1, 0, 2, 1);
    9. t.show();
    10. return a.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  6. #6
    Join Date
    Jul 2007
    Location
    NW Florida
    Posts
    16
    Thanks
    3
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTableView Repaint/Refresh

    yes that worked, but still not sure why mine is acting differently. Ill attach a portion of my model code just in case you may want to take a look at it at a deeper level.

    FYI though this is PyQT code, but the ideology is the same.

    Qt Code:
    1. class PyTableModel(QAbstractTableModel):
    2. def __init__(self, xmlData=None, numPasses=1):
    3. super(PyTableModel, self).__init__()
    4.  
    5. self.alternateColor_Header = True
    6. self.alternateColor_Data = True
    7.  
    8. self.lstPassItems = [...left out to save space...]
    9.  
    10. self.chkPassItems = [1]*len(self.lstPassItems)
    11. self.numPasses = numPasses*3
    12. self.lstPasses = range(numPasses)
    13. self.passData = [[[float('%.3F' % (random.randint(0.00, 1000.00)*random.random())),
    14. float('%.3F' % (random.randint(0.00, 1000.00)*random.random())),
    15. float('%.3F' % (random.randint(0.00, 1000.00)*random.random()))]]]
    16.  
    17. self.palette = QPalette()
    18.  
    19. self.initPassData()
    20.  
    21. print len(self.passData)
    22. print len(self.passData[0])
    23. print len(self.passData[0][0])
    24.  
    25. def initPassData(self):
    26. for x in range(self.numPasses/3):
    27. for y in range(len(self.lstPassItems)):
    28. self.passData[x].append([float('%.3F' % (random.randint(0.00, 1000.00)*random.random())),
    29. float('%.3F' % (random.randint(0.00, 1000.00)*random.random())),
    30. float('%.3F' % (random.randint(0.00, 1000.00)*random.random()))])
    31.  
    32. self.passData.append([])
    33.  
    34. def rowCount(self, index=QModelIndex()):
    35. return len(self.lstPassItems)+2
    36.  
    37. def columnCount(self, index=QModelIndex()):
    38. return self.numPasses+1
    39.  
    40. def data(self, index, role=Qt.DisplayRole):
    41. if 0 == index.row() and 0 == index.column():
    42. if role == Qt.DisplayRole:
    43. return QVariant('Pass')
    44. if role == Qt.TextAlignmentRole:
    45. return QVariant(Qt.AlignTop)
    46. if role == Qt.BackgroundColorRole:
    47. return QVariant(QColor(0,0,0))
    48. if role == Qt.FontRole:
    49. return QVariant(QFont('', 16, QFont.Bold))
    50. if role == Qt.ForegroundRole:
    51. return QVariant(QColor(255,255,255))
    52.  
    53. if 0 == index.row() and index.column() > 0:
    54. if index.column() % 3 == 1:
    55. self.alternateColor_Header = not self.alternateColor_Header
    56.  
    57. if role == Qt.DisplayRole:
    58. if 1 == (index.column()%3):
    59. return QVariant(self.lstPasses[index.column()/3]+1)
    60. else:
    61. return QVariant()
    62. if role == Qt.TextAlignmentRole:
    63. return QVariant(Qt.AlignCenter)
    64. if role == Qt.BackgroundColorRole:
    65. if self.alternateColor_Header:
    66. return QVariant(QColor(0,0,0))
    67. else:
    68. return QVariant(QColor(50,50,50))
    69. if role == Qt.FontRole:
    70. return QVariant(QFont('', 8, QFont.Bold))
    71. if role == Qt.TextColorRole:
    72. return QVariant(QColor(255,255,255))
    73.  
    74. if 1 == index.row() and (index.column() != 0 and 0 == (index.column()%3)):
    75. if role == Qt.DisplayRole:
    76. return QVariant('Tolerance')
    77. if role == Qt.TextAlignmentRole:
    78. return QVariant(Qt.AlignCenter)
    79. if role == Qt.BackgroundColorRole:
    80. return QVariant(self.palette.color(QPalette.Background).darker(200))
    81. if role == Qt.FontRole:
    82. return QVariant(QFont('', 8, QFont.Normal))
    83. elif 1 == index.row() and (index.column() != 0 and 1 == (index.column()%3)):
    84. if role == Qt.DisplayRole:
    85. return QVariant('Value')
    86. if role == Qt.TextAlignmentRole:
    87. return QVariant(Qt.AlignCenter)
    88. if role == Qt.BackgroundColorRole:
    89. return QVariant(self.palette.color(QPalette.Background).darker(200))
    90. if role == Qt.FontRole:
    91. return QVariant(QFont('', 8, QFont.Normal))
    92. elif 1 == index.row() and (index.column() != 0 and 2 == (index.column()%3)):
    93. if role == Qt.DisplayRole:
    94. return QVariant('Display')
    95. if role == Qt.TextAlignmentRole:
    96. return QVariant(Qt.AlignCenter)
    97. if role == Qt.BackgroundColorRole:
    98. return QVariant(self.palette.color(QPalette.Background).darker(200))
    99. if role == Qt.FontRole:
    100. return QVariant(QFont('', 8, QFont.Normal))
    101.  
    102. if 1 <= index.column() < self.numPasses+1 and 2 <= index.row():
    103. if (index.column()-1) % 3 == 0:
    104. self.alternateColor_Data = not self.alternateColor_Data
    105. if role == Qt.DisplayRole:
    106. return QVariant(self.passData[(index.column()-1)/3][index.row()-2][(index.column()-1)%3])
    107. if role == Qt.BackgroundColorRole:
    108. if self.alternateColor_Data:
    109. return QVariant(self.palette.color(QPalette.Background).darker(25))
    110. else:
    111. return QVariant(self.palette.color(QPalette.Background).darker(105))
    112. return QVariant()
    113.  
    114. item = self.lstPassItems[index.row()-2]
    115.  
    116. if 0 == index.column() and 2 <= index.row():
    117. if role == Qt.DisplayRole:
    118. return QVariant(item[0])
    119. if role == Qt.TextAlignmentRole:
    120. return QVariant(Qt.AlignLeft)
    121. if role == Qt.BackgroundColorRole:
    122. return QVariant(self.palette.color(QPalette.Background).darker(125))
    123. if role == Qt.FontRole:
    124. return QVariant(QFont('', 8, QFont.Normal))
    125. if role == Qt.ToolTipRole:
    126. return QVariant(item[1])
    127. if role == Qt.CheckStateRole:
    128. if self.chkPassItems[index.row()-2] == 1:
    129. return QVariant(Qt.Checked)
    130. else:
    131. return QVariant(Qt.Unchecked)
    132.  
    133. return QVariant()
    134.  
    135. def setData(self, index, value=QVariant(), role=Qt.EditRole):
    136. if index.isValid() and (index.column() == 0 and index.row() >= 2):
    137. if role == Qt.CheckStateRole:
    138. if self.chkPassItems[index.row()-2] == 0:
    139. self.chkPassItems[index.row()-2] = 1
    140. else:
    141. self.chkPassItems[index.row()-2] = 0
    142. if role == Qt.EditRole:
    143. try:
    144. if value.toString() != '':
    145. self.passData[(index.column()-1)/3][index.row()-2][(index.column()-1)%3] = float('%.3F' % float(value.toString()))
    146. print 'Pass Data:', self.passData[(index.column()-1)/3][index.row()-2][(index.column()-1)%3]
    147. print 'Value:', float('%.3F' % float(value.toString()))
    148. except ValueError, ve:
    149. pass
    150.  
    151. return True
    152.  
    153. def flags(self, index):
    154. if 0 == index.column() and 2 <= index.row():
    155. return Qt.ItemIsUserCheckable|Qt.ItemIsSelectable|Qt.ItemIsEnabled
    156. if 1 <= index.column() < self.numPasses+1:
    157. if 0 <= index.row() < 2:
    158. return Qt.ItemIsSelectable|Qt.ItemIsEnabled
    159. return Qt.ItemIsEditable|Qt.ItemIsEnabled|Qt.ItemIsSelectable
    160. else:
    161. if index.row() <= 1 >= index.column():
    162. return Qt.ItemIsEnabled
    163. return Qt.ItemIsSelectable|Qt.ItemIsEnabled
    164.  
    165. def headerData(self, section, orientation, role=Qt.DisplayRole):
    166. return QVariant()
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Jul 2007
    Location
    NW Florida
    Posts
    16
    Thanks
    3
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTableView Repaint/Refresh

    Also here is the class that I wrote that subclassed the QTableView.

    Qt Code:
    1. class PyTableView(QTableView):
    2. def __init__(self, parent=None):
    3. super(PyTableView, self).__init__(parent)
    4.  
    5. self.model = PyTableModel(numPasses=5)
    6.  
    7. self.setModel(self.model)
    8. self.resizeColumnToContents(0)
    9.  
    10. self.setSpan(0,0,2,1)
    11. for col in range(self.model.columnCount())[1:]:
    12. self.setSpan(0,((col*3)-3)+1,1,3)
    13.  
    14. self.horizontalHeader().hide()
    15. self.verticalHeader().hide()
    16.  
    17. self.setRowHeight(0,25)
    18. self.setRowHeight(1,25)
    19.  
    20. p = self.palette()
    21. p.setColor(QPalette.AlternateBase, p.color(QPalette.AlternateBase).darker(105))
    22.  
    23. for row in range(self.model.rowCount())[2:]:
    24. self.setRowHeight(row,20)
    25.  
    26. self.connect(self, SIGNAL('clicked(const QModelIndex &)'), self.testTable)
    27.  
    28. self.setAlternatingRowColors(True)
    29. self.setAutoFillBackground(True)
    30. self.setAutoScroll(True)
    31. self.setWindowTitle('PyTableView')
    32. self.resize(800,500)
    33. self.show()
    34.  
    35. def testTable(self, index):
    36. if index.column() == 0 and index.row() >= 2:
    37. self.model.setData(index, role=Qt.CheckStateRole)
    38. if index.column() > 0 and index.row() >= 2:
    39. self.model.setData(index)
    40. if index.column() > 0 and index.row() == 0:
    41. col_multiplier = int(self.model.data(self.model.index(index.row(), index.column()), Qt.DisplayRole).toString())
    42. print self.model.data(self.model.index(index.row(), index.column()), Qt.DisplayRole).toString()
    43. print '-'*50
    44. print 'Col:', index.column()*col_multiplier
    45. print '-'*50
    46.  
    47. def closeEvent(self, closeEvt):
    48. for row in range(self.model.rowCount(self.model.index(0,0)))[2:]:
    49. if self.model.data(self.model.index(row,0), Qt.CheckStateRole).toString() == '2':
    50. print row, self.model.data(self.model.index(row,0), Qt.DisplayRole).toString()
    To copy to clipboard, switch view to plain text mode 

  8. #8
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QTableView Repaint/Refresh

    Model's data() should at least check index validity. Also, you could also try temporarily disabling that palette adjustment.
    J-P Nurmi

  9. #9
    Join Date
    Jul 2007
    Location
    NW Florida
    Posts
    16
    Thanks
    3
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTableView Repaint/Refresh

    ill put the index validity checker in there, but I the problem was there before I inserted the palette code.

  10. #10
    Join Date
    Jul 2007
    Location
    NW Florida
    Posts
    16
    Thanks
    3
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTableView Repaint/Refresh

    inserted index.isValid() at the right spots, but still no go. im beginning to wonder if it has something to do with a dataChanged signal and/or a paintEvent.

Similar Threads

  1. Set height of QTableView to fit exact number of rows.
    By Ben.Hines in forum Qt Programming
    Replies: 3
    Last Post: 17th January 2019, 01:49
  2. QTableView sorting
    By gabriels in forum Qt Programming
    Replies: 11
    Last Post: 6th October 2010, 17:13
  3. QTableView Row Selection
    By ToddAtWSU in forum Qt Programming
    Replies: 8
    Last Post: 12th December 2007, 21:27
  4. QTableView paints too much
    By Jimmy2775 in forum Qt Programming
    Replies: 2
    Last Post: 26th July 2006, 18:42
  5. Multi-line messages in QTableView
    By Conel in forum Qt Programming
    Replies: 6
    Last Post: 13th April 2006, 13:49

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.