荐 pyqt5 tableWidget入门,和treewidget联动功能
程序员文章站
2022-05-30 22:58:12
1.成品最近在做项目的过程中需要用到QTreeWidget和QTableWidget联动,上一篇文章介绍了 QTreeWidget的基本用法,这里介绍一下QTableWidget的基本用法,及我在项目中的使用。先看看最终成品。要实现以上图片中的功能,需要在上一篇文中加上QTableWidget。所以先介绍下QTableWidget。2. QTableWidget类QTableWidget类中的常用方法如下表:方 法描 述setRowCount(int row)设置Q...
1.成品
最近在做项目的过程中需要用到QTreeWidget和QTableWidget联动,上一篇文章介绍了 QTreeWidget的基本用法,这里介绍一下QTableWidget的基本用法,及我在项目中的使用。先看看最终成品。
要实现以上图片中的功能,需要在上一篇文中加上QTableWidget。所以先介绍下QTableWidget。
2. QTableWidget类
QTableWidget类中的常用方法如下表:
方 法 | 描 述 |
---|---|
setRowCount(int row) | 设置QTableWidget表格控件行数 |
setColumnCount(int col) | 设置QTableWidget表格控件列数 |
setHorizontalHeaderLabels() | 设置QTableWidget表格控件的水平标签 |
setVeriticalHeaderLabels() | 设置QTableWidget表格控件的垂直标签 |
setItem(int, int, QTableWidgetItem) | 在QTableWidget表格控件的每个选项的单元空间里添加控件 |
horizontalHeader | 获取QTableWidget表格控件的表格头,以便执行隐藏 |
rowCount() | 获取QTableWidget表格控件的行数 |
columnCount() | 获取QTableWidget表格控件的列数 |
setEditTriggers(EditTriggers triggers) | 设置表格是否可编辑。设置编辑规则的枚举值 |
setSelectionBehavior | 设置表格的选择行为 |
setTextAlignment | 设置表格内文字的对齐方式 |
setSpan(int row, int column, int rowSpanCount, int columnSpanCount) | 合并单元格,要改变单元格的第row行第column列,要合并rowSpanCount行数和columnSpanCount列数。 ·row: 要改变的单元格行数 ·column要改变的单元格列数 ·rowSpanCount:需要合并的行数 ·columnSpanCount:需要合并的列数 |
setShowGrid() | 在默认情况下,表格的显示是有网格线的。 · True:显示网格线 · False:不显示网格线 |
setColumnWidth(int column, int width) | 设置单元格的宽度 |
setRowHeight(int row, int height) | 设置单元格的高度 |
编辑规则中的枚举类型:
选 项 | 值 | 描述 |
---|---|---|
QAbstractItemView:NoEditTriggers0No | 0 | 不能对表格内容进行修改 |
QAbstractItemView:CurrentChanged1Editing | 1 | 任何时候都能对单元格进行修改 |
QAbstractItemView:DoubleClicked2Editing | 2 | 双击单元格 |
QAbstractItemView:SelectedClicked4Editing | 4 | 单击已选中的内容 |
QAbstractItemView:EditKeyPressed8Editing | 8 | 当修改键被按下时修改单元格 |
QAbstractItemView:AnyKeyPressed16Editing | 16 | 按任意键修改单元格 |
QAbstractItemView:AllEditTriggers31Editing | 31 | 包括以上所有条件 |
表格的选择行为的枚举值类型:
选 项 | 值 | 描述 |
---|---|---|
QAbstractItemView.SelectItems0Selecting | 0 | 选中单个单元格 |
QAbstractItemView.SelectRows1Selecting | 1 | 选中一行 |
QAbstractItemView.SelectColumns2Selecting | 2 | 选中一列 |
单元格文本的水平对齐方式:
选 项 | 描 述 |
---|---|
Qt.AlignLeft | 将单元格的内容沿单元格的左边缘对齐 |
Qt.AlignRight | 将单元格的内容沿单元格的右边缘对齐 |
Qt.AlignHCenter | 在可用空间中,居中显示在水平向上 |
Qt.AlignJustify | 将文本在可用空间中对齐,默认是从左到右 |
单元格文本的垂直对齐方式:
选 项 | 描 述 |
---|---|
Qt.AlignTop | 与顶部对齐 |
Qt.AlignBottom | 与底部对齐 |
Qt.AlignVCenter | 在可用空间中,居中显示在垂直方向上 |
Qt.AlignBaseline | 与基线对齐 |
如果要设置水平和垂直对齐方式,比如在表格空间内上下、左右居中对齐,那么只要使用Qt.AlignHCenter和Qt.AlignVCenter即可。
3. 代码实现
在上一篇文章的基础上加上QTableWidget类
class TableWidgetClass(QWidget):
def __init__(self, tablewidget):
# noinspection PyArgumentList
super(TableWidgetClass, self).__init__()
self.tableWidget = None
self.initui(tablewidget)
self.twrowcnt = 0
def initui(self, tablewidget):
# layout = QHBoxLayout()
self.tableWidget = tablewidget
self.tableWidget.setColumnCount(4)
self.tableWidget.setRowCount(0)
# 设置头label
self.tableWidget.setHorizontalHeaderLabels(['测试用例', '执行时间', '结果', '备注'])
# 设置列宽度
self.tableWidget.setColumnWidth(0, 200)
self.tableWidget.setColumnWidth(1, 130)
self.tableWidget.setColumnWidth(2, 100)
self.tableWidget.setColumnWidth(3, 400)
# 优化3 将表格变为禁止编辑
self.tableWidget.setEditTriggers(QAbstractItemView.NoEditTriggers)
# 优化 4 设置表格整行选中
self.tableWidget.setSelectionBehavior(QAbstractItemView.SelectRows)
def table_append(self, str_info):
if str_info == 'CCO测试项' or str_info == 'STA测试项' or str_info == '协议一致性' \
or str_info == '通信性能测试' or str_info == 'CCO性能测试项' or str_info == 'STA性能测试项':
return
item = self.tableWidget.findItems(str_info, Qt.MatchExactly)
if not item:
row = self.tableWidget.rowCount()
self.tableWidget.insertRow(row)
newitem = QTableWidgetItem(str_info)
self.tableWidget.setItem(row, 0, newitem)
def table_remove(self, str_info):
item = self.tableWidget.findItems(str_info, Qt.MatchExactly)
if item:
row = item[0].row()
self.tableWidget.removeRow(row)
全部的实现代码如下,同样分为2个文件:
py1 name: treewidget.py
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'treewidget.ui'
#
# Created by: PyQt5 UI code generator 5.13.0
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(1299, 594)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.horizontalLayout = QtWidgets.QHBoxLayout(self.centralwidget)
self.horizontalLayout.setObjectName("horizontalLayout")
self.treeWidget = QtWidgets.QTreeWidget(self.centralwidget)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Minimum)
sizePolicy.setHorizontalStretch(1)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.treeWidget.sizePolicy().hasHeightForWidth())
self.treeWidget.setSizePolicy(sizePolicy)
self.treeWidget.setObjectName("treeWidget")
self.treeWidget.headerItem().setText(0, "1")
self.horizontalLayout.addWidget(self.treeWidget)
self.tableWidget = QtWidgets.QTableWidget(self.centralwidget)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
sizePolicy.setHorizontalStretch(2)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.tableWidget.sizePolicy().hasHeightForWidth())
self.tableWidget.setSizePolicy(sizePolicy)
self.tableWidget.setObjectName("tableWidget")
self.tableWidget.setColumnCount(0)
self.tableWidget.setRowCount(0)
self.horizontalLayout.addWidget(self.tableWidget)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 1299, 23))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
py2 name: main.py
# -*- coding: utf-8 -*-
import sys
from PyQt5 import QtWidgets, QtCore
from PyQt5.QtWidgets import *
from PyQt5.QtCore import Qt
from treewidget import Ui_MainWindow
class AllCertCaseValue:
ROOT_PROTOCON = 0
# STA 协议一致性所有case
ROOT_PROTOCON_STA_CHILD = 1
# sta scan tmi band0/1/2/3
ROOT_PROTOCON_STA_TMISCAN_B0 = 2
ROOT_PROTOCON_STA_TMISCAN_B1 = 3
ROOT_PROTOCON_STA_TMISCAN_B2 = 4
ROOT_PROTOCON_STA_TMISCAN_B3 = 5
# sta tonemask band0/1/2/3
ROOT_PROTOCON_STA_TM_B0 = 6
ROOT_PROTOCON_STA_TM_B1 = 7
ROOT_PROTOCON_STA_TM_B2 = 8
ROOT_PROTOCON_STA_TM_B3 = 9
ROOT_PROTOCON_STA_MAX = ROOT_PROTOCON_STA_TM_B3 + 1
# CCO 协议一致性所有case
ROOT_PROTOCON_CCO_CHILD = 40
# cco scan tmi band0/1/2/3
ROOT_PROTOCON_CCO_TMISCAN_B0 = 41
ROOT_PROTOCON_CCO_TMISCAN_B1 = 42
ROOT_PROTOCON_CCO_TMISCAN_B2 = 43
ROOT_PROTOCON_CCO_TMISCAN_B3 = 44
# sta tonemask band0/1/2/3
ROOT_PROTOCON_CCO_TM_B0 = 45
ROOT_PROTOCON_CCO_TM_B1 = 46
ROOT_PROTOCON_CCO_TM_B2 = 47
ROOT_PROTOCON_CCO_TM_B3 = 48
ROOT_PROTOCON_CCO_MAX = ROOT_PROTOCON_CCO_TM_B3 + 1
# 通信性能测试
ROOT_PERFORMANCE_CHILD = 80
ROOT_PERFORMANCE_STA_CHILD = 81
# white noise
ROOT_PERFORMANCE_STA_WN_B1 = 82
ROOT_PERFORMANCE_STA_WN_B2 = 83
# anti-ppm
ROOT_PERFORMANCE_STA_ANTIPPM_B1 = 84
ROOT_PERFORMANCE_STA_ANTIPPM_B2 = 85
# anti-attenuation
ROOT_PERFORMANCE_STA_ANTIATT_B1 = 86
ROOT_PERFORMANCE_STA_ANTIATT_B2 = 87
# anti-narrowband
ROOT_PERFORMANCE_STA_ANTINARROW_B1 = 88
ROOT_PERFORMANCE_STA_ANTINARROW_B2 = 89
# anti-pulse
ROOT_PERFORMANCE_STA_ANTIPULSE_B1 = 90
ROOT_PERFORMANCE_STA_ANTIPULSE_B2 = 91
# psd
ROOT_PERFORMANCE_STA_PSD_B1 = 92
ROOT_PERFORMANCE_STA_PSD_B2 = 93
# sta rate
ROOT_PERFORMANCE_STA_RATE_B1 = 94
ROOT_PERFORMANCE_STA_RATE_B2 = 95
ROOT_PERFORMANCE_STA_MAX = ROOT_PERFORMANCE_STA_RATE_B2 + 1
ROOT_PERFORMANCE_CCO_CHILD = 100
# white noise
ROOT_PERFORMANCE_CCO_WN_B1 = 101
ROOT_PERFORMANCE_CCO_WN_B2 = 102
# anti-ppm
ROOT_PERFORMANCE_CCO_ANTIPPM_B1 = 103
ROOT_PERFORMANCE_CCO_ANTIPPM_B2 = 104
# anti-attenuation
ROOT_PERFORMANCE_CCO_ANTIATT_B1 = 105
ROOT_PERFORMANCE_CCO_ANTIATT_B2 = 106
# anti-narrowband
ROOT_PERFORMANCE_CCO_ANTINARROW_B1 = 107
ROOT_PERFORMANCE_CCO_ANTINARROW_B2 = 108
# anti-pulse
ROOT_PERFORMANCE_CCO_ANTIPULSE_B1 = 109
ROOT_PERFORMANCE_CCO_ANTIPULSE_B2 = 110
# psd
ROOT_PERFORMANCE_CCO_PSD_B1 = 111
ROOT_PERFORMANCE_CCO_PSD_B2 = 112
# CCO rate
ROOT_PERFORMANCE_CCO_RATE_B1 = 113
ROOT_PERFORMANCE_CCO_RATE_B2 = 114
ROOT_PERFORMANCE_CCO_MAX = ROOT_PERFORMANCE_CCO_RATE_B2 + 1
ROOT_OTHER_CHILD = 130
ROOT_OTHER_RATE = 131
ROOT_OTHER_MAX = ROOT_OTHER_RATE + 1
# max
TREE_MAX = ROOT_OTHER_MAX + 1
DictCommandInfo = {
"协议一致性": AllCertCaseValue.ROOT_PROTOCON,
# STA test case
"STA测试项": AllCertCaseValue.ROOT_PROTOCON_STA_CHILD,
"TMI遍历 STA band0": AllCertCaseValue.ROOT_PROTOCON_STA_TMISCAN_B0,
"TMI遍历 STA band1": AllCertCaseValue.ROOT_PROTOCON_STA_TMISCAN_B1,
"TMI遍历 STA band2": AllCertCaseValue.ROOT_PROTOCON_STA_TMISCAN_B2,
"TMI遍历 STA band3": AllCertCaseValue.ROOT_PROTOCON_STA_TMISCAN_B3,
"ToneMask测试 STA band0": AllCertCaseValue.ROOT_PROTOCON_STA_TM_B0,
"ToneMask测试 STA band1": AllCertCaseValue.ROOT_PROTOCON_STA_TM_B1,
"ToneMask测试 STA band2": AllCertCaseValue.ROOT_PROTOCON_STA_TM_B2,
"ToneMask测试 STA band3": AllCertCaseValue.ROOT_PROTOCON_STA_TM_B3,
# CCO test case
"CCO测试项": AllCertCaseValue.ROOT_PROTOCON_CCO_CHILD,
"TMI遍历 CCO band0": AllCertCaseValue.ROOT_PROTOCON_CCO_TMISCAN_B0,
"TMI遍历 CCO band1": AllCertCaseValue.ROOT_PROTOCON_CCO_TMISCAN_B1,
"TMI遍历 CCO band2": AllCertCaseValue.ROOT_PROTOCON_CCO_TMISCAN_B2,
"TMI遍历 CCO band3": AllCertCaseValue.ROOT_PROTOCON_CCO_TMISCAN_B3,
"ToneMask测试 CCO band0": AllCertCaseValue.ROOT_PROTOCON_CCO_TM_B0,
"ToneMask测试 CCO band1": AllCertCaseValue.ROOT_PROTOCON_CCO_TM_B1,
"ToneMask测试 CCO band2": AllCertCaseValue.ROOT_PROTOCON_CCO_TM_B2,
"ToneMask测试 CCO band3": AllCertCaseValue.ROOT_PROTOCON_CCO_TM_B3,
# communication performance
"通信性能测试": AllCertCaseValue.ROOT_PERFORMANCE_CHILD,
"STA性能测试项": AllCertCaseValue.ROOT_PERFORMANCE_STA_CHILD,
"白噪性能 STA band1": AllCertCaseValue.ROOT_PERFORMANCE_STA_WN_B1,
"白噪性能 STA band2": AllCertCaseValue.ROOT_PERFORMANCE_STA_WN_B2,
"抗频偏性能 STA band1": AllCertCaseValue.ROOT_PERFORMANCE_STA_ANTIPPM_B1,
"抗频偏性能 STA band2": AllCertCaseValue.ROOT_PERFORMANCE_STA_ANTIPPM_B2,
"抗衰减性能 STA band1": AllCertCaseValue.ROOT_PERFORMANCE_STA_ANTIATT_B1,
"抗衰减性能 STA band2": AllCertCaseValue.ROOT_PERFORMANCE_STA_ANTIATT_B2,
"抗窄带性能 STA band1": AllCertCaseValue.ROOT_PERFORMANCE_STA_ANTINARROW_B1,
"抗窄带性能 STA band2": AllCertCaseValue.ROOT_PERFORMANCE_STA_ANTINARROW_B2,
"抗脉冲性能 STA band1": AllCertCaseValue.ROOT_PERFORMANCE_STA_ANTIPULSE_B1,
"抗脉冲性能 STA band2": AllCertCaseValue.ROOT_PERFORMANCE_STA_ANTIPULSE_B2,
"功率频谱密度 STA band1": AllCertCaseValue.ROOT_PERFORMANCE_STA_PSD_B1,
"功率频谱密度 STA band2": AllCertCaseValue.ROOT_PERFORMANCE_STA_PSD_B2,
"STA 速率测试 band1": AllCertCaseValue.ROOT_PERFORMANCE_STA_RATE_B1,
"STA 速率测试 band2": AllCertCaseValue.ROOT_PERFORMANCE_STA_RATE_B2,
"CCO性能测试项": AllCertCaseValue.ROOT_PERFORMANCE_CCO_CHILD,
"白噪性能 CCO band1": AllCertCaseValue.ROOT_PERFORMANCE_CCO_WN_B1,
"白噪性能 CCO band2": AllCertCaseValue.ROOT_PERFORMANCE_CCO_WN_B2,
"抗频偏性能 CCO band1": AllCertCaseValue.ROOT_PERFORMANCE_CCO_ANTIPPM_B1,
"抗频偏性能 CCO band2": AllCertCaseValue.ROOT_PERFORMANCE_CCO_ANTIPPM_B2,
"抗衰减性能 CCO band1": AllCertCaseValue.ROOT_PERFORMANCE_CCO_ANTIATT_B1,
"抗衰减性能 CCO band2": AllCertCaseValue.ROOT_PERFORMANCE_CCO_ANTIATT_B2,
"抗窄带性能 CCO band1": AllCertCaseValue.ROOT_PERFORMANCE_CCO_ANTINARROW_B1,
"抗窄带性能 CCO band2": AllCertCaseValue.ROOT_PERFORMANCE_CCO_ANTINARROW_B2,
"抗脉冲性能 CCO band1": AllCertCaseValue.ROOT_PERFORMANCE_CCO_ANTIPULSE_B1,
"抗脉冲性能 CCO band2": AllCertCaseValue.ROOT_PERFORMANCE_CCO_ANTIPULSE_B2,
"功率频谱密度 CCO band1": AllCertCaseValue.ROOT_PERFORMANCE_CCO_PSD_B1,
"功率频谱密度 CCO band2": AllCertCaseValue.ROOT_PERFORMANCE_CCO_PSD_B2,
"CCO 速率测试 band1": AllCertCaseValue.ROOT_PERFORMANCE_CCO_RATE_B1,
"CCO 速率测试 band2": AllCertCaseValue.ROOT_PERFORMANCE_CCO_RATE_B2,
# other test case
"OTHER_TEST": AllCertCaseValue.ROOT_OTHER_CHILD,
"RATE TEST": AllCertCaseValue.ROOT_OTHER_RATE,
}
class tree(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self):
super(tree, self).__init__()
self.setupUi(self)
self.AllTestCase = None
self.intiui()
def intiui(self):
# 初始化tablewidget模块
self.tw = TableWidgetClass(self.tableWidget)
# 设置列数
self.treeWidget.setColumnCount(1)
# 设置树形控件头部的标题
self.treeWidget.setHeaderLabels(['测试用例'])
self.treeWidget.setColumnWidth(0, 120)
# 设置根节点
self.AllTestCase = QTreeWidgetItem(self.treeWidget)
self.AllTestCase.setText(0, '测试项')
self.AllTestCase.setCheckState(0, Qt.Unchecked)
# sg
for value in DictCommandInfo.keys():
if DictCommandInfo[value] == AllCertCaseValue.ROOT_PROTOCON:
item_protocon = QTreeWidgetItem(self.AllTestCase)
item_protocon.setText(0, value)
item_protocon.setCheckState(0, Qt.Unchecked)
elif DictCommandInfo[value] == AllCertCaseValue.ROOT_PROTOCON_STA_CHILD:
item_sta_father = QTreeWidgetItem(item_protocon)
item_sta_father.setText(0, value)
item_sta_father.setCheckState(0, Qt.Unchecked)
elif (AllCertCaseValue.ROOT_PROTOCON_STA_CHILD < DictCommandInfo[value] <
AllCertCaseValue.ROOT_PROTOCON_STA_MAX):
item_sta_child = QTreeWidgetItem(item_sta_father)
item_sta_child.setText(0, value)
item_sta_child.setCheckState(0, Qt.Unchecked)
elif DictCommandInfo[value] == AllCertCaseValue.ROOT_PROTOCON_CCO_CHILD:
item_cco_father = QTreeWidgetItem(item_protocon)
item_cco_father.setText(0, value)
item_cco_father.setCheckState(0, Qt.Unchecked)
elif (AllCertCaseValue.ROOT_PROTOCON_CCO_CHILD < DictCommandInfo[value] <
AllCertCaseValue.ROOT_PROTOCON_CCO_MAX):
item_cco_child = QTreeWidgetItem(item_cco_father)
item_cco_child.setText(0, value)
item_cco_child.setCheckState(0, Qt.Unchecked)
elif DictCommandInfo[value] == AllCertCaseValue.ROOT_PERFORMANCE_CHILD:
item_prerf_father = QTreeWidgetItem(self.AllTestCase)
item_prerf_father.setText(0, value)
item_prerf_father.setCheckState(0, Qt.Unchecked)
elif DictCommandInfo[value] == AllCertCaseValue.ROOT_PERFORMANCE_STA_CHILD:
item_prerf_sta_father = QTreeWidgetItem(item_prerf_father)
item_prerf_sta_father.setText(0, value)
item_prerf_sta_father.setCheckState(0, Qt.Unchecked)
elif (AllCertCaseValue.ROOT_PERFORMANCE_STA_CHILD < DictCommandInfo[value] <
AllCertCaseValue.ROOT_PERFORMANCE_STA_MAX):
item_perf_sta_child = QTreeWidgetItem(item_prerf_sta_father)
item_perf_sta_child.setText(0, value)
item_perf_sta_child.setCheckState(0, Qt.Unchecked)
elif DictCommandInfo[value] == AllCertCaseValue.ROOT_PERFORMANCE_CCO_CHILD:
item_prerf_cco_father = QTreeWidgetItem(item_prerf_father)
item_prerf_cco_father.setText(0, value)
item_prerf_cco_father.setCheckState(0, Qt.Unchecked)
elif (AllCertCaseValue.ROOT_PERFORMANCE_CCO_CHILD < DictCommandInfo[value] <
AllCertCaseValue.ROOT_PERFORMANCE_CCO_MAX):
item_perf_cco_child = QTreeWidgetItem(item_prerf_cco_father)
item_perf_cco_child.setText(0, value)
item_perf_cco_child.setCheckState(0, Qt.Unchecked)
elif DictCommandInfo[value] == AllCertCaseValue.ROOT_OTHER_CHILD:
item_other_father = QTreeWidgetItem(self.AllTestCase)
item_other_father.setText(0, value)
item_other_father.setCheckState(0, Qt.Unchecked)
elif AllCertCaseValue.ROOT_OTHER_CHILD < DictCommandInfo[value] < \
AllCertCaseValue.ROOT_OTHER_MAX:
item_other_child = QTreeWidgetItem(item_other_father)
item_other_child.setText(0, value)
item_other_child.setCheckState(0, Qt.Unchecked)
# 节点全部展开
self.treeWidget.expandAll()
self.treeWidget.itemChanged.connect(self.handlechanged)
def handlechanged(self, item, column):
count = item.childCount()
if item.checkState(column) == Qt.Checked:
if count == 0:
self.tw.table_append(item.text(0))
for f in range(count):
if item.child(f).checkState(0) != Qt.Checked:
item.child(f).setCheckState(0, Qt.Checked)
self.tw.table_append(item.child(f).text(0))
if item.checkState(column) == Qt.Unchecked:
if count == 0:
self.tw.table_remove(item.text(0))
for f in range(count):
if item.child(f).checkState != Qt.Unchecked:
item.child(f).setCheckState(0, Qt.Unchecked)
self.tw.table_remove(item.text(0))
class TableWidgetClass(QWidget):
def __init__(self, tablewidget):
# noinspection PyArgumentList
super(TableWidgetClass, self).__init__()
self.tableWidget = None
self.initui(tablewidget)
self.twrowcnt = 0
def initui(self, tablewidget):
# layout = QHBoxLayout()
self.tableWidget = tablewidget
self.tableWidget.setColumnCount(4)
self.tableWidget.setRowCount(0)
# 设置头label
self.tableWidget.setHorizontalHeaderLabels(['测试用例', '执行时间', '结果', '备注'])
# 设置列宽度
self.tableWidget.setColumnWidth(0, 200)
self.tableWidget.setColumnWidth(1, 130)
self.tableWidget.setColumnWidth(2, 100)
self.tableWidget.setColumnWidth(3, 400)
# 优化3 将表格变为禁止编辑
self.tableWidget.setEditTriggers(QAbstractItemView.NoEditTriggers)
# 优化 4 设置表格整行选中
self.tableWidget.setSelectionBehavior(QAbstractItemView.SelectRows)
def table_append(self, str_info):
if str_info == 'CCO测试项' or str_info == 'STA测试项' or str_info == '协议一致性' \
or str_info == '通信性能测试' or str_info == 'CCO性能测试项' or str_info == 'STA性能测试项':
return
item = self.tableWidget.findItems(str_info, Qt.MatchExactly)
if not item:
row = self.tableWidget.rowCount()
self.tableWidget.insertRow(row)
newitem = QTableWidgetItem(str_info)
self.tableWidget.setItem(row, 0, newitem)
def table_remove(self, str_info):
item = self.tableWidget.findItems(str_info, Qt.MatchExactly)
if item:
row = item[0].row()
self.tableWidget.removeRow(row)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
myshow = tree()
myshow.show()
sys.exit(app.exec_())
以上代码可以实现图片上的功能。
本文地址:https://blog.csdn.net/acdefghb/article/details/107357498
上一篇: 一文搞定Python字典操作
下一篇: 腾讯AI布局迟缓,要想突围还有两场硬仗