gowthami
gowthami

Reputation: 239

How to call the first main window class from the second mainwindow class uisng pyqt4

Here is my example i want to call my first window from the second window class.when i click the back button in second window my behind main window come front.But i am not getting idea to call the first window.

Given below is my example:

import sys
from PyQt4.QtCore    import *
from PyQt4.QtGui     import *

class Window(QMainWindow):
    def __init__(self):
        super(Window,self).__init__()
        self.setWindowTitle("TeahcherPortal")
        self.setGeometry(50,50,800,600)

        self.FirstWindow()

    def FirstWindow(self):
        btn = QPushButton("Login", self)
        btn.clicked.connect(self.secondPage)    # - (SecondPage())
        btn.move(400,300)

        self.showFullScreen()

    def secondPage(self):
        self.secondWindow = SecondPage()
        self.secondWindow.showFullScreen()

class SecondPage(QMainWindow,Window):#how to inheritence the main window properties
    def __init__(self,parent=None):
        super(SecondPage,self).__init__(parent)
        self.setWindowTitle("SecondPage")
        self.secondwindow()
        self.showFullScreen()

    def secondwindow(self):
        btn = QPushButton("back", self)
        btn.clicked.connect(self.FirstWindow)    # - (SecondPage())
        btn.move(400,300)
    def FirstWindow(self):
        print "here i want to call the first window login button"


def run():
    app = QApplication(sys.argv)
    GUI = Window()
    sys.exit(app.exec_())

run()

Upvotes: 0

Views: 41

Answers (1)

musicamante
musicamante

Reputation: 48424

If you want a reference to other objects, it's better to have a "main" object that collects them and use it to interact between them.
Also, it's better to avoid using multiple QMainWindows, unless you really need all features a QMainWindow has, like toolbars, dock widgets, status bars, menus, etc. And the last two can be added anyway using layouts.
Keep in mind that you can even use a single window and use a QTabWidget or, better, a QStackedWidget, which will help you in "centralize" all in a single interface without the need to have multiple window classes. Another possibility is to use QWizard, but it's interface is a bit more complex.

That said, you cannot have references to other instancies from the class declaration, nor it's good practice to use it, at least for your means.

In this example, I'm subclassing QApplication to use it as a "parent controller" for interactions between all pages. This is just one way of doing it, but it's a good way to do it whenever you don't actually have one and unique "main" window. Of course, you could use any other object, as long as you have a unique reference along all your widgets/windows.

from PyQt4 import QtCore, QtGui

class MyApplication(QtGui.QApplication):
    def __init__(self, *args, **kwargs):
        super(MyApplication, self).__init__(*args, **kwargs)
        self.windows = []
        self.currentIndex = 0

    def addWindow(self, newWindow):
        if self.windows:
            # enable next button of the last window, if one exists...
            self.windows[-1].nextButton.setEnabled(True)
            # and enable the back button of the new one
            newWindow.backButton.setEnabled(True)
        self.windows.append(newWindow)
        newWindow.goBack.connect(self.goBack)
        newWindow.goNext.connect(self.goNext)
        if len(self.windows) == 1:
            self.windows[-1].show()

    def goBack(self):
        self.goToWindow(self.currentIndex - 1)

    def goToWindow(self, index):
        # ensure that the index is between the range
        if not 0 <= index <= len(self.windows) - 1:
            return
        # hide the current window, set the new index and show the new one
        self.windows[self.currentIndex].hide()
        self.currentIndex = index
        self.windows[self.currentIndex].show()

    def goNext(self):
        self.goToWindow(self.currentIndex + 1)

class BaseWindow(QtGui.QWidget):
    goBack = QtCore.pyqtSignal()
    goNext = QtCore.pyqtSignal()

    def __init__(self, title):
        super(BaseWindow, self).__init__()
        self.setWindowTitle(title)
        mainLayout = QtGui.QVBoxLayout()
        self.setLayout(mainLayout)

        # create a layout for the actual contents of the window
        self.widgetLayout = QtGui.QGridLayout()
        mainLayout.addLayout(self.widgetLayout)

        # add a layout for the buttons, placing it at the bottom
        buttonLayout = QtGui.QHBoxLayout()
        mainLayout.addLayout(buttonLayout)

        # the navigation buttons are disabled by default (see above)
        self.backButton = QtGui.QPushButton('Back')
        buttonLayout.addWidget(self.backButton)
        self.backButton.setEnabled(False)
        self.backButton.clicked.connect(self.goBack)

        self.nextButton = QtGui.QPushButton('Next')
        buttonLayout.addWidget(self.nextButton)
        self.nextButton.setEnabled(False)
        self.nextButton.clicked.connect(self.goNext)

class LoginPage(BaseWindow):
    def __init__(self):
        super(LoginPage, self).__init__('Login')
        # add widgets to the "BaseWindow" inherited class' widgetLayout
        self.userEdit = QtGui.QLineEdit()
        self.widgetLayout.addWidget(self.userEdit)
        self.passwordEdit = QtGui.QLineEdit()
        self.widgetLayout.addWidget(self.passwordEdit)

class SecondPage(BaseWindow):
    def __init__(self):
        super(SecondPage, self).__init__('Second page')
        self.widgetLayout.addWidget(QtGui.QLabel('Hello!'))


if __name__ == '__main__':
    import sys
    app = MyApplication(sys.argv)

    loginPage = LoginPage()
    app.addWindow(loginPage)

    secondPage = SecondPage()
    app.addWindow(secondPage)
    sys.exit(app.exec_())

Upvotes: 1

Related Questions