向 QTableWidget 添加新列

Add new column to a QTableWidget

我需要在按下按钮时向现有 QTableWidget 添加一个新列,例如:

ui->tableWidget->addColumn();

我能找到的最佳方法是:

ui->tableWidget->setColumnCount(ui->tableWidget->horizontalHeader()->count() + 1);
        column_index = self.table_name.columnCount()
        self.table_name.setRowCount(1)
        self.table_name.setColumnCount(column_index + 1)

        self.tb_widget = QWidget()

        picture = "C:\Users\usa\Desktop\test.png"
        pixmap = QPixmap(picture).scaled(QSize(160, 90))
        tb_item_img = QLabel()
        tb_item_img.setPixmap(pixmap)
        # tb_item_img.setFixedSize(160, 90)
        tb_item_time = QLabel()
        tb_item_time.setText("00:00:00")

        self.tb_item_btb.setProperty('time', tb_item_time.text())
        self.tb_item_btb.clicked.connect(self.clickTableCut)

        self.table_name.setColumnWidth(column_index, 160)
        self.table_name.setRowHeight(0, 90)
        self.table_name.horizontalHeader().setSectionResizeMode(column_index, QtWidgets.QHeaderView.Fixed)

        self.table_name.setCellWidget(0, column_index, self.tb_widget)

        # click
        def clickTableCut(self, ):
            button = self.sender()
            if button:
                t = button.property('time')
                print(t)
                column_index = self.table_name.indexAt(button.pos()).column()
                # if remove an item
                self.table_name.removeColumn(column_index)