Last active
August 29, 2015 14:16
-
-
Save davidbeauchamp/c5f3dc5e7260d27693b2 to your computer and use it in GitHub Desktop.
qmenu tricks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var _list = mywindow.list(); | |
populateMenu = function(pMenu, pItem, pCol) | |
{ | |
if(pMenu == null) { | |
pMenu = _list.findChild("_menu"); | |
} | |
if(pMenu != null) | |
{ | |
// how to remove a core menu entry by looping through the actions | |
// already in the menu and finding the one you want by text label | |
// once you have actions[i] you can use any of its properties/methods | |
// such as actions[i].enabled = false instead of .visible | |
var actions = pMenu.actions(); | |
for (var i = 0; i < actions.length; i++) { | |
if (actions[i].text == "Delete...") { | |
actions[i].visible = false; | |
} | |
} | |
// this is an example of how to create sub-menus on the context menu | |
// of an xtreewidget. Works the same way the main menu structure does | |
var extraMenu = new QMenu(); | |
extraMenu.objectName = "extra.menu"; | |
extraMenu.name = "_extra"; | |
extraMenu.setTitle("Extras"); | |
pMenu.addMenu(extraMenu); | |
// now that we have the menu we can add actions to it like we normally would | |
var tmpaction = extraMenu.addAction(qsTr("Extra Menu")); | |
tmpaction.setData("ExtraMenu"); | |
tmpaction.objectName = "em.extraMenu"; | |
tmpaction.enabled = true; | |
// tmpaction.clicked.connect(yourFunctionHere); | |
// incorporating @andersdd's snippet | |
// contextually adding menu entries based on column contents | |
if (pItem.rawValue("documenttype").toString() == "Invoice" && _list.id() > 0 ) { | |
// do something with that pItem | |
} | |
} | |
} | |
_list["populateMenu(QMenu*,XTreeWidgetItem*,int)"].connect(populateMenu); |
What should that do David? if I add that script and run the debugger it gets to 😄 populateMenu = function(pMenu, pItem, pCol)
then jumps to the bottom. it never adds the extra menu.
Never Mind David, It's the delete on the right click. This is Great, this is just what I need to work on.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
And here is an example of reading the contents of the selected row and displaying/not displaying a menu based on rules:
if (pItem.rawValue("documenttype").toString() == "Invoice" && _list.id() > 0 && (pItem.rawValue("amount") == pItem.rawValue("balance")))