Results 1 to 6 of 6

Thread: On mouse click changing selection's background color in tableview

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Apr 2015
    Posts
    20
    Thanks
    7
    Qt products
    Platforms
    Unix/X11

    Default Re: On mouse click changing selection's background color in tableview

    Finally got some free time and taste for looking in to this again.

    As I said before, returning of the QBrush in data() only changes background color, while the color of the selected row stays the same as the "selection-background-color"
    so going for some passing of index and trying to find which row to have color change, well that seem waste of time (apart from learning experience) since we would never see the color change, it would be covered by the defauly highlight color




    Above is gif of changing qbrush color for everything. I got problem getting the default color of the background of the table... but solved it by just not applying any brush when I am not setting it
    Anyway, this code works on ubuntu and it does give me some control over colors, but not over selection/highlight color.
    It looks worse, less elegant than the stylesheet way that can be viewed in OP post in this thread, but still better than nothing, and this makes me appreciate how awesome stylesheets are.

    Qt Code:
    1. from PyQt5.QtCore import *
    2. from PyQt5.QtGui import *
    3. from PyQt5.QtWidgets import *
    4. import sys
    5.  
    6.  
    7. class My_Model_table(QAbstractTableModel):
    8. def __init__(self, table_data=[], parent=None):
    9. super().__init__()
    10. self.table_data = table_data
    11. self.color_enabled = False
    12. self.color_back = Qt.magenta # just something there
    13.  
    14. def rowCount(self, parent):
    15. return len(self.table_data)
    16.  
    17. def columnCount(self, parent):
    18. return 1
    19.  
    20. def data(self, index, role):
    21. if role == Qt.DisplayRole:
    22. value = self.table_data[index.row()]
    23. return value
    24. if role == Qt.TextAlignmentRole:
    25. return Qt.AlignCenter
    26.  
    27. if role == Qt.BackgroundRole and self.color_enabled:
    28. return QBrush(self.color_back)
    29.  
    30. def change_color(self, qt_color, color_enabled):
    31. self.layoutAboutToBeChanged.emit()
    32. self.color_enabled = color_enabled
    33. self.color_back = qt_color
    34. self.layoutChanged.emit()
    35.  
    36.  
    37. class My_table(QTableView):
    38. def __init__(self, parent=None):
    39. super().__init__()
    40. self.activated.connect(self.double_click_enter)
    41.  
    42. def double_click_enter(self, QModelIndex):
    43. QModelIndex.model().change_color(Qt.red, True)
    44.  
    45. self.alarm = QTimer()
    46. self.alarm.setSingleShot(True)
    47. self.alarm.timeout.connect(self.color_timeout)
    48. self.alarm.start(200)
    49.  
    50. def color_timeout(self):
    51. self.model().change_color(Qt.magenta, False)
    52.  
    53.  
    54. if __name__ == '__main__':
    55. app = QApplication(sys.argv)
    56. data = ['1', '2', '3', '4', '5']
    57. main_table = My_table()
    58. main_table.setModel(My_Model_table(data))
    59.  
    60. main_table.show()
    61. sys.exit(app.exec_())
    To copy to clipboard, switch view to plain text mode 
    Last edited by DoTheEvo; 15th February 2016 at 18:53.

  2. #2
    Join Date
    Apr 2015
    Posts
    20
    Thanks
    7
    Qt products
    Platforms
    Unix/X11

    Angry Re: On mouse click changing selection's background color in tableview

    OK, final edit since I figured it out when I started to play with qdarkstylesheet and noticed that everything worked nicely even on ubuntu...
    my mistake in original code was just using selection-background-color, when I use QTableView::item:selected:active everything now works perfectly



    Qt Code:
    1. from PyQt5.QtCore import *
    2. from PyQt5.QtGui import *
    3. from PyQt5.QtWidgets import *
    4. import sys
    5.  
    6.  
    7. class My_Model_table(QAbstractTableModel):
    8. def __init__(self, table_data=[], parent=None):
    9. super().__init__()
    10. self.table_data = table_data
    11.  
    12. def rowCount(self, parent):
    13. return len(self.table_data)
    14.  
    15. def columnCount(self, parent):
    16. return 1
    17.  
    18. def data(self, index, role):
    19. if role == Qt.DisplayRole:
    20. value = self.table_data[index.row()]
    21. return value
    22. if role == Qt.TextAlignmentRole:
    23. return Qt.AlignCenter
    24.  
    25.  
    26. class My_table(QTableView):
    27. def __init__(self, parent=None):
    28. super().__init__()
    29. self.activated.connect(self.double_click_enter)
    30.  
    31. def double_click_enter(self, QModelIndex):
    32. row = QModelIndex.row()
    33.  
    34. self.setStyleSheet(
    35. '''
    36. QTableView::item:selected:active {
    37. background: #ff0000;
    38. }
    39. '''
    40. )
    41.  
    42. self.alarm = QTimer()
    43. self.alarm.timeout.connect(self.row_color_back)
    44. self.alarm.setSingleShot(True)
    45. self.alarm.start(200)
    46. return
    47.  
    48. def row_color_back(self):
    49. self.setStyleSheet('')
    50.  
    51.  
    52. if __name__ == '__main__':
    53. app = QApplication(sys.argv)
    54. data = ['1', '2', '3', '4', '5']
    55. main_table = My_table()
    56. main_table.setModel(My_Model_table(data))
    57.  
    58. main_table.show()
    59. sys.exit(app.exec_())
    To copy to clipboard, switch view to plain text mode 

    /EDIT



    damn, and it does not work in my actual code because I use delegate to get html rich text to get bold emphasize
    oh how I hate it, I undertand very little and its always huge pain in the ass in getting it to done things without screwing something else
    heres my delegate if someone is guru with them
    Last edited by DoTheEvo; 15th February 2016 at 21:47.

Similar Threads

  1. Replies: 4
    Last Post: 3rd July 2014, 14:52
  2. Setting the background color of a header in TableView
    By sunilqt in forum Qt Programming
    Replies: 1
    Last Post: 13th April 2013, 13:06
  3. Replies: 6
    Last Post: 27th September 2012, 23:44
  4. changing background color in qvfb
    By John Douma in forum Qt for Embedded and Mobile
    Replies: 0
    Last Post: 31st March 2011, 19:30
  5. Replies: 1
    Last Post: 22nd February 2010, 09:38

Tags for this Thread

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.