widget 设置
- QtWidgets.QTableWidget()
- 设置列数为5 .setColumnCount(5)
- 设置每列宽度 setColumnWidth(0, 22) 这里的0 是第0列
- 设置水平头部标签 .setHorizontalHeaderLabels(['', 'Name', 'Tx', 'Ty', 'Tz']) 这里的列表有5个元素,对应5列
- 获取水平头部 horizontalHeader()
- 设置部分大小方式为拉伸 .setSectionResizeMode(1, QtWidgets.QHeaderView.Stretch) 这里的 1 是第二列的索引
def create_widgets(self):
self.table_wdg = QtWidgets.QTableWidget()
self.table_wdg.setColumnCount(5)
self.table_wdg.setColumnWidth(0, 22)
self.table_wdg.setColumnWidth(2, 70)
self.table_wdg.setColumnWidth(3, 70)
self.table_wdg.setColumnWidth(4, 70)
self.table_wdg.setHorizontalHeaderLabels(['', 'Name', 'Tx', 'Ty', 'Tz'])
header_view = self.table_wdg.horizontalHeader()
# 设置部分大小模式为拉伸, 索引1是对应的"Name"
header_view.setSectionResizeMode(1, QtWidgets.QHeaderView.Stretch)
self.refresh_btn = QtWidgets.QPushButton('Refresh')
self.close_btn = QtWidgets.QPushButton('Close')
refresh_table 槽函数设置
- 更新的方法是,设置行数为 0。 self.table_wdg.setRowCount(0)
- 列出mesh 物体, 获取名字, 属性, 可见性
- 插入行的分割线 self.table_wdg.insertRow(i)
- 插入每行 的成员数据,调用 后面的insert_item 槽函数
def refresh_table(self):
# 更新的方法是,设置行数为 0
self.table_wdg.setRowCount(0)
meshes = pm.ls(typ = 'mesh')
for i in range(len(meshes)):
transform_name = pm.listRelatives(meshes[i], parent = True)[0].name()
translation = pm.getAttr('{}.translate'.format(transform_name))
visible = pm.getAttr('{}.visibility'.format(transform_name))
# 插入行
self.table_wdg.insertRow(i)
self.insert_item(i, 0, '', 'visibility', visible, True)
self.insert_item(i, 1, transform_name, None, transform_name, False)
self.insert_item(i, 2, self.float_to_string(translation[0]), 'tx', translation[0], False)
self.insert_item(i, 3, self.float_to_string(translation[1]), 'ty', translation[1], False)
self.insert_item(i, 4, self.float_to_string(translation[2]), 'tz', translation[2], False)
插入成员
- insert_item 槽函数传入参数 “行,列, 文本, 属性,值,复选框 ”insert_item(self, row, column, text, attr, value, is_boolean)
def insert_item(self, row, column, text, attr, value, is_boolean):
item = QtWidgets.QTableWidgetItem(text)
self.set_item_attr(item, attr)
self.set_item_value(item, value)
if is_boolean:
# 复选框可以被点击,且是点击中
item.setFlags(QtCore.Qt.ItemIsUserCheckable | QtCore.Qt.ItemIsEnabled)
self.set_item_checked(item, value)
self.table_wdg.setItem(row, column, item)
完整代码
from PySide2 import QtCore, QtWidgets
from shiboken2 import wrapInstance
import maya.OpenMayaUI as omui
import pymel.core as pm
def get_maya_main_window():
maya_main_wnd = omui.MQtUtil.mainWindow()
return wrapInstance(long(maya_main_wnd), QtWidgets.QWidget)
class TabelExampleWindow(QtWidgets.QDialog):
ATTR_ROLE = QtCore.Qt.UserRole
VALUE_ROLE = QtCore.Qt.UserRole + 1
def __init__(self, parent = get_maya_main_window()):
super(TabelExampleWindow, self).__init__(parent)
self.setWindowTitle('Table Example UI')
self.setMinimumWidth(500)
self.create_widgets()
self.create_layouts()
self.create_connections()
def create_widgets(self):
self.table_wdg = QtWidgets.QTableWidget()
self.table_wdg.setColumnCount(5)
self.table_wdg.setColumnWidth(0, 22)
self.table_wdg.setColumnWidth(2, 70)
self.table_wdg.setColumnWidth(3, 70)
self.table_wdg.setColumnWidth(4, 70)
self.table_wdg.setHorizontalHeaderLabels(['', 'Name', 'Tx', 'Ty', 'Tz'])
header_view = self.table_wdg.horizontalHeader()
# 设置部分大小模式为拉伸, 索引1是对应的"Name"
header_view.setSectionResizeMode(1, QtWidgets.QHeaderView.Stretch)
self.refresh_btn = QtWidgets.QPushButton('Refresh')
self.close_btn = QtWidgets.QPushButton('Close')
def create_layouts(self):
btn_layout = QtWidgets.QHBoxLayout()
btn_layout.setSpacing(2)
btn_layout.setContentsMargins(2, 2, 2, 2)
btn_layout.addWidget(self.refresh_btn)
btn_layout.addWidget(self.close_btn)
main_layout = QtWidgets.QVBoxLayout(self)
main_layout.setContentsMargins(2, 2, 2, 2)
main_layout.setSpacing(2)
main_layout.addWidget(self.table_wdg)
main_layout.addStretch()
main_layout.addLayout(btn_layout)
def create_connections(self):
self.refresh_btn.clicked.connect(self.refresh_table)
self.close_btn.clicked.connect(self.close)
#___Slots___
def refresh_table(self):
# 更新的方法是,设置行数为 0
self.table_wdg.setRowCount(0)
meshes = pm.ls(typ = 'mesh')
for i in range(len(meshes)):
transform_name = pm.listRelatives(meshes[i], parent = True)[0].name()
translation = pm.getAttr('{}.translate'.format(transform_name))
visible = pm.getAttr('{}.visibility'.format(transform_name))
# 插入行
self.table_wdg.insertRow(i)
self.insert_item(i, 0, '', 'visibility', visible, True)
self.insert_item(i, 1, transform_name, None, transform_name, False)
self.insert_item(i, 2, self.float_to_string(translation[0]), 'tx', translation[0], False)
self.insert_item(i, 3, self.float_to_string(translation[1]), 'ty', translation[1], False)
self.insert_item(i, 4, self.float_to_string(translation[2]), 'tz', translation[2], False)
# 插入成员
def insert_item(self, row, column, text, attr, value, is_boolean):
item = QtWidgets.QTableWidgetItem(text)
self.set_item_attr(item, attr)
self.set_item_value(item, value)
if is_boolean:
# 复选框可以被点击,且是点击中
item.setFlags(QtCore.Qt.ItemIsUserCheckable | QtCore.Qt.ItemIsEnabled)
self.set_item_checked(item, value)
self.table_wdg.setItem(row, column, item)
def set_item_text(self, item, text):
item.setText(text)
def get_item_text(self, item):
return item.text()
# 设置复选框状态
def set_item_checked(self, item, checked):
if checked:
item.setCheckState(QtCore.Qt.Checked)
else:
item.setCheckState(QtCore.Qt.Unchecked)
# 判断复选框状态
def is_item_checked(self, item):
return item.checkState() == QtCore.Qt.Checked
# 设置成员属性
def set_item_attr(self, item, attr):
item.setData(self.ATTR_ROLE, attr)
def get_item_attr(self, item):
return item.data(self.ATTR_ROLE)
# 设置成员属性值
def set_item_value(self, item, value):
item.setData(self.VALUE_ROLE, value)
def get_item_value(self, item):
item.data(self.ATTR_ROLE)
def float_to_string(self, value):
return '{0:.4f}'.format(value)
# 为了不每次去点击 Refresh 再生成行数,需要执行 showEvent。
# 利用窗口显示的时候去执行 refresh_table 这个槽函数
def showEvent(self, event):
# 继承父的 showEvent
super(TabelExampleWindow, self).showEvent(event)
self.refresh_table()
if __name__ == '__main__':
try:
myTest_ui.close() # pylint:disable = E0601
myTest_ui.deletelater() # pylint:disable = E0601
except:
pass
myTest_ui = TabelExampleWindow()
myTest_ui.show()