试图中选择物体,并且在tabwidget中聚焦物体,使用快捷按钮删除的时候不删除物体
- 复写 keyPressEvent, 继承自父类(TabelExampleWindow, self).keyPressEvent(event)
- event.accept()
- 快捷键一斤被处理, 不传递给下一个父窗口
def keyPressEvent(self, event):
super(TabelExampleWindow, self).keyPressEvent(event)
event.accept()
table_wdg 的 cellChanged 会每一个元素改变的时候都会关联上对应的槽函数
- 可以设置一个 具有开关的槽函数,再把这个具有开关的槽函数放在 create_connects 里
- 具有开关的槽函数
def on_cell_changed(self, row, column):
# 不调用
self.set_cell_changed_connection_enabled(False)
item = self.table_wdg.item(row, column)
if column == 1:
self.rename(item)
# 调用
self.set_cell_changed_connection_enabled(True)
def set_cell_changed_connection_enabled(self, enabled):
if enabled:
self.table_wdg.cellChanged.connect(self.on_cell_changed)
else:
self.table_wdg.cellChanged.disconnect(self.on_cell_changed)
完整代码
from PySide2 import QtCore, QtWidgets
from shiboken2 import wrapInstance
import maya.OpenMayaUI as omui
import pymel.core as pm
import maya.cmds as cmds
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.set_cell_changed_connection_enabled(True)
self.refresh_btn.clicked.connect(self.refresh_table)
self.close_btn.clicked.connect(self.close)
#___Slots___
def refresh_table(self):
self.set_cell_changed_connection_enabled(False)
# 更新的方法是,设置行数为 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)
self.set_cell_changed_connection_enabled(True)
# 插入成员
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 on_cell_changed(self, row, column):
self.set_cell_changed_connection_enabled(False)
item = self.table_wdg.item(row, column)
if column == 1:
self.rename(item)
self.set_cell_changed_connection_enabled(True)
def set_cell_changed_connection_enabled(self, enabled):
if enabled:
self.table_wdg.cellChanged.connect(self.on_cell_changed)
else:
self.table_wdg.cellChanged.disconnect(self.on_cell_changed)
def rename(self, item):
old_name = self.get_item_value(item)
new_name = self.get_item_text(item)
if old_name != new_name:
actual_new_name = cmds.rename(old_name, new_name)
if actual_new_name != new_name:
self.set_item_text(item, actual_new_name)
self.set_item_value(item, actual_new_name)
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()
def keyPressEvent(self, event):
super(TabelExampleWindow, self).keyPressEvent(event)
event.accept()
if __name__ == '__main__':
try:
myTest_ui.close() # pylint:disable = E0601
myTest_ui.deletelater() # pylint:disable = E0601
except:
pass
myTest_ui = TabelExampleWindow()
myTest_ui.show()