Qtablewidget操作,设置表头,填充内容,删除⾏,获取⾏内其它元素PyQt表格操作
qtablewidget添加表头
qtablewidget添加Button
qtablewidget添加内容
qtablewidget获取⾏内其它元素
qtablewidget删除⾏
代码⽰例
UI⽂件:UI_main.py
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return anslate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return anslate(context, text, disambig)
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
self.tableWidget = QtGui.alWidget)
self.tableWidget.setRowCount(10)
self.tableWidget.setColumnCount(7)
self.tableWidget.setObjectName(_fromUtf8("tableWidget"))
MainWindow.alWidget)
tSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
业务代码⽂件:main.py
# -*- coding: utf-8 -*-
from PyQt4.QtGui import QMainWindow, QColor, QPushButton, QTableWidgetItem
from PyQt4.QtCore import QTextCodec, Qt
from Ui_main import Ui_MainWindow
from PyQt4 import QtGui
class MainWindow(QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
QMainWindow.__init__(self, parent)
# 加载UI
self.setupUi(self)
# 填充内容
self.set_content()
# 设置表头
def set_table_header(self, header_list):
"""设置table表头"""
self.tableWidget.clear()
self.tableWidget.setColumnCount(len(header_list))
self.tableWidget.setRowCount(20)
# 表格展⽰
self.tableWidget.setHorizontalHeaderLabels(header_list)
# 设置最后⼀列⾃动拉伸
self.tableWidget.horizontalHeader().setStretchLastSection(True)
self.tableWidget.verticalHeader().setVisible(True)
# 设置默认排序
self.tableWidget.sortItems(0, Qt.AscendingOrder)
# 设置表头颜⾊
for x in range(lumnCount()):
headItem = self.tableWidget.horizontalHeaderItem(x)
headItem.setTextColor(QColor(200, 111, 30))
headItem.setBackgroundColor(QColor(200, 200, 200))
def set_content(self):
"""填充数据"""
data = [
{'id': "1", 'desc': '第⼀条数据!'},
{'id': "2", 'desc': '第⼆条数据!'},
{'id': "3", 'desc': '第三条数据!'},
{'id': "4", 'desc': '第四条数据!'}
]
self.set_table_header(['删除', '启⽤', '禁⽤', 'id', '描述'])
for row, i in enumerate(data):
btn_del = QPushButton("删除")
btn_del.id = i["id"]
btn_t(self.delete_slot)
btn_inact = QPushButton("禁⽤")
btn_inact.id = i["id"]
btn_t(self.inactive_slot)
btn_act = QPushButton("启⽤")
btn_act.id = i["id"]
btn_t(self.active_slot)
id_item = QTableWidgetItem(i["id"])
btn_del.item = id_item
self.tableWidget.setCellWidget(row, 0, btn_del)
self.tableWidget.setCellWidget(row, 1, btn_inact)
self.tableWidget.setCellWidget(row, 2, btn_act)
self.tableWidget.setItem(row, 3, id_item)
self.tableWidget.setItem(row, 4, QTableWidgetItem(i["desc"]))
def delete_slot(self):
sender = self.sender()
# 删除⾏,关键点
veRow(w(sender.item))
def inactive_slot(self):
sender = self.sender()
sender.setEnabled(False)
for btn in self.tableWidget.findChildren(QPushButton):
if btn.id == sender.id () == "启⽤":
btn.setEnabled(True)
def active_slot(self):
sender = self.sender()
delete删除表格还是内容sender.setEnabled(False)
for btn in self.tableWidget.findChildren(QPushButton):
if btn.id == sender.id () == "禁⽤":
btn.setEnabled(True)
if __name__ == "__main__":
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
QTextCodec.decForName("utf-8")) app = QtGui.QApplication(sys.argv)
ui = MainWindow()
ui.show()
<_())
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论