qt - Can't add widget from event call -
i want add button during event, reason doesn't work.
if run code, tempfunc function run during creation , button created.
from pyside.qtgui import * pyside.qtcore import * import sys import math class example(qwidget): def __init__(self, val): super(example,self).__init__() self.scrollareaconstraint = qlabel() self.scrollareaconstraint.setfixedsize(qsize(400,400)) self.scroll = qscrollarea() self.scroll.setwidget(self.scrollareaconstraint) self.scroll.setwidgetresizable(true) layout = qvboxlayout(self) layout.addwidget(self.scroll) self.countslider = qslider() self.countslider.setorientation(qt.orientation(1)) layout.addwidget(self.countslider) self.tempfunc() #this create button!! def tempfunc(self): print "slider pressed!!! add button" qpushbutton(self.scrollareaconstraint) if __name__ == '__main__': import sys app = qapplication(sys.argv) window = example(25) window.setgeometry(500, 500, 500, 400) window.show() sys.exit(app.exec_())
however running this, pressing slider cause same tempfunc function run, button not created.
from pyside.qtgui import * pyside.qtcore import * import sys import math class example(qwidget): def __init__(self, val): super(example,self).__init__() self.scrollareaconstraint = qlabel() self.scrollareaconstraint.setfixedsize(qsize(400,400)) self.scroll = qscrollarea() self.scroll.setwidget(self.scrollareaconstraint) self.scroll.setwidgetresizable(true) layout = qvboxlayout(self) layout.addwidget(self.scroll) self.countslider = qslider() self.countslider.setorientation(qt.orientation(1)) layout.addwidget(self.countslider) #self.tempfunc() #<----disabled!! self.countslider.sliderpressed.connect(self.tempfunc) def tempfunc(self): print "slider pressed!!! add button" qpushbutton(self.scrollareaconstraint) if __name__ == '__main__': import sys app = qapplication(sys.argv) window = example(25) window.setgeometry(500, 500, 500, 400) window.show() sys.exit(app.exec_())
why button not created when not being called directly "init"?
the button is created, code nothing useful it, explains why "doesn't work".
i'm guessing that, since make scrollareaconstraint
parent of these buttons, expecting them appear inside scroll-area. scrollareaconstraint
qlabel
, cannot act container other widgets.
so make scrollareaconstraint
qwidget
, give layout, , add buttons layout:
self.scrollareaconstraint = qwidget() self.scrollareaconstraint.setlayout(qvboxlayout()) ... def tempfunc(self): button = qpushbutton(self.scrollareaconstraint) self.scrollareaconstraint.layout().addwidget(button)
Comments
Post a Comment