Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Language Bindings
  4. Changing tabs push the action to be executed more than 1 time ( 2 after 3 after 4 )
Forum Updated to NodeBB v4.3 + New Features

Changing tabs push the action to be executed more than 1 time ( 2 after 3 after 4 )

Scheduled Pinned Locked Moved Solved Language Bindings
pyqt
9 Posts 2 Posters 3.9k 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by A Former User
    #1

    Hello,
    I have a tabwidget with several tabs added dynamically(having the same content).

    I execute a simple action on a button (writing in QTextBrowser). I can execute this action on each tab.

    For example, I execute on tab 1 then on tab 2 then I come back to tab 1 : the action will be executed 2 times ( the same print will be displayed two times in the QTextBrowser). Then, I go to tab 3 and come back to tab 1 and the action will be executed 3 times ... etc ...

    My code :

    self.tabs.blockSignals(True) 
    self.tabs.currentChanged.connect(self.onChange) 
    self.tabs.blockSignals(False) 
    ....
    
    def onChange(self):
    
            index = self.tabs.currentIndex()
            tab_title = self.tabs.tabText(index)
            tab = self.tabs.currentWidget()
    
            if tab_title != "All checked scenarios":
                scenario = tab_title.split('-')[1]
            else :
                scenario ="ALL"
    
            self.run_actions(tab,scenario)  
    
     def run_actions(self,tab,scenario):
    
        tab.generate_btn.clicked.connect(lambda : self.launch(tab,scenario))
        tab.run_tranus_btn.clicked.connect(lambda :self.run_tranus(tab,scenario))
        tab.check_all_btn.clicked.connect(lambda :self.check_all(tab,scenario))
    
    def run_tranus(self,tab,scenario):
        tab.console.append("Beginning of execution of basic TRANUS programs for scenario "+scenario)
        tab.console.append("Executing loop TRANUS for "+ `tab.spin_box.value()` +" iterations")
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      You are re-creating the actions each time onChange is called.

      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
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

        Yes this is my problem. I think about a solution to avoid that.
        My problem is I must know the tab selectionned to execute actions. That's why I put my actions in onChange.

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

          Like I already wrote in your other thread, you should use a QSignalMapper if you really want to go that way. Mapper that you will connect when you create your widget.

          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
          • ? Offline
            ? Offline
            A Former User
            wrote on last edited by
            #5

            I read about QSignal mapper, but my actions will be the same for all the tabs : example, if I click on a button in tab 1, the action (for example) prints on console, then if I click on the same button in tab 2, the action will be the same. The difference will be in printing the name of scenario which is the name of the tab or running a program for a scenario ... What I must know is what scenario to perform my actions.

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

              Then why not put the active part inside that widget ?

              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
              • ? Offline
                ? Offline
                A Former User
                wrote on last edited by
                #7

                Hello,

                You mean that you put actions in the class InterfaceTemplateDialog where I define the widget ?

                1 Reply Last reply
                0
                • ? Offline
                  ? Offline
                  A Former User
                  wrote on last edited by
                  #8

                  Hello again,

                  putting actions in my class InterfaceTemplateDialog doesn't solve my problem because actions are dependant to the name of the tab.

                  For example, when I click on a button and print to console, I must know the name (title of the tab).

                  I think about solution like that but it is not working :

                  def onChange(self):
                          
                          index = self.tabs.currentIndex()
                          tab_title = self.tabs.tabText(index)
                          tab = self.tabs.currentWidget()
                          
                          if tab_title != "All checked scenarios":
                              scenario = tab_title.split('-')[1]
                          else :
                              scenario ="ALL"
                  
                   return tab,scenario
                  

                  This function to get the tab selectioned.

                  tab = self.onChange()[0]
                  scenario = self.onChange()[1]
                  def run_actions(self,tab,scenario):
                          
                          tab.generate_btn.clicked.connect(lambda : self.launch(tab,scenario))
                          tab.run_tranus_btn.clicked.connect(lambda :self.run_tranus(tab,scenario))
                          tab.check_all_btn.clicked.connect(lambda :self.check_all(tab,scenario))
                          
                   def run_tranus(self,tab,scenario):
                          tab.console.append("Beginning of execution of basic TRANUS programs for scenario "+scenario)
                          tab.console.append("Executing loop TRANUS for "+ `tab.spin_box.value()` +" iterations")
                  
                  1 Reply Last reply
                  0
                  • ? Offline
                    ? Offline
                    A Former User
                    wrote on last edited by
                    #9

                    Hello again,

                    Finally, I found a solution : putting actions when creating tabs. Here is my code :

                    def add_new_tab(self,index,text):
                            
                            new_tab = InterfaceTemplateDialog()
                            self.tabs.addTab(new_tab,text)
                            self.tabs.setTabText(index,text)
                            
                            new_tab.generate_btn.clicked.connect(self.launch)
                            new_tab.run_tranus_btn.clicked.connect(self.run_tranus)
                            new_tab.check_all_btn.clicked.connect(self.check_all)
                    def onChange(self):
                            
                            index = self.tabs.currentIndex()
                            tab_title = self.tabs.tabText(index)
                            tab = self.tabs.currentWidget()
                            
                            if tab_title != "All checked scenarios":
                                scenario = tab_title.split('-')[1]
                            else :
                                scenario ="ALL"
                                
                            return [tab,scenario]
                    
                            
                        def run_tranus(self):
                            tab = self.onChange()[0]
                            scenario = self.onChange()[1]
                            tab.console.append("Beginning of execution of basic TRANUS programs for scenario "+scenario)
                            tab.console.append("Executing loop TRANUS for "+ `tab.spin_box.value()` +" iterations")
                    
                    1 Reply Last reply
                    0

                    • Login

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