如何通过双击为添加的行编辑 qtablewidget 中水平标题的标签?我从这里获取代码并对其进行了调整,但它不会更改添加列的名称。我正在使用 python 3.6 和 pyqt5。from PyQt5 import QtWidgetsimport sysfrom PyQt5.QtWidgets import QApplication, QWidget, QInputDialog, QLineEditclass MyWindow(QtWidgets.QWidget): def __init__(self, parent=None): super(MyWindow, self).__init__(parent) self.table = QtWidgets.QTableWidget(5,5) self.table.setHorizontalHeaderLabels(['1', '2', '3', '4', '5']) self.table.setVerticalHeaderLabels(['1', '2', '3', '4', '5']) self.table.horizontalHeader().sectionDoubleClicked.connect(self.changeHorizontalHeader) self.button_add_c = QtWidgets.QPushButton('add column', self) self.button_add_c.clicked.connect(self.click_button_add_c) layout = QtWidgets.QHBoxLayout() layout.addWidget(self.table) layout.addWidget(self.button_add_c) self.setLayout(layout) def changeHorizontalHeader(self, index): oldHeader = self.table.horizontalHeaderItem(index).text() newHeader, okPressed = QInputDialog.getText(self,' Change header label for column %d', "Your name:", QLineEdit.Normal, oldHeader) if okPressed: self.table.horizontalHeaderItem(index).setText(newHeader) def click_button_add_c(self): culPosition = self.table.columnCount() self.table.insertColumn(culPosition)if __name__ == '__main__': app = QtWidgets.QApplication(sys.argv) main = MyWindow() main.show() sys.exit(app.exec_())
添加回答
举报
0/150
提交
取消