A toolbar icon with a multiple actions in a submenu

snippets/toolbar/createToolbarWithDropDownMenu.py
 1# © 2024 Autodesk, Inc. All rights reserved.
 2
 3# This example shows how to add a custom icon with a submeny to the VRED Toolbar.
 4# It creates a toolbar with multiple actions that open a URL in the default web browser.
 5# The actions are added to a drop-down menu that is displayed when the left mouse button is 
 6# held down on the toolbar icon.
 7# The toolbar is also added as a toggle to the View->Toolbars menu.
 8
 9from PySide6 import QtGui, QtWidgets
10import datetime, os
11import webbrowser
12
13def my_tool_action():
14    print(f"My Tool: {datetime.datetime.now()}.")
15
16def open_url_in_explorer(url):
17    webbrowser.open(url)
18    
19def sub_menu_1():
20    print('VRED Documentation...')
21    open_url_in_explorer("https://help.autodesk.com/view/VREDPRODUCTS/2025/ENU/")
22
23def sub_menu_2():
24    print('Alias Documentation...')
25    open_url_in_explorer("https://help.autodesk.com/view/ALIAS/2025/ENU/")
26
27def sub_menu_3():
28    print('Flow PT Documentation...')
29    open_url_in_explorer("https://help.autodesk.com/view/SGSUB/ENU/?guid=SG_user_wn_whats_new_html")
30
31exampleFolder = os.path.join(os.getenv('VRED_ROOT'), "examples", "snippets", "toolbar")
32iconPath = os.path.join(exampleFolder, "my_tool_icon.png")
33
34my_action = QtGui.QAction(QIcon(iconPath), 'My Tool')
35my_action.setToolTip('My Tool\n\nClick to activate.')
36
37my_toolbar = QtWidgets.QToolBar('my_toolbar')
38my_toolbar.setToolButtonStyle(QtGui.Qt.ToolButtonTextUnderIcon)
39my_toolbar.addAction(my_action)
40
41vrGUIService.addMainWindowToolBar(my_toolbar)
42
43tool_action_1 = QtGui.QAction(my_toolbar.tr("VRED Help"))
44tool_action_1.triggered.connect(sub_menu_1)
45
46tool_action_2 = QtGui.QAction(my_toolbar.tr("Alias Help"))
47tool_action_2.triggered.connect(sub_menu_2)
48
49tool_action_3 = QtGui.QAction(my_toolbar.tr("Flow PT Help"))
50tool_action_3.triggered.connect(sub_menu_3)
51
52my_menu = QtWidgets.QMenu(my_toolbar.tr("My Menu"))
53my_menu.addAction(tool_action_1)
54my_menu.addAction(tool_action_2)
55my_menu.addAction(tool_action_3)
56
57my_action.setMenu(my_menu)
58my_action.triggered.connect(my_tool_action)