简体   繁体   中英

PyQt5 QTableView Updating view on button click

How do I get my QTableView to update when a button is pushed? Utilizing pandas dataframe and standard setup as source of data for table. I understand that it's not (addWidget(self.view) is only called once), but what do I need to change to have it update? (print(data) in code confirms the dataframe is being updated)

class QModel(QAbstractTableModel):

  def __init__(self, data):
    QAbstractTableModel.__init__(self)
    self._data = data

  def rowCount(self, parent=None):
    return self._data.shape[0]

  def columnCount(self, parent=None):
    return self._data.shape[1]

  def data(self, index, role=Qt.DisplayRole):
    if index.isValid():
        if role == Qt.DisplayRole:
            return str(self._data.iloc[index.row(), index.column()])
    return None

  def headerData(self, col, orientation, role):
    if orientation == Qt.Horizontal and role == Qt.DisplayRole:
        return self._data.columns[col]
    return None

class Tabs_Widget(QWidget):
    
    def __init__(self):
    
        super().__init__()
        
        self.setWindowTitle('Adjustments')
        
        self.df = pd.DataFrame()
        
        self.add = QPushButton('Add Line')
        self.add.clicked.connect(self.addLine)
        
        self.viewmodel = self.tableView(self.df)

        
        self.layout = QVBoxLayout(self)
        self.layout.addWidget(self.add)
        self.layout.addWidget(self.view)
        
        self.show()
        
    def addLine(self, items):
        self.df = self.df.append([[1,2]])
#        print(self.df)
        self.tableView(self.df)
        
    def tableView(self, data):
        
        print("data")
        print(data)
        model = QModel(data)
        self.view = QTableView()
        
        self.view.setModel(model)
        self.view.setAlternatingRowColors(True)
        self.view.setSizeAdjustPolicy(QTableView.AdjustToContents)        
        
if __name__ == '__main__':
    app = 0
    app = QApplication(sys.argv)
    win = Tabs_Widget()
    sys.exit(app.exec_())
class Tabs_Widget(QWidget):

def __init__(self):

    super().__init__()
    
    self.setWindowTitle('Adjustments')
    
    self.df = pd.DataFrame()
    
    self.model = QModel(self.df)
    self.view = QTableView()
    
    self.view.setModel(self.model)
    self.view.setAlternatingRowColors(True)
    self.view.setSizeAdjustPolicy(QTableView.AdjustToContents)
    
    self.add = QPushButton('Add Line')
    self.add.clicked.connect(self.addLine)
    
    self.layout = QVBoxLayout(self)
    self.layout.addWidget(self.add)
    self.layout.addWidget(self.view)
    
    self.show()
    
def addLine(self, items):
    self.df = self.df.append([[1,2]])
    self.view.setModel(QModel(self.df))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM