0

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())
dissidia
  • 1,531
  • 3
  • 23
  • 53
  • try to make it `self.menu = QMenu("menu", self.setCameraBtn)` instead of `menu = Q....`. – Bleeding Fingers Oct 07 '14 at 09:38
  • Pardon me but is adding in `self.` towards the line that you mentioned going to make any differences? – dissidia Oct 07 '14 at 09:52
  • By the way, I am somewhat able to get my menu to have a live-update by adding `self.connect(self.setCameraBtn, SIGNAL("pressed()"), self.camMenu)` in `createConnections` however I will need to press the button twice in order to get the latest but this 'lagginess' is driving me crazy though – dissidia Oct 07 '14 at 09:55
  • It was a advice, easy referencing of the QMenu instance from elsewhere in the class. And that solution is really not what you were looking for when you mentioned "live-update" and what you described. Of course you could add a refresh button to do that but that aint updating the menu automatically. – Bleeding Fingers Oct 07 '14 at 10:23

1 Answers1

0

This can be easily accomplished by firing a QThread instead the UI that periodically call a update camera method which checks for the current cameras in the scene and compares them with the one that the UI has already registered. And if there is an addition, change or deletion in the camera(s) then it updates the menu to reflect it.

Another solution is to use scriptJob.

Bleeding Fingers
  • 6,993
  • 7
  • 46
  • 74
  • I am not very familiar with `QThread` but supposedly if I am going to use it, does this means that I will need to re-work the parts that are involved with QMenu? Then again, I am trying to make the whole thing work with the current coding that I have... – dissidia Oct 07 '14 at 02:46
  • There isn't any magic flag that if set could make this work. That said, you will have to write new code which does the checking for camera changes and barely change the existing QMenu construction. – Bleeding Fingers Oct 07 '14 at 09:37