How to update contents in QMenu?

I have a UI in which it consists of a few QPushButton and a QLineEdit and currently I am having trouble to ‘update’ the contents within this QMenu that was attached onto a QPushButton.
So assumingly, if there are already 2 cameras in my scene, and as I execute my UI, by pressing onto this setCameraBtn I will get the 2 cameras in the list. However, if I create a new camera where the UI is not yet close, how do I make my QMenu to read in the new camera, like a ‘live-update’?

I tried creating another function where it re-read the cameras in scene and retabulate the camLs as well as a connection similar to the one that I have written in the createConnections but it does not seems to be reading in.


camLs = []

class orientCameraUI(QDialog):
    def __init__(self, parent=None):
        ...
        ...

    def initUI(self):
        ...
        ...

    def createConnections(self):
        self.connect(self.orientToCamBtn, SIGNAL('clicked()'), self.orientToCam)
    
    def camMenu(self):
    
        allCams = [cam for cam in cmds.listRelatives(cmds.ls(cameras=1),parent=1) if cam not in ['front','persp','side','top']]
        camLs.extend(allCams)
        
        menu = QMenu("menu", self.setCameraBtn)
        
        for item in camLs:
            menu.addAction(QAction(item, menu))
        self.setCameraBtn.setMenu(menu)
        
        menu.triggered.connect(self._camSelected)
        
    def _camSelected(self, action):
        self.currentCamTxt.setText(action.text())

Upon adding in this line in the createConnections(), I have sort of got it to work but there is a “lag” in updating the list.
I have to click twice on the button in order to get it to work, in which this is not effective…

self.connect(self.setCameraBtn, SIGNAL('pressed()'), self.camMenu)

Funnily enough, while using ‘clicked()’ as the signal, it does not seems to generate anything, despite I have read from docs that clicked is equivalent to Pressed and Released? Any advices?

whatever code finds the cameras and adds them as menu items to your menu, I would put that code in its own function and connect it to the Clicked() signal of the button.

in pseudo code:

    
    def buttonContextMenu(self, position):
        cameraMenu = QtGui.QMenu(self)
        for cam in cmds.ls(type='camera'):
            cameraMenu.addAction(cam)
        cameraMenu.popup(position)
    ...
    myCameraButton.clicked.connect(buttonContextMenu))