from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import sys
def __init__(self, table_data=[], parent=None):
super().__init__()
self.table_data = table_data
self.color_enabled = False
self.color_back = Qt.magenta # just something there
def rowCount(self, parent):
return len(self.table_data)
def columnCount(self, parent):
return 1
def data(self, index, role):
if role == Qt.DisplayRole:
value = self.table_data[index.row()]
return value
if role == Qt.TextAlignmentRole:
return Qt.AlignCenter
if role == Qt.BackgroundRole and self.color_enabled:
return QBrush(self.
color_back)
def change_color(self, qt_color, color_enabled):
self.layoutAboutToBeChanged.emit()
self.color_enabled = color_enabled
self.color_back = qt_color
self.layoutChanged.emit()
def __init__(self, parent=None):
super().__init__()
self.activated.connect(self.double_click_enter)
self.alarm.setSingleShot(True)
self.alarm.timeout.connect(self.color_timeout)
self.alarm.start(200)
def color_timeout(self):
self.model().change_color(Qt.magenta, False)
if __name__ == '__main__':
data = ['1', '2', '3', '4', '5']
main_table = My_table()
main_table.setModel(My_Model_table(data))
main_table.show()
sys.exit(app.exec_())
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import sys
class My_Model_table(QAbstractTableModel):
def __init__(self, table_data=[], parent=None):
super().__init__()
self.table_data = table_data
self.color_enabled = False
self.color_back = Qt.magenta # just something there
def rowCount(self, parent):
return len(self.table_data)
def columnCount(self, parent):
return 1
def data(self, index, role):
if role == Qt.DisplayRole:
value = self.table_data[index.row()]
return value
if role == Qt.TextAlignmentRole:
return Qt.AlignCenter
if role == Qt.BackgroundRole and self.color_enabled:
return QBrush(self.color_back)
def change_color(self, qt_color, color_enabled):
self.layoutAboutToBeChanged.emit()
self.color_enabled = color_enabled
self.color_back = qt_color
self.layoutChanged.emit()
class My_table(QTableView):
def __init__(self, parent=None):
super().__init__()
self.activated.connect(self.double_click_enter)
def double_click_enter(self, QModelIndex):
QModelIndex.model().change_color(Qt.red, True)
self.alarm = QTimer()
self.alarm.setSingleShot(True)
self.alarm.timeout.connect(self.color_timeout)
self.alarm.start(200)
def color_timeout(self):
self.model().change_color(Qt.magenta, False)
if __name__ == '__main__':
app = QApplication(sys.argv)
data = ['1', '2', '3', '4', '5']
main_table = My_table()
main_table.setModel(My_Model_table(data))
main_table.show()
sys.exit(app.exec_())
To copy to clipboard, switch view to plain text mode
Bookmarks