How to get the text of all items within a QMenu


#1

Hi all,

Is there a way that I can check my input against the list of items in a QMenu?

I have tried checking the documentation and tried googling online but to no avail results and it seems that most of the results are usually where the menu item is called only when it is clicked/selected.

So far, I am only able to get the adding of new items works as follows, but it allows items of the same naming.

def add_new_item(self):
    menu = QtGui.QMenu()
    new_item_name, ok = QtGui.QInputDialog.getText(
            self,
            "Name of Item",
            "Name of new Item:"
    )

    if ok:
        add_icon = QtGui.QIcon('/Desktop/icons/add.png')
        add_item_action = QtGui.QAction(add_icon, 'Add Item', menu)
        slot = functools.partial(self._addActionItem, menu)
        add_item_action.triggered.connect(slot)
        menu.addAction(add_item_action)

#2

Pseudo coding but shouldn’t something like this do the trick?

if not name in [ child.text() for child in menu.children() if instanceof( child, QAction ) ]:
    menu.addAction( new_action )