Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. TreeView only updates when mouse is moved over it
Forum Updated to NodeBB v4.3 + New Features

TreeView only updates when mouse is moved over it

Scheduled Pinned Locked Moved Unsolved Qt for Python
2 Posts 2 Posters 301 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    IDontKnowHowToCode
    wrote on last edited by
    #1

    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?

    JonBJ 1 Reply Last reply
    0
    • I IDontKnowHowToCode

      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?

      JonBJ Online
      JonBJ Online
      JonB
      wrote on last edited by JonB
      #2

      @IDontKnowHowToCode
      The model has to tell the view when data changes, only then does the view call data() to redraw. Because changing the search term could change the background of any item (right?) you need to emit 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.

      1 Reply Last reply
      2

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved