Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Language Bindings
  4. App without main window - with many widgets showing one by one

App without main window - with many widgets showing one by one

Scheduled Pinned Locked Moved Language Bindings
5 Posts 5 Posters 4.2k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    her_dom
    wrote on last edited by
    #1

    I would like to have app without main window, but with many widgets showing one by one (dialogs won't work for me).

    I have following code:
    @# -- coding: utf-8 --
    from PyQt4 import QtCore, QtGui

    from start_window import Ui_Form
    from second_window import Ui_Form as Ui_Form_Second

    class StartForm(QtGui.QWidget):
    def init(self, parent=None):
    QtGui.QWidget.init(self, parent)
    self.ui = Ui_Form()
    self.ui.setupUi(self)
    self.ui.pushButton.clicked.connect(self.button)

    def button(self):
        secondWindow = SecondForm()
        secondWindow.show()
        self.hide()
    

    class SecondForm(QtGui.QWidget):
    def init(self, parent=None):
    QtGui.QWidget.init(self, parent)
    self.ui = Ui_Form_Second()
    self.ui.setupUi(self)

    if name == "main":
    app = QtGui.QApplication(sys.argv)
    myapp = StartForm()
    myapp.show()
    sys.exit(app.exec_())@

    When I press button app closes. How to pass execution of the app to the second widget? I am not interested in using QWizard and QStackedWidget.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      Then you have to either open the next widget before closing the last (because by default closing the "main" widget will end the application) or disable application ending on last widget closed and re-enable it when appropriate.

      Just curiosity, why doesn't any of the current Qt technology fit in your design ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • Q Offline
        Q Offline
        qurban_ali36
        wrote on last edited by
        #3

        Hi
        The problem with your current logic is that, you are saving the reference of "secondForm" in "secondWindow" which is a local variable and destroyed when execution of "button" function is completed. You have to put the reference of "secondForm" in "self.secondWindow", everything else is fine.

        1 Reply Last reply
        0
        • jazzycamelJ Offline
          jazzycamelJ Offline
          jazzycamel
          wrote on last edited by
          #4

          As Qurban says your problem is that you're "secondWindow" is going out of scope. What you're basically trying to do is create a managed stack of parentless windows (i.e. a QStackWidget without the actual widget). The following is a simplistic example that uses a subclass if QApplication to manage both the widgets and the transitions between them:

          @
          from PyQt4.QtCore import *
          from PyQt4.QtGui import *

          class Window(QWidget):
          next=pyqtSignal()
          prev=pyqtSignal()

          def __init__(self, title, **kwargs):
              QWidget.__init__(self, **kwargs)
          
              self.resize(320,240)
              self.setWindowTitle(title)
              l=QVBoxLayout(self)
              l.addWidget(QPushButton("Prev", self, clicked=self.prev))
              l.addWidget(QPushButton("Next", self, clicked=self.next))
          

          class Application(QApplication):
          def init(self, *args, **kwargs):
          QApplication.init(self, *args, **kwargs)

              self._index=None
              self._windows=[Window(t, next=self.next, prev=self.prev) for t in ["Window 1", "Window 2", "Window 3"]]
              self.next()
          
          @pyqtSlot()
          def prev(self):
              if self._index in (None, 0): return
          
              self._windows[self._index].hide()
              self._index-=1
              self._windows[self._index].show()
              self._windows[self._index].raise_()
          
          @pyqtSlot()
          def next(self):
              if self._index>=(len(self._windows)-1): return
              if self._index is not None:
                  self._windows[self._index].hide()
                  self._index+=1
              else: self._index=0
          
              self._windows[self._index].show()
              self._windows[self._index].raise_()
          

          if name=="main":
          from sys import argv, exit

          a=Application(argv)
          exit(a.exec_())
          

          @

          Hope this helps ;o)

          For the avoidance of doubt:

          1. All my code samples (C++ or Python) are tested before posting
          2. As of 23/03/20, my Python code is formatted to PEP-8 standards using black from the PSF (https://github.com/psf/black)
          1 Reply Last reply
          0
          • H Offline
            H Offline
            her_dom
            wrote on last edited by
            #5

            Thank you guys for your answers!

            I solved this by adding two lines (for sure it works, but is it OK?:):

            @# -- coding: utf-8 --
            from PyQt4 import QtCore, QtGui
            from start_window import Ui_Form
            from second_window import Ui_Form as Ui_Form_Second

            class StartForm(QtGui.QWidget):
            def init(self, parent=None):
            QtGui.QWidget.init(self, parent)
            self.ui = Ui_Form()
            self.ui.setupUi(self)
            self.child_windows = None # this one
            self.ui.pushButton.clicked.connect(self.button)

            def button(self):
                secondWindow = SecondForm()
                secondWindow.show()
                self.child_windows = secondWindow # this one
                self.hide()
            

            class SecondForm(QtGui.QWidget):
            def init(self, parent=None):
            QtGui.QWidget.init(self, parent)
            self.ui = Ui_Form_Second()
            self.ui.setupUi(self)

            if name == "main":
            app = QtGui.QApplication(sys.argv)
            myapp = StartForm()
            myapp.show()
            sys.exit(app.exec_())@

            I am just wondering if this approach doesn't leave any garbages in memory, like closed forms?

            I ma not using QWizard because this is going to be something more than widget with "Next" and "Previous" buttons. Nor QStackedWidget since, at some point, I want to have more than one visible widget at a time.

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved