PyQt - Implement a QAbstractTableModel for display in QTableView

前端 未结 3 999
盖世英雄少女心
盖世英雄少女心 2021-02-07 08:48

I would like to display a pandas data frame in a PyQt table. I have made some progress with this, but have not been able to correctly derive the Table Model class. Any help with

3条回答
  •  名媛妹妹
    2021-02-07 09:11

    This is probably your problem:

    def rowCount(self, parent=QtCore.QModelIndex()):
        if type(self.datatable) == pd.DataFrame:
        ...
    
    
    def columnCount(self, parent=QtCore.QModelIndex()):
        if (self.datatable) == pd.DataFrame:
        ...
    

    You set your datatable to a QTableWidget in dataFrameToQtTable, so it can't be a pd.DataFrame, your methods will always return 0.

    Without the type check, you would have caught the problem immediately. Do you really want to silently ignore all cases where your type doesn't match (better let it raise an error if it doesn't follow the same interface you're expecting)? Typechecks are in most cases unnecessary.

提交回复
热议问题