您好,我已经设法使用下面的代码将一些qgraphicsitems添加到了managed场景中
def generate_graph_and_update_scene(self):
try:
local_params=locals() #for error log get local paramters
this_function_name=sys._getframe().f_code.co_name #for error log get function name
self.vertex_dict.clear()
self.clear() #clear graphicsscene
self.graph_pos.clear() #clear graph position holder object
#function that generates the node data
root_nodes=my_database_query.get_nodes_information()
for node in root_nodes:
# add nodes to nx.graph object
self.nx_graph.add_node(node['column1'])
# create networkx graph
self.graph_pos = nx.spring_layout(self.nx_graph, iterations=25,scale=10)
for node in self.nx_graph.nodes(): # Add nodes to qgraphicsscene
v=default_nodeobject.my_ellipse(node,self.graph_pos)
self.addItem(v) # Add ellipse to qgraphics scene
for edge in self.nx_graph.edges():
self.addItem(defaultedgeview.edgeview(edge[0], edge[1],self.graph_pos))#add edges to qgraphicscene
except:
#Exception handler
message=str(sys.exc_info())
message=message + str(local_params)+" "+ str(this_function_name)
print message这允许我在我的qgraphics场景中添加600个“节点”,但是当我清除场景并添加另外1500个节点时,添加的项目会阻塞UI,并且我的整个应用程序会冻结几秒钟。还有,每当我做一些事情,比如循环遍历graphicsitems,比如寻找具有特定属性的节点,当我循环时,主线程再次冻结,
有没有人能建议一个好的方法来保持UI的响应性,同时对场景中的grpahicsscene场景/项目进行操作。理想情况下,我希望有平滑的,非阻塞的场景更新,即使我有几千个项目显示。
发布于 2014-11-12 17:33:13
这里的问题是将每个节点作为一个图形项进行管理。添加和删除场景,以及渲染每个项目都需要时间。有了这么多项目,我建议以不同的方式设计它。
将节点图视为单个自定义图形项,它存储一组节点并将它们作为一个单元进行管理,而不是600+单独的项。
以这种方式设计,您只需向场景中添加一个项目(节点图),即可快速添加和移除节点,并且还会看到渲染场景的性能提高,因为所有节点都是在一次调用paint()时绘制的。
当然,如果您需要通过单击和拖动来移动节点,则必须添加额外的代码来检测项目中选择的节点,并自行移动节点。
但是,这是处理场景中大量项目的最佳方法。
https://stackoverflow.com/questions/26875396
复制相似问题