Skip to content

Instantly share code, notes, and snippets.

@davidbeauchamp
Last active August 29, 2015 14:16
Show Gist options
  • Save davidbeauchamp/c5f3dc5e7260d27693b2 to your computer and use it in GitHub Desktop.
Save davidbeauchamp/c5f3dc5e7260d27693b2 to your computer and use it in GitHub Desktop.
qmenu tricks
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);
@tomdatkins
Copy link

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