go.js 节点添加右键菜单

公司项目开发时,采用了go.js绘制节点关系图,需添加右键功能,采坑经历分享。

go.js API文档不太好懂,开发功能时,建议从实例入手,然后再去看API文档查找相关属性用法 就很容易理解了。如图:

1.添加右键功能,如下图:

右键属性contextMenu,贴代码:

  //定义一个简单节点,右键属性contextMenu在节点内添加
	myDiagram.nodeTemplate =
		$(go.Node, "Auto",
		  { contextMenu: myContextMenu },
		  $(go.Shape, "RoundedRectangle",
			// Shape.fill is bound to Node.data.color
			new go.Binding("fill", "color")),
		  $(go.TextBlock,
			{ margin: 3 },  // some room around the text
			// TextBlock.text is bound to Node.data.key
			new go.Binding("text", "key"))
		 );


//  ContextMenu,添加右键菜单,绑定事件,自定义菜单样式
 var myContextMenu =  $("ContextMenu",
           $("ContextMenuButton",
                   $(go.TextBlock, "剪切",{
                      alignment: go.Spot.Center,
                      margin: 5,
                      font: "12px sans-serif",
                      opacity: 0.75,
                      stroke: "#404040"
                   }),
                    { click: function(e, obj) { handleCut(obj,'0');}
                   }),
           $("ContextMenuButton",
                   $(go.TextBlock, "复制",{
                      alignment: go.Spot.Center,
                      margin: 5,
                      font: "12px sans-serif",
                      opacity: 0.75,
                      stroke: "#404040"
                   }),
                   { click: function(e, obj) { handleCopy(obj,'1'); } }
                   ),
           $("ContextMenuButton",
                   $(go.TextBlock, "删除",{
                      alignment: go.Spot.Center,
                      margin: 5,
                      font: "12px sans-serif",
                      opacity: 0.75,
                      stroke: "#404040"
                   }),
                   { click: function(e, obj) { handleDelete(obj,'2'); } }),
           $("ContextMenuButton",
                   $(go.TextBlock, "变色",{
                      alignment: go.Spot.Center,
                      margin: 5,
                      font: "12px sans-serif",
                      opacity: 0.75,
                      stroke: "#404040"
                   }),
                   { click: function(e, obj) { handleColor(obj,'3'); } }));

//右键事件
function handleCut(){console.log('剪切')};
function handleCopy(){console.log('复制')};
function handleDelete(){console.log('删除')};
function handleColor(){console.log('变色')};

注意!!此处的坑:版本问题,还有层级嵌套的节点组

1. go.js版本最好为GOJS 2.1 版本以上,目前测试过1.7 、1.8 版本,都不兼容添加右键功能,莫名其妙抛错;

2. 有层级嵌套的节点组,groupTemplate,nodeTemplate节点模板定义里都要添加右键属性contextMenu,如图:

以上就是右键添加的实现。

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
你可以通过以下代码实现: ```python from PyQt5.QtWidgets import QMenu, QTreeWidgetItem, QTreeWidget, QApplication from PyQt5.QtCore import Qt class MyTreeWidget(QTreeWidget): def __init__(self): super().__init__() self.init_ui() def init_ui(self): self.setHeaderLabels(['Name', 'Age']) root = QTreeWidgetItem(self) root.setText(0, 'Root') root.setText(1, '30') child1 = QTreeWidgetItem(root) child1.setText(0, 'Child1') child1.setText(1, '20') child2 = QTreeWidgetItem(root) child2.setText(0, 'Child2') child2.setText(1, '25') self.addTopLevelItem(root) self.setContextMenuPolicy(Qt.CustomContextMenu) self.customContextMenuRequested.connect(self.show_context_menu) def show_context_menu(self, pos): item = self.itemAt(pos) if not item.parent(): menu = QMenu() action = menu.addAction('Add Child') action.triggered.connect(self.add_child) menu.exec_(self.mapToGlobal(pos)) def add_child(self): item = self.currentItem() child = QTreeWidgetItem(item) child.setText(0, 'New Child') child.setText(1, '0') item.addChild(child) if __name__ == '__main__': app = QApplication([]) tree = MyTreeWidget() tree.show() app.exec_() ``` 在这个例子中,我们创建了一个继承自 QTreeWidget 的类 MyTreeWidget。在类的初始化函数中,我们添加了一个根节点和两个子节点,并将其添加到树中。我们通过设置 setContextMenuPolicy() 方法为树添加右键菜单,然后在 customContextMenuRequested 信号的槽函数中根据鼠标点击的位置获取到当前的节点,判断该节点是否为根节点,如果是则弹出菜单菜单添加了一个 'Add Child' 的动作,在其槽函数中添加了一个新的子节点,并将其添加到当前节点中。需要注意的是,我们在添加菜单时只为根节点添加菜单,因为其他节点没有该菜单的需求。 你可以根据需要修改菜单的内容和槽函数的实现。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值