Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Context Menu : lambda triggered connect - called with same args?
Forum Updated to NodeBB v4.3 + New Features

Context Menu : lambda triggered connect - called with same args?

Scheduled Pinned Locked Moved Solved Qt for Python
5 Posts 2 Posters 1.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    nicholas_yue
    wrote on 9 Jan 2022, 19:43 last edited by nicholas_yue 1 Sept 2022, 19:43
    #1

    I am attempting to build a context menu programmatically and connecting them too.

    I thought I am passing unique arguments to my handler but when I print out the values, they all seems to be given the same arguments when I print them out in the function createInstance(). What am I doing wrongly in the loop ?

        def createInstance(self, category: str, classname: str):
            print('000000000000000000000 createInstance {}:{}'.format(category,classname))
            # self.scene().addNode(category, classname, xpos = 0, ypos = 0, node_id = None)
    
        def createContextMenu(self):
            menu = QtWidgets.QMenu()
            for k, v in self.scene().nodeManager.getClasses().items():
                submenu = menu.addMenu(k)
                for kk, vv in v.items():
                    print('createContextMenu {}:{}'.format(k,kk))
                    action = submenu.addAction(kk)
    
                    # C++ version : https://stackoverflow.com/questions/42522136/qaction-triggered-signal-to-pass-parameter-to-slot
                    # connect(copyDataAction, &QAction::triggered, this, [=](){
                    # CopyTableData(DataTable);
                    # });
    
                    # PySide2 implementation : https://www.pythonguis.com/tutorials/pyside-transmitting-extra-data-qt-signals/
                    action.triggered.connect(lambda x: self.createInstance(k, kk))
    
            return menu
    
    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 9 Jan 2022, 20:37 last edited by
      #4

      Use a parameter with default values as explained here.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      N 1 Reply Last reply 10 Jan 2022, 01:50
      1
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 9 Jan 2022, 19:46 last edited by
        #2

        Hi,

        k and kk (you should use proper variable names) are not captured so they keep the last value they were assigned.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • N Offline
          N Offline
          nicholas_yue
          wrote on 9 Jan 2022, 20:31 last edited by
          #3

          @SGaist Thanks for the advice.

          How do I ensure that they are captured, I cannot use string literal as I would not know in advanced what they will be.

          Cheers

          1 Reply Last reply
          0
          • S Offline
            S Offline
            SGaist
            Lifetime Qt Champion
            wrote on 9 Jan 2022, 20:37 last edited by
            #4

            Use a parameter with default values as explained here.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            N 1 Reply Last reply 10 Jan 2022, 01:50
            1
            • S SGaist
              9 Jan 2022, 20:37

              Use a parameter with default values as explained here.

              N Offline
              N Offline
              nicholas_yue
              wrote on 10 Jan 2022, 01:50 last edited by
              #5

              @SGaist Thank you for the link. I have got it working.

              1 Reply Last reply
              0

              4/5

              9 Jan 2022, 20:37

              • Login

              • Login or register to search.
              4 out of 5
              • First post
                4/5
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved