python - PyQT - referencing a widget within a specific tab -


for example, want button in tab clear textedit box in tab, (i know can directly want know how widget function) can't figure out how reference textedit:

(indentation messed here fine @ end)

#!/usr/bin/python  import sys pyqt4 import qtgui, qtcore  class cc(qtgui.qwidget):     def __init__(self):         super(cc, self).__init__()          self.initui()      def initui(self):          tab_widget = qtgui.qtabwidget(self)          ################## tab1         tab1 = qtgui.qwidget()         layout = qtgui.qvboxlayout(tab1)         ###### add widgets layout          clear = qtgui.qpushbutton('clear results box', tab1)         clear.resize(clear.sizehint())         clear.move(50, 50)         clear.clicked.connect(self.clearit)          results = qtgui.qtextedit(tab1)         results.move(250, 50)         results.setfixedwidth(450)         results.setfixedheight(350)         ######################          tab_widget.addtab(tab1, "tab 1")          mainlayout = qtgui.qvboxlayout()         mainlayout.addwidget(tab_widget)          self.setgeometry(300, 300, 800, 500)         self.setfixedsize(800, 500)         self.setwindowtitle('crouton mn toolw v05')         self.setwindowicon(qtgui.qicon('drk.ico'))                  self.setlayout(mainlayout)              self.show()      def clearit(self):         tab1.results.clear()   ##### <<< here doesn't work, please :)         #self.results.clear()  def main():      app = qtgui.qapplication(sys.argv)     ex = cc()     sys.exit(app.exec_())   if __name__ == '__main__':     main()  

i have tried self.results.clear(), tab_widget.results.clear(), tab1.results.clear() , dozen other variations no joy, missing obvious. (like brain maybe.)

i have found answer stupid grasp should do: how reference widget inside specific tab of qtabwidget?

any appreciated!

edit: can work sticking 'global results' inside initui , calling 'results.clear()' function there surely more correct way?

first, tab layout stuff work? know there different ways of doing this, never set layout on tab1 widget. this:

tab1 = qtgui.qwidget() layout = qtgui.qvboxlayout() tab1.setlayout(layout) 

second, link referenced talking getting individual tab widget qtabwidget. in case getting tab1 tab_widget.

third, in clearit function can't reference old variable initui function. if aren't familiar python variable scope may want up. reference clearit function either has global (usually unnecessary or bad practice) or has passed argument. since qwidgets objects provided reference object instance itself, self. reference results object (not tested):

results = self.layout.children()[0].widget(0).layout.children()[1] results.clear() 

there other ways children, bottom line traversing series of layouts , widgets object. easiest solution (which may "direct" method call it) put in initui function after working results object:

self.results = results 

then in clearit function:

self.results.clear() 

if there more "qt" ways of doing this, let me know. consider "pythonic" way it, maybe that's me.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -