TreeView only updates when mouse is moved over it
-
I'm trying to add a search functionality to my tree view which highlights entries that match the search term from a text field. I have implemented this by using my tree views item model data method like this:
def data(self, index, role=None): ... # If background role then highlight if searched or editable if role == QtCore.Qt.BackgroundRole: if self.currentSearchTerm != "": if self.currentSearchTerm in item.itemData.name: return QBrush(QColor(255, 255, 130))
I update the currentSearchTerm like this:
def onSearchBarCharTyped(self): text = self.modelTreeSearchBar.toPlainText().lower() self.model.setCurrentSearchTerm(text) self.update()
For some reason this doesn't update the colors immediately when a letter is typed into the searchbar textfield. The colors only update when the mouse is moved over the tree view. I have also tried calling the repaint method but it doesn't do anything to solve the issue either. Is there maybe something I need to call on the model to make it update the colors? Any ideas?
-
I'm trying to add a search functionality to my tree view which highlights entries that match the search term from a text field. I have implemented this by using my tree views item model data method like this:
def data(self, index, role=None): ... # If background role then highlight if searched or editable if role == QtCore.Qt.BackgroundRole: if self.currentSearchTerm != "": if self.currentSearchTerm in item.itemData.name: return QBrush(QColor(255, 255, 130))
I update the currentSearchTerm like this:
def onSearchBarCharTyped(self): text = self.modelTreeSearchBar.toPlainText().lower() self.model.setCurrentSearchTerm(text) self.update()
For some reason this doesn't update the colors immediately when a letter is typed into the searchbar textfield. The colors only update when the mouse is moved over the tree view. I have also tried calling the repaint method but it doesn't do anything to solve the issue either. Is there maybe something I need to call on the model to make it update the colors? Any ideas?
@IDontKnowHowToCode
The model has to tell the view when data changes, only then does the view calldata()
to redraw. Because changing the search term could change the background of any item (right?) you need toemit dataChanged(..., QtCore.Qt.BackgroundRole)
. You probably need to do this for the whole table range, unless you want to do extra work to keep updating down.Moving the mouse over the treeview happens to cause a repaint/re-evaluation, hence why you see that.