我是PYQT的新手,我正试图感受它。我试图用5个不同的按钮填充表格小部件。我需要清除每个按钮之间的表格,但是想留下我的标题。我试过一个tablewidget clearcontents。但是这清除了一切,我不确定将它放在哪里,所以它会在每次按下按钮之前清除内容,从而带来新的信息。
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName(_fromUtf8("Form"))
Form.resize(761, 637)
self.pushButton = QtGui.QPushButton(Form)
self.pushButton.setGeometry(QtCore.QRect(0, 550, 201, 31))
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.pushButton.clicked.connect(self.get_output1_statement)
self.pushButton_2 = QtGui.QPushButton(Form)
self.pushButton_2.setGeometry(QtCore.QRect(0, 580, 201, 31))
self.pushButton_2.setObjectName(_fromUtf8("pushButton_2"))
self.pushButton.clicked.connect(self.get_output2_statement)
self.pushButton_3 = QtGui.QPushButton(Form)
self.pushButton_3.setGeometry(QtCore.QRect(0, 610, 201, 31))
self.pushButton_3.setObjectName(_fromUtf8("pushButton_3"))
self.pushButton_3.clicked.connect(self.get_output3_statement)
self.pushButton_4 = QtGui.QPushButton(Form)
self.pushButton_4.setGeometry(QtCore.QRect(250, 580, 201, 31))
self.pushButton_4.setObjectName(_fromUtf8("pushButton_4"))
self.pushButton.clicked.connect(self.get_output4_statement)
self.pushButton_5 = QtGui.QPushButton(Form)
self.pushButton_5.setGeometry(QtCore.QRect(510, 580, 201, 31))
self.pushButton_5.setObjectName(_fromUtf8("pushButton_5"))
self.pushButton.clicked.connect(self.get_output5_statement)
self.tableWidget = QtGui.QTableWidget(Form)
self.tableWidget.setGeometry(QtCore.QRect(0, 0, 741, 551))
sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding)
sizePolicy.setHorizontalStretch(5)
sizePolicy.setVerticalStretch(5)
sizePolicy.setHeightForWidth(self.tableWidget.sizePolicy().hasHeightForWidth())
self.tableWidget.setSizePolicy(sizePolicy)
self.tableWidget.setObjectName(_fromUtf8("tableWidget"))
self.tableWidget.setColumnCount(5)
self.tableWidget.setRowCount(50)
self.tableWidget.setItem(0,0,QtGui.QTableWidgetItem("name"))
self.tableWidget.setItem(0,1,QtGui.QTableWidgetItem("column_1"))
self.tableWidget.setItem(0,2,QtGui.QTableWidgetItem("column_2"))
self.tableWidget.setItem(0,3,QtGui.QTableWidgetItem("column_3"))
self.tableWidget.setItem(0,4,QtGui.QTableWidgetItem("column_4"))
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
Form.setWindowTitle(_translate("Form", "Form", None))
self.pushButton.setText(_translate("Form", "button1", None))
self.pushButton_2.setText(_translate("Form", "button2", None))
self.pushButton_3.setText(_translate("Form", "button3", None))
self.pushButton_4.setText(_translate("Form", "button4", None))
self.pushButton_5.setText(_translate("Form", "button5", None))
答案 0 :(得分:2)
当您以适当的方式设置表格标题时,可以使用clearContents()
方法。
变化:
self.tableWidget.setItem(0,0,QtGui.QTableWidgetItem("name"))
self.tableWidget.setItem(0,1,QtGui.QTableWidgetItem("column_1"))
self.tableWidget.setItem(0,2,QtGui.QTableWidgetItem("column_2"))
self.tableWidget.setItem(0,3,QtGui.QTableWidgetItem("column_3"))
self.tableWidget.setItem(0,4,QtGui.QTableWidgetItem("column_4"))
为:
self.tableWidget.setHorizontalHeaderItem(0, QtGui.QTableWidgetItem("name"))
self.tableWidget.setHorizontalHeaderItem(1, QtGui.QTableWidgetItem("column_1"))
self.tableWidget.setHorizontalHeaderItem(2, QtGui.QTableWidgetItem("column_2"))
self.tableWidget.setHorizontalHeaderItem(3, QtGui.QTableWidgetItem("column_3"))
self.tableWidget.setHorizontalHeaderItem(4, QtGui.QTableWidgetItem("column_4"))
编辑评论后的额外代码
也许这可以帮到你?我试着保留你的代码。
class Ui_Form(QtGui.QDialog):
def __init__(self, parent=None):
super(Ui_Form, self).__init__(parent)
self.setObjectName(_fromUtf8("Form"))
self.resize(761, 637)
self.pushButton = QtGui.QPushButton()
self.pushButton.setGeometry(QtCore.QRect(0, 550, 201, 31))
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.connect(self.pushButton, QtCore.SIGNAL("released()"), self.get_output1_statement)
self.tableWidget = QtGui.QTableWidget()
self.tableWidget.setGeometry(QtCore.QRect(0, 0, 741, 551))
sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding)
sizePolicy.setHorizontalStretch(5)
sizePolicy.setVerticalStretch(5)
sizePolicy.setHeightForWidth(self.tableWidget.sizePolicy().hasHeightForWidth())
self.tableWidget.setSizePolicy(sizePolicy)
self.tableWidget.setObjectName(_fromUtf8("tableWidget"))
self.tableWidget.setColumnCount(5)
self.tableWidget.setRowCount(50)
self.tableWidget.setHorizontalHeaderItem(0, QtGui.QTableWidgetItem("name"))
layout = QtGui.QVBoxLayout()
layout.addWidget(self.tableWidget)
layout.addWidget(self.pushButton)
self.setLayout(layout)
for i in range(1, 5):
self.tableWidget.setHorizontalHeaderItem(i, QtGui.QTableWidgetItem("column_{0}".format(i)))
self.retranslateUi()
def retranslateUi(self):
self.setWindowTitle(_translate("Form", "Form", None))
self.pushButton.setText(_translate("Form", "button1", None))
def get_output1_statement(self):
self.tableWidget.clearContents()
self.tableWidget.setItem(0,0,QtGui.QTableWidgetItem("add some data"))
app = QtGui.QApplication(sys.argv)
form = Ui_Form()
form.show()
app.exec_()