Spawning one window from another window on button click in qt4

后端 未结 2 337
时光取名叫无心
时光取名叫无心 2021-01-27 16:11

I\'ve created two windows using qt4 designer, and would like to link them together. I put them both in a folder and created a file outside the directory, which I\'m importing th

相关标签:
2条回答
  • 2021-01-27 17:07

    You really need to reconsider the design of your application.

    Opening and closing multiple main windows in the manner you describe is ugly and completely unnecessary. Instead, you should have one main window and use a QStackedWidget to hold a sequence of pages that can navigated through using buttons.

    To experiment with this idea, create a new main window in Qt Designer and add a QStackedWidget to it (it's in the "Containers" section). Then open the two UIs you've already designed, and copy the widgets of each UI into separate pages of the stacked-widget.

    Once you've done that, make sure you give all the widgets descriptive names, because you will need to refer to them later when you start writing the logic for your program. The main script of your application should look something like this:

    from PyQt4 import QtCore, QtGui
    from mainwindow_ui import Ui_MainWindow
    
    class MainWindow(QtGui.QMainWindow, Ui_MainWindow):
        def __init__(self):
            super(MainWindow, self).__init__()
            self.setupUi(self)
            ...
            self.buttonNext.clicked.connect(self.handleButtonNext)
            self.buttonPrev.clicked.connect(self.handleButtonPrev)
    
        def handleButtonNext(self):
            index = self.stackedWidget.currentIndex() + 1
            if index < self.stackedWidget.count():
                self.stackedWidget.setCurrentWidget(index)
    
        def handleButtonPrev(self):
            index = self.stackedWidget.currentIndex() - 1
            if index >= 0:
                self.stackedWidget.setCurrentWidget(index)
    
    if __name__ == '__main__':
    
        import sys
        app = QtGui.QApplication(sys.argv)
        window = MainWindow()
        window.show()
        sys.exit(app.exec_())
    

    Obviously, the real logic for your program will be more sophisticated than this, but it should give you a general idea of how to go about things.

    PS:

    I have never used it myself, but you might also want to see if the QWizard class might be more suitable for your needs.

    0 讨论(0)
  • 2021-01-27 17:09

    Your question don't show much context but it seems you are new at PyQt and PyUIC tools.

    That you post aren't (or not should be) the real windows of your app. Those class are generated for pyuic or pyuic4.

    You must have something like:

    class RealWindow(QMainWindow):
        def __init__(parent=None):
            super(RealWindow, self).__init__(parent) # Call QMainWindow constructor.
            self.__ui = UI_MainWindow()              # Here is when you should use that class.
            self.__ui.setupUI(self)                  # And apply the properties to yout actual window
    
        def on_some_button_clicked(*args, **args)   # And here is where you do whatever you want to do when you press a button.
    

    On the other hand, I'm not sure is a good idea having two main windows in the same app. I don't know even if Qt allow that at all.

    I recommend you build one of your windows as a QDialog. Or build a single window with a set of controls you can hide or show at will.

    I also recommend you: a pyqt tutorial

    0 讨论(0)
提交回复
热议问题