python - 在表格的每一行旁边添加按钮

标签 python python-3.x pyqt pyqt5 qtablewidget

目前我所做的是将按钮添加到第一列的表格中。但我想要做的是将按钮添加到表格的列之外,而是将其添加到表格的每一行旁边。

目前我是这样做的:

# initialize a table somehow
table = QTableWidget(parent)
table.setRowCount(3)
table.setColumnCount(5)
table.setHorizontalHeaderLabels(['', 'Field 1', 'Field 2', 'Field 3', 'Field N'])
table.verticalHeader().hide()

# create an cell widget
for index in range(table.rowCount()):
    btn = QPushButton(table)
    btn.setText('add')
    table.setCellWidget(index, 0, btn)

enter image description here

我怎样才能做到这一点(见下图): enter image description here

最佳答案

您可以向垂直标题添加按钮:

from PyQt5 import QtCore, QtGui, QtWidgets


class ButtonHeaderView(QtWidgets.QHeaderView):
    def __init__(self, parent):
        super().__init__(QtCore.Qt.Vertical, parent)
        self.m_buttons = []
        self.sectionResized.connect(self.adjustPositions)
        self.sectionCountChanged.connect(self.onSectionCountChanged)
        self.parent().horizontalScrollBar().valueChanged.connect(self.adjustPositions)

    @QtCore.pyqtSlot()
    def onSectionCountChanged(self):
        while self.m_buttons:
            button = self.m_buttons.pop()
            button.deleteLater()
        for i in range(self.count()):
            button = QtWidgets.QPushButton(self)
            button.setCursor(QtCore.Qt.ArrowCursor)
            self.m_buttons.append(button)
            self.update_data()
            self.adjustPositions()

    def setModel(self, model):
        super().setModel(model)
        if self.model() is not None:
            self.model().headerDataChanged.connect(self.update_data)

    def update_data(self):
        for i, button in enumerate(self.m_buttons):
            text = self.model().headerData(i, self.orientation(), QtCore.Qt.DisplayRole)
            button.setText(str(text))

    def updateGeometries(self):
        super().updateGeometries()
        self.adjustPositions()

    @QtCore.pyqtSlot()
    def adjustPositions(self):
        w = 0
        for index, button in enumerate(self.m_buttons):
            geom = QtCore.QRect(
                0,
                self.sectionViewportPosition(index),
                button.height(),
                self.sectionSize(index),
            )
            w = max(w, button.height())
            geom.adjust(0, 2, 0, -2)
            button.setGeometry(geom)
        self.setFixedWidth(w)


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    w = QtWidgets.QTableWidget(3, 4)
    header = ButtonHeaderView(w)
    w.setVerticalHeader(header)
    w.setHorizontalHeaderLabels(["Field 1", "Field 2", "Field 3", "Field N"])

    header_buttons = []
    for i in range(w.columnCount()):
        header_button = "+"
        header_buttons.append(header_button)
    w.setVerticalHeaderLabels(header_buttons)

    w.resize(320, 240)
    w.show()
    sys.exit(app.exec_())

关于python - 在表格的每一行旁边添加按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57489001/

相关文章:

python - 如何自动拉伸(stretch) QTableView 列并保持它们可调整

python - tensorflow 中 numpy.linalg.pinv 的替代方案

python - 如何使用不确定条或等价物创建 3D 曲面图?

python - cv2::imshow 的参数

Python 类型系统——对象与类型

python - 如何使用 Python 检测图形的转折点

python - 让 pyqt 使用 cocoa 小部件

python - 放大鼠标位置 QGraphicsView

python - 在 python shell 中用点向前和向后打印文本 'Loading'

python - Numpy 高效大矩阵乘法