import sys
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWidgets import QWidget
from PyQt5.QtWidgets import QTableWidget
from PyQt5.QtWidgets import QTableWidgetItem
from PyQt5.QtWidgets import QAbstractItemView
from PyQt5.QtWidgets import QPushButton
from qtpy.QtCore import Qt
class testWindow(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.table = QTableWidget(self)
self.table.move(20, 20)
self.table.setColumnCount(3)
self.table.setFixedHeight(300)
self.table.setFixedWidth(500)
self.table.setSelectionBehavior(QAbstractItemView.SelectRows)#设置表格的选取方式是行选取
self.table.setSelectionMode(QAbstractItemView.SingleSelection)#设置选取方式为单个选取
self.table.setHorizontalHeaderLabels(["标记ID", "标记名称", "标记初始坐标"]) #设置行表头
self.table.verticalHeader().setVisible(False)#隐藏列表头
self.table_insert()
self.table.itemChanged.connect(self.table_update)
self.delete_button = QPushButton(self)
self.delete_button.move(230, 350)
self.delete_button.setFixedWidth(100)
self.delete_button.setFixedHeight(32)
self.delete_button.clicked.connect(self.table_delete)
self.delete_button.setText("Delete")
self.setGeometry(200, 200, 570, 400)
self.show()
#insert,只是简单插入一个固定数据
def table_insert(self):
row = self.table.rowCount()
self.table.insertRow(row)
item_id = QTableWidgetItem("1")
item_id.setFlags(Qt.ItemIsSelectable | Qt.ItemIsEnabled) # 设置物件的状态为只可被选择(未设置可编辑)
item_name = QTableWidgetItem("door") #我们要求它可以修改,所以使用默认的状态即可
item_pos = QTableWidgetItem("(1,2)")
item_pos.setFlags(Qt.ItemIsSelectable | Qt.ItemIsEnabled) # 设置物件的状态为只可被选择
self.table.setItem(row, 0, item_id)
self.table.setItem(row, 1, item_name)
self.table.setItem(row, 2, item_pos)
#以下可以加入保存数据到数据的操作
#update
def table_update(self):
row_select = self.table.selectedItems()
if len(row_select) == 0:
return
id = row_select[0].text()
new_name = row_select[1].text()
print("id: {}, save_name: {}".format(id,new_name))
# 以下可以加入保存数据到数据的操作
'''
eg. update {table} set name = "new_name" where id = "id"
'''
#delete
def table_delete(self):
row_select = self.table.selectedItems()
if len(row_select) == 0:
return
id = row_select[0].text()
print("id: {}".format(id))
row = row_select[0].row()
self.table.removeRow(row)
# 以下可以加入保存数据到数据的操作
'''
eg. delete from {table} where id = "id"
'''
if __name__ == "__main__":
app = QApplication(sys.argv)
main_window = testWindow()
sys.exit(app.exec_())
QT文档上说,每当QTableWidget中的一个部件(item)内容更改完毕的时候,会释放itemChanged信号,即我们更改door内容后,会释放这个信号,然后将它接入槽函数table_update,进行后续操作
获取修改成功!