Changing tabs push the action to be executed more than 1 time ( 2 after 3 after 4 )
-
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")
-
Hi,
You are re-creating the actions each time onChange is called.
-
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. -
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.
-
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.
-
Then why not put the active part inside that widget ?
-
Hello,
You mean that you put actions in the class InterfaceTemplateDialog where I define the widget ?
-
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")
-
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")