def __init__(self, auth, race, interval_secs=INTERVAL_SECS, parent=None): """Initialize the FieldStatisticsTable instance.""" super().__init__(parent=parent) self.setWindowTitle(' - '.join([self.WINDOW_TITLE, race['name']])) self.auth = auth field_list = get_field_list(self.auth, race) self.field_list_lock = threading.Lock() # Set up the table. self.setColumnCount(3) self.setAlternatingRowColors(True) self.setHorizontalHeaderLabels(['Field', 'Finished', 'Total']) self.horizontalHeader().setHighlightSections(False) self.horizontalHeader().setStretchLastSection(True) self.verticalHeader().setVisible(False) self.setSelectionMode(QAbstractItemView.NoSelection) self.clearContents() self.setRowCount(len(field_list)) # Qt warns us that inserting items while sorting is enabled will mess with the insertion # order, so disable sorting before populating the list, and then re-enable sorting # afterwards. self.setSortingEnabled(False) for row, field in enumerate(field_list): # Make our name item. Stash our field dict into this item to associate the field with # this row, even if the table gets resorted. item = QTableWidgetItem(field['name']) item.field = field self.setItem(row, self.NAME_COLUMN, item) # Make our finished item. item = QTableWidgetItem(0) self.setItem(row, self.FINISHED_COLUMN, item) # Make our total item. item = QTableWidgetItem(0) self.setItem(row, self.TOTAL_COLUMN, item) self.setSortingEnabled(True) self.sortByColumn(self.NAME_COLUMN, Qt.AscendingOrder) self.horizontalHeader().resizeSections(QHeaderView.ResizeToContents) self.read_settings() # Make our single-shot update timer. self.timer = QTimer(self) self.timer.setInterval(interval_secs * 1000) self.timer.setTimerType(Qt.VeryCoarseTimer) self.timer.setSingleShot(True) self.timer.timeout.connect(self.schedule_work) # Simulate a timer timeout so that we immediately schedule work. self.schedule_work()