我不确定Qt的模型/视图系统是如何工作的...他们声称qtreewidget小部件使用它,但我无法看到它的工作原理。
我想要做的是让qtreewidget节点指向数据对象,并覆盖UI用于显示数据的方法。有人可以告诉我如何覆盖这些方法吗?
这是我的数据对象......
class Car():
def __init__(self, name="", age=0, children=None):
self.name = name
self.uid = str( uuid.uuid4( ) )
self.age = age
self.children = children if children is not None else []
我无法看到在哪里做这项工作,但理论上我觉得它看起来像这样:
class CustomTreeNode( QtGui.QTreeWidgetItem ):
def __init__( self, parent, car ): # Only supply the car, you will see why down below
super( CustomTreeNode, self ).__init__( parent )
self.car_data = car
def getText( self, col ):
# Override whatever internal method the UI calls to query the text value before painting itself
# --- this would always just reflect the Car instance's value instead of trying to synchronize all of the TreeItems
return self.car_data.name
def getFont( self, col ):
# Likewise for painting the selected items bolded --
# Over in your treewidget on selection changed events, you'd store off the selected cars:
# --- self.selected_cars = set( [ item.car_data for item in event.selection ] )
# Then here when the UI queries the font to render it would do something like this:
if self.car_data in self.tree_widget.selected_cars:
return FONTS.get_font("accent")
else:
return FONTS.get_font("regular")