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. Assign to each QAction choice (on a Qmenu) a method
Qt 6.11 is out! See what's new in the release blog

Assign to each QAction choice (on a Qmenu) a method

Scheduled Pinned Locked Moved Solved Qt for Python
5 Posts 3 Posters 1.5k 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.
  • J Offline
    J Offline
    john_hobbyist
    wrote on last edited by john_hobbyist
    #1

    I am following this code: https://www.tutorialspoint.com/pyqt/qmenubar_qmenu_qaction_widgets.htm

    The problem is that I have assigned a method to each choice of the QMenu, for instance:

    Edit (1st choice int he QMenu ("up")) ---> dothis()
    Copy ---> dothat()
    Cut (last choice in the QMenu ("down")) ----> doCut()

    but when I run the code and choose "Cut" from the QMenu it runs all 3 methods...

    dothis()
    dothat()
    doCut()
    

    however I need only the

    doCut()
    

    method to run... What I miss here???

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Show your code so we may see what is happening,

      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
      1
      • J Offline
        J Offline
        john_hobbyist
        wrote on last edited by john_hobbyist
        #3

        Yes, the main code:

               . . . 
                
                bar=self.menuBar() 
                file=bar.addMenu("Calculations")
                
                calc_1 = QAction("Calc_1", self) 
                file.addAction(calc_1) 
                file.triggered[QAction].connect(self.calculate_1) 
                
                calc_2 = QAction("Calc_2", self) 
                file.addAction(calc_2) 
                file.triggered[QAction].connect(self.calculate_2) 
                
                calc_3 = QAction("Calc_3", self) 
                file.addAction(calc_3) 
                file.triggered[QAction].connect(self.calculate_3) 
                     
                self.setLayout(mylayout) 
                self.setWindowTitle("menu demo") 
                        
                        
                . . . 
                
                
                def calculate_1(self):
                    print("Calculating 1...")
                    
                def calculate_2(self):
                    print("Calculating 2...")
                    
                def calculate_3(self):
                    print("Calculating 3...")
        
                   . . . 
        

        Any idea???

        JonBJ 1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Why are you connecting the menu three times rather than the specific action to their corresponding slot ?

          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
          2
          • J john_hobbyist

            Yes, the main code:

                   . . . 
                    
                    bar=self.menuBar() 
                    file=bar.addMenu("Calculations")
                    
                    calc_1 = QAction("Calc_1", self) 
                    file.addAction(calc_1) 
                    file.triggered[QAction].connect(self.calculate_1) 
                    
                    calc_2 = QAction("Calc_2", self) 
                    file.addAction(calc_2) 
                    file.triggered[QAction].connect(self.calculate_2) 
                    
                    calc_3 = QAction("Calc_3", self) 
                    file.addAction(calc_3) 
                    file.triggered[QAction].connect(self.calculate_3) 
                         
                    self.setLayout(mylayout) 
                    self.setWindowTitle("menu demo") 
                            
                            
                    . . . 
                    
                    
                    def calculate_1(self):
                        print("Calculating 1...")
                        
                    def calculate_2(self):
                        print("Calculating 2...")
                        
                    def calculate_3(self):
                        print("Calculating 3...")
            
                       . . . 
            

            Any idea???

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #5

            @john_hobbyist

            file.triggered[QAction].connect(self.calculate_1) 
            file.triggered[QAction].connect(self.calculate_2) 
            file.triggered[QAction].connect(self.calculate_3) 
            

            This sets up so that any triggered action on file (your whole menu with all its actions) will call each/all of your slots. That is not what the code you chose to copy from did, it had one

            file.triggered[QAction].connect(self.processtrigger)
            

            Either do it that way, or more likely just connect each QAction to its slot. EDIT E.g. write calc_1.triggered.connect(self.calculate_1) instead of file.triggered[QAction].connect(self.calculate_1).

            1 Reply Last reply
            1

            • Login

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