PyQt5 tableView 双击事件

PyQt5 tableView 双击事件

由于业务需求,需要实现teableview 双击加载数据,找了很久都没找到办法,在网上搜到了一篇关于重写双击事件的方法,试过了还是不行,后来自己不断尝试终于解决,这里做个简单记录

        self.tableView.doubleClicked.connect(self.table_change)

    def table_change(self, index):
        row = index.row()
        match_id = self.model.data(self.model.index(row, 7))

目的是实现双击获取某一行的id,通过id去缓存找到对应数据,渲染着在另一个tableview
其中,需要考虑到清空表格数据

       self.tableView.doubleClicked.connect(self.table_change)

    def table_change(self, index):
        row = index.row()
        match_id = self.model.data(self.model.index(row, 7))
        try:
            detail_data = self.detail_data[match_id]
            detail_1x2 = detail_data['1x2']
            detail_ou = detail_data['ou']
            detail_ah = detail_data['ah']

            # 显示1x2 详细数据
            if detail_1x2 is not None:
                self.model11.clear()
                self.model12.clear()
                self.model13.clear()
                self.model11 = QStandardItemModel(0, 4)
                self.model11.setHorizontalHeaderLabels(['Company','1X2/풀타임\n1', '1X2/풀타임\nX', '1X2/풀타임\n2'])
                self.model12 = QStandardItemModel(0, 4)
                self.model12.setHorizontalHeaderLabels(['Company','1X2/전반전\n1', '1X2/전반전\nX', '1X2/전반전\n2'])
                self.model13 = QStandardItemModel(0, 4)
                self.model13.setHorizontalHeaderLabels(['Company','1X2/후반전\n1', '1X2/후반전\nX', '1X2/후반전\n2'])
                self.tableView_9.setModel(self.model12)
                self.tableView_10.setModel(self.model13)
                self.tableView_11.setModel(self.model11)
                self.tableView_9.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
                self.tableView_9.setEditTriggers(QAbstractItemView.NoEditTriggers)
                self.tableView_10.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
                self.tableView_10.setEditTriggers(QAbstractItemView.NoEditTriggers)
                self.tableView_11.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
                self.tableView_11.setEditTriggers(QAbstractItemView.NoEditTriggers)
                try:
                    for temp in detail_1x2[list(detail_1x2.keys())[0]]:
                        data_list = [temp['company']] + temp['score_text']
                        self.model11.appendRow([QStandardItem(str(i)) for i in data_list])
                except:
                    pass
                try:
                    for temp in detail_1x2[list(detail_1x2.keys())[1]]:
                        data_list = [temp['company']] + temp['score_text']
                        self.model12.appendRow([QStandardItem(str(i)) for i in data_list])
                except:
                    pass
                try:
                    for temp in detail_1x2[list(detail_1x2.keys())[2]]:
                        data_list = [temp['company']] + temp['score_text']
                        self.model13.appendRow([QStandardItem(str(i)) for i in data_list])
                except:
                    pass
            else:
                pass

你可能感兴趣的:(PyQt5,PyQt5,tableView,双击事件)