简体   繁体   中英

Signal Not Emitted when selecting tab in Pyqt

I have a tab widget created with 3 tabs in it. I need to emit signals when I change the tab, Looking at the pyQt documentation currentChanged() is the signal that is emitted on Changing the tab but it does not work for me. So need your help on understanding the same,

MY code is as follows:

main_tab_widget = QTabWidget()

    #Creating Tabs
    self.run_tab = QWidget()
    self.cc_tab = QWidget()
    self.result_tab = QWidget()

    #Adding Tabs to Tab Widget
    main_tab_widget.addTab(self.run_tab, "RUN")
    main_tab_widget.addTab(self.cc_tab, "Config Creator")
    main_tab_widget.addTab(self.result_tab, "Result")

    #Layout Creation
    main_layout = QVBoxLayout()
    main_layout.addWidget(main_tab_widget)

    self.connect(self.cc_tab, SIGNAL('currentChanged(int)'), self.pseudofunction)
    self.RunTab()
    self.setLayout(main_layout)
    #self.configcreatortab()
    #self.resulttab()

def pseudofunction(self):
    print 'Inside Pseudo Function'

You're attempting to wire up the currentChanged signal from the wrong object. main_tab_object is the QTabWidget that emits this signal, but you're attempting to wire up this signal from self.cc_tab , a QWidget in one of the tabs.

Replace the line

    self.connect(self.cc_tab, SIGNAL('currentChanged(int)'), self.pseudofunction)

with

    self.connect(main_tab_widget, SIGNAL('currentChanged(int)'), self.pseudofunction)

EDIT : There is no way to use the signals/slots mechanism to connect a different function to each tab of a QTabWidget. If you want to call different functions when different tabs are selected, you'll have to write another function that calls the relevant function depending on which tab was selected. For example:

def selector(self, selected_index):
    if selected_index == 0:
        self.some_function_for_run_tab()
    elif selected_index == 1:
        self.some_function_for_cc_tab()
    elif selected_index == 2:
        self.some_function_for_result_tab()

You then connect the currentChanged signal of the QTabWidget to this function.

You are correct that QTabBar also has the currentChanged signal, but your usage of it in your second comment, by replacing QWidget() with QTabBar() in your code above, will not achieve what you want.

A QTabBar acts only as the bar that contains a collection of tabs. (In fact, the QTabWidget uses a QTabBar internally for its tab bar.) So, putting a QTabBar within a QTabWidget gives you tabs within tabs. I don't believe this is what you want. Furthermore I suspect you're not adding any tabs to the QTabBars. The QTabBars do have the currentChanged signal, and your code will correctly connect these signals, but these signals will never fire because the QTabBars contain no tabs to change.

The fact that these QTabBars may themselves be inside a QTabWidget is irrelevant. They only see changes to their own set of tabs and hence can only fire signals for changes to their own set of tabs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM