将新列添加到QTableWidget

时间:2018-08-11 15:50:36

标签: c++ qt qtablewidget

每当按下按钮时,我需要向现有QTableWidget添加新列,例如:

ui->tableWidget->addColumn();

2 个答案:

答案 0 :(得分:0)

我能找到的最好方法是:

ui->tableWidget->setColumnCount(ui->tableWidget->horizontalHeader()->count() + 1);

答案 1 :(得分:0)

        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)