Re: Is there any screen manager?

Maurizio Berti <[email protected]>
Newsgroups gmane.comp.python.pyqt-pykde
Message-ID <CAPn+-XSHsycVn53yowUbTDcD=PhKvj1J7uvYrm5ANL6nAaFtFw@mail.gmail.com>
Il giorno ven 28 feb 2020 alle ore 19:01 Souvik Dutta Chowdhury <
[email protected]> ha scritto:

> Hi i am creating an application in which i have a button that prompts you
> to add a user. Now currently whenever the button is clicked it opens a new
> window. But i dont want to open it as a second window rather keep it inside
> the same window. How can I do it?
>

You have to create a QWidget that has the toplevel window as a parent, and
ensure that it always occupies the whole interface.
To do that, you have to use setGeometry() as soon as it's shown the first
time, and ensure that the geometry is updated everytime the parent (the top
level window) is resized.

If you're using a QMainWindow, you might want to use the central widget as
a reference, so that the menubar and statusbar will always be visible,
alternatively you can use self.rect(), which works for other types of
widgets/dialogs too, but might also be better for QMainWindow if you have
other "special" widgets, like toolbars or dock widgets.

class LoginTest(QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()
        central = QtWidgets.QWidget()
        self.setCentralWidget(central)
        layout = QtWidgets.QGridLayout(central)
        for row in range(2):
            for col in range(2):
                group = QtWidgets.QGroupBox('Group {} {}'.format(row + 1,
col + 1))
                group.setMinimumSize(160, 120)
                layout.addWidget(group, row, col)

        groupLayout = QtWidgets.QHBoxLayout(group)
        loginButton = QtWidgets.QPushButton('Login')
        groupLayout.addWidget(loginButton)
        loginButton.clicked.connect(self.showLogin)

        self.loginWidget = None

    def showLogin(self):
        self.loginWidget = QtWidgets.QWidget(self)
        self.loginWidget.setAutoFillBackground(True)
        layout = QtWidgets.QGridLayout(self.loginWidget)
        layout.setColumnStretch(0, 1)
        layout.setColumnStretch(2, 1)
        layout.setRowStretch(0, 1)
        layout.setRowStretch(2, 1)

        formWidget = QtWidgets.QWidget()
        layout.addWidget(formWidget, 1, 1)
        formLayout = QtWidgets.QFormLayout(formWidget)
        userEdit = QtWidgets.QLineEdit()
        formLayout.addRow('User', userEdit)
        passwordEdit = QtWidgets.QLineEdit()
        passwordEdit.setEchoMode(passwordEdit.Password)
        formLayout.addRow('Password', passwordEdit)
        buttonBox =
QtWidgets.QDialogButtonBox(QtWidgets.QDialogButtonBox.Ok|QtWidgets.QDialogButtonBox.Cancel)
        formLayout.addRow(buttonBox)

        login = lambda: self.login(userEdit.text(), passwordEdit.text())

        passwordEdit.returnPressed.connect(login)
        buttonBox.accepted.connect(login)
        buttonBox.rejected.connect(self.hideLogin)

        self.resizeLogin()
        self.loginWidget.show()

    def login(self, user, password):
        if user != 'user' or password != 'password':
            QtWidgets.QMessageBox.warning(self, 'Wrong credentials',
'Invalid user or password', QtWidgets.QMessageBox.Ok)
        else:
            self.hideLogin()

    def hideLogin(self):
        self.loginWidget.hide()
        self.loginWidget.deleteLater()
        self.loginWidget = None

    def resizeLogin(self):
        if self.loginWidget:
            # resize while keeping menubar and statusbar visible:
            self.loginWidget.setGeometry(self.centralWidget().geometry())

            # alternatively, completely hide the whole interface:
            # self.loginWidget.setGeometry(self.rect())


    def resizeEvent(self, event):
        super().resizeEvent(event)
        self.resizeLogin()

-- 
È difficile avere una convinzione precisa quando si parla delle ragioni del
cuore. - "Sostiene Pereira", Antonio Tabucchi
http://www.jidesk.net

_______________________________________________
PyQt mailing list    [email protected]
https://www.riverbankcomputing.com/mailman/listinfo/pyqt
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.