Pyqt QAbstractTableModel remove all table rows



  • I'm using a QtableView and I set the model to be my subclassed QAbstractTableModel.
    I'm searching a database and I would like to present the results in the QtableView.
    Everything works but for each row the new search results are added to a new row in the table.
    For each search I would like to completely clear the QtableView/QAbstractTableModel and only present the latest search results.

    I have tried every possible example that I can find online but I cant figure it out.
    How can I completely clear the QtableView/QAbstractTableModel?

    Thanks

    Below is my subclassed QAbstractTableModel :
    @

    class SearchModel(QtCore.QAbstractTableModel):

    def __init__(self,searchWin,  entrys=[[]],headers=[], parent=None):
        QtCore.QAbstractTableModel.__init__(self,parent)
        
        self.entrys = entrys
        self.headers = headers
        self.searchWindow = searchWin
        
    
        
        
    #HAS TO BE IMPLEMENTED IN A MODEL  
    def data(self,index,role):
        
        if role == QtCore.Qt.DisplayRole:
            
            row = index.row()
            column = index.column()
            value = self.entrys[row][column]
            
            return value
        
        if role == QtCore.Qt.EditRole:
            row = index.row()
            column = index.column()
            return self.entrys[row][column]
        
        if role == QtCore.Qt.ToolTipRole:
            row = index.row()
            column = index.column()
            
            
            
            if self.searchWindow.searchPresetsOnly:
                
                tt = "click to load this preset"
            else:
                tt = str(self.entrys[row][column])
            
            return  tt
    
    #HAS TO BE IMPLEMENTED IN A MODEL  DATA FOR EACH ROWCOUNT
    def rowCount(self,parent):
        return len(self.entrys)
    
    def columnCount(self,parent):
        
        return len(self.entrys[0])
    
    def flags(self, index):
        return QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable
    
    
    def headerData(self, section, orientation, role):
        
        if role == QtCore.Qt.DisplayRole:
            
            
            
            if orientation == QtCore.Qt.Horizontal: 
                
                
                #print(section)
               
                return QtCore.QString(self.headers[section])
                
                
            else:
                return QtCore.QString("Select")
    

    @

    [Edit: Added @ tags around code -- mlong]


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.