python - PySide multi-window, get QStackWidget to work -



python - PySide multi-window, get QStackWidget to work -

i need create multi-window gui, first tried qwidgets, find qstackwidget tool need use. im trying to, ive got problems. time.

class mainwindow(qmainwindow): def __init__(self): super(mainwindow,self).__init__() self.mainwidget = mainwidget() self.searchwidget = searchwidget() self.swidget = qstackedwidget() self.swidget.addwidget(self.mainwidget) self.swidget.addwidget(self.searchwidget) self.initui()

and calling setcurrentwidget sub_widget class:

class mainwidget(qwidget): def __init__(self, parent=mainwindow): super(mainwidget,self).__init__() self.initui() def initui(self): searchbutton = qpushbutton('searchbutton',self) optionbutton = qpushbutton('optionbutton',self) quitbutton = qpushbutton('quitbutton',self) listbutton = qpushbutton('listbutton',self) searchbutton.clicked.connect(self.gosearch) hbox = qhboxlayout() hbox.addwidget(listbutton) hbox.addwidget(quitbutton) vbox = qvboxlayout() vbox.addstretch(1) vbox.addwidget(searchbutton) vbox.addwidget(optionbutton) vbox.addlayout(hbox) self.setlayout(vbox) def gosearch(self): self.parent().swidget.setcurrentwidget(self.parent().searchwidget)

ive got message ide:

self.parent().swidget.setcurrentwidget(self.parent().searchwidget) attributeerror: 'pyside.qtgui.qstackedwidget' object has no attribute 'swidget'

what thing im doing wrong?

i'm going comment on code posted here: http://pastebin.com/fbfs1x5m

an of import thing know can set widgets within widgets , on. example:

class widget(qwidget): def __init__(self, parent=none): layout = qvboxlayout(self) childwidget = qwidget(parent=self) layout.addwidget(childwidget)

just quick note: don't need setlayout if pass self main layout constructor - via docs.

anyways, i'm trying illustrate here qstackedwidget , searchwidget shouldn't part of mainwindow, should live within own relevant widget handle switching between qstackedwidget pages.

for illustration mainwindow.__init__ this:

class mainwindow(qmainwindow): def __init__(self): super(mainwindow, self).__init__() self.mainwidget = mainwidget(self) self.setcentralwidget(self.mainwidget) self.initui()

your mainwidget like:

class mainwidget(qtgui.qwidget): ... def initui(self): ... self.stack = qtgui.qstackedwidget(parent=self) self.searchwidget = searchwidget(parent=self) self.searchwidget.searchbutton.clicked.connect(self.gosearch) self.backwidget = backwidget(parent=self) self.backwidget.backbutton.clicked.connect(self.goback) ... def gosearch(self): self.stack.setcurrentwidget(self.backwidget) def goback(self): self.stack.setcurrentwidget(self.searchwidget)

i've renamed of class names create little more sense (to me @ least). searchwidget old mainwidget. backwidget old searchwidget. next changes searchwidget same old mainwidget 1 exception - save reference search button can access in mainwidget class seen above (when connect signals our slots). same button in backwidget.

the 2 renamed "child" widgets:

class searchwidget(qtgui.qwidget): ... def initui(self): self.searchbutton = qtgui.qpushbutton('searchbutton', parent=self) optionbutton = qtgui.qpushbutton('optionbutton', parent=self) quitbutton = qtgui.qpushbutton('quitbutton', parent=self) listbutton = qtgui.qpushbutton('listbutton', parent=self) vbox = qtgui.qvboxlayout(self) vbox.addstretch(1) vbox.addwidget(self.searchbutton) vbox.addwidget(optionbutton) hbox = qtgui.qhboxlayout() hbox.addwidget(listbutton) hbox.addwidget(quitbutton) vbox.addlayout(hbox) class backwidget(qtgui.qwidget): ... def initui(self): self.backbutton = qtgui.qpushbutton('goback', parent=self)

so have like:

mainwindow |---mainwidget |---qstackedwidget |---searchwidget |---backwidget

you can find total working code here.

python constructor parent-child pyside qstackedwidget

Comments

Popular posts from this blog

assembly - What is the addressing mode for ld, add, and rjmp instructions? -

vowpalwabbit - Interpreting Vowpal Wabbit results: Why are some lines appended by "h"? -

Is there a way to convert an HTML page styled with Bootstrap CSS into email-compatible html? -