qt - Handle signal from a widget whose reference is unkown -


say have implement widget a, , need handle signal sent widget c. widget doesn't have reference of widget c(at lease cumbersome get). what's proper way do?

here naive example:

class widgeta(qtgui.qwidget):     def __init__(self):         self._tab_widget = qtgui.qtabwidget()         self.layout().addwidget(_tab_widget)         self._tab_widget.addtab(widgetb())         self._tab_widget.addtab(...)      def show_another_tab(self, index):         self._tab_widget.setcurrentindex(index)  class widgetb(qtgui.qwidget):     def __init__(self):         self.layout().addwidget(widgetc())  class widgetc(qtgui.qwidget):     show_another_tab = qtcore.pyqtsignal()     def __init__(self):         button = qtgui.qpushbutton("show_another_tab")         button.clicked.connect(lambda: self.show_another_tab.emit(2)) 

so basically, have more 1 tab. there little button in 1 of tabs. when user clicked button want switch tab. sadly button child of child .. of widgeta, it's cumbersome me keep reference of button in widgeta. more on button may dynamically created @ runtime.

what's proper way use signal in situation? or should reference of widgeta in global variable(i don't want this, seems simplest way)?

getting reference needn't cumbersome if consistent setting parents. if did in example, do:

class widgeta(qtgui.qwidget):     def __init__(self):         ...         self._tab_widget.addtab(widgetb(self))      def show_another_tab(self, index):         self._tab_widget.setcurrentindex(index)  class widgetb(qtgui.qwidget):     def __init__(self, parent):         self.layout().addwidget(widgetc(self))  class widgetc(qtgui.qwidget):     show_another_tab = qtcore.pyqtsignal()     def __init__(self, parent):         ...         button.clicked.connect(             lambda: self.parent().parent().show_another_tab.emit(2)) 

but if want avoid long chains of parent() calls, more global approach might better. 1 way set qapp subclass of qapplication:

# before qtgui imported elsewhere qtgui.qapp = myapplication() 

you can provide accessor methods allow navigate widget hierarchy top-down:

       button.clicked.connect(             lambda: qtgui.qapp.tabmanager().setcurrentindex(2)) 

(nb: explicitly setting qapp isn't optional - won't able access attributes of subclass without doing that).


Comments

Popular posts from this blog

Payment information shows nothing in one page checkout page magento -

tcpdump - How to check if server received packet (acknowledged) -