【发布时间】:2017-02-27 05:04:08
【问题描述】:
我有一个 QTableWidget,2 列,其中第一列包含项目的名称,而第二列填充有 qcomboboxes(使用 cellwidgets 创建),其中包含颜色选项列表(例如“”、RED、BLUE、绿色)
比如下表中带*的项目有一个变量叫color_set,我尝试实现如下:
- 它将查找场景中项目的名称
- 然后它会通过一个函数来检查项目是否
shirt_01包含一个名为color_set的变量 - 如果变量存在,比如它是 RED,它将在组合框中查找选项列表并将 RED 显示为值
- 如果变量不存在,则会显示一个空白字段
-
此函数/值在启动 ui 之前使用信息填充表格,它不是按钮单击函数等。
Name of items | Color Options shirt_01* | RED shirt_02 | pants* | GREEN
但是,我没有像上面那样让我的 ui 输出,而是得到以下结果:
Name of items | Color Options
shirt_01* | RED
shirt_02 | RED
pants* | GREEN
由于某些原因,shirt_02 似乎“纠结”并显示与shirt_01 相同的结果。而且我认为这可能是由于我的逻辑是如何编写的,但我无法弄清楚,或者因为我一直将输出结果绑定到self.color_combobox,因为我的代码最初检查第一列,然后在第二列中找到匹配的文本柱子。
但是我在行中不断收到错误 - item_name 没有属性 .text()
import maya.cmds as cmds
from PyQt4 import QtGui, QtCore
import json
def get_all_mesh():
all_mesh = cmds.listRelatives(cmds.ls(type = 'mesh'), parent=True)
return all_mesh
def read_json(node_name):
# checks if the node_name exists in the json file
with open('/Desktop/car_colors.json') as data_file:
data = json.load(data_file)
items = set()
for index, name in enumerate(data):
# if the name is in the json, it will states the color
if node_name in name:
for item in (data[name]):
#print "{0} - {1}".format(name, item)
items.add(item)
return items
def attrToPy(objAttr):
stringAttrData = str(cmds.getAttr(objAttr))
loadedData = cPickle.loads(stringAttrData)
return loadedData
class testTableView(QtGui.QDialog):
def __init__(self, parent=None):
QtGui.QDialog.__init__(self, parent)
self.setWindowTitle('Color Test')
self.setModal(False)
self.all_mesh = get_all_mesh()
# Build the GUI
self.init_ui()
self.populate_data()
def init_ui(self):
# Table setup
self.mesh_table = QtGui.QTableWidget()
self.mesh_table.setRowCount(len(self.all_mesh))
self.mesh_table.setColumnCount(3)
self.mesh_table.setHorizontalHeaderLabels(['Mesh Found', 'Color for Mesh'])
self.md_insert_color_btn = QtGui.QPushButton('Apply color')
# Layout
self.layout = QtGui.QVBoxLayout()
self.layout.addWidget(self.mesh_table)
self.layout.addWidget(self.md_insert_color_btn)
self.setLayout(self.layout)
def populate_data(self):
geo_name = self.all_mesh
for row_index, geo_item in enumerate(geo_name):
new_item = QtGui.QTableWidgetItem(geo_item)
# Add in each and every mesh found in scene and append them into rows
self.mesh_table.setItem(row_index, 0, new_item)
geo_exclude_num = ''.join(i for i in geo_item if not i.isdigit())
color_list = read_json(geo_exclude_num)
color_list.add("")
# Insert in the color
self.color_combobox = QtGui.QComboBox()
#color_list = get_color()
self.color_combobox.addItems(list(sorted(color_list)))
self.mesh_table.setCellWidget(row_index, 1, self.color_combobox)
self.color_value_to_combobox()
def color_value_to_combobox(self):
obj_nodes = get_all_mesh()
for node in obj_nodes:
# This attribute is stored in the Extra Attributes
objAttr = '%s.pyPickle'%node
storedData = attrToPy(objAttr)
if 'color_set' in storedData:
color_variant = storedData['color_set']
combo_index = self.color_combobox.findText(color_variant, QtCore.Qt.MatchFixedString)
self.color_combobox.setCurrentIndex(0 if combo_index < 0 else combo_index)
# To opent the dialog window
dialog = testTableView()
dialog.show()
无论如何我可以让我的用户界面检查场景中的项目列表,记录它应该去的名称和组合框(根据哪些行/列)?否则我还有什么其他方法可以接近?
编辑:这是我快速完成的附件 - Link 如果你运行 UI,你会注意到 pCube1 的颜色是“红色”而 pCube2 是“蓝色”,但是两个组合框都会变成蓝色
【问题讨论】:
标签: python pyqt maya qtablewidget qcombobox