[prev in list] [next in list] [prev in thread] [next in thread] 

List:       pykde
Subject:    Re: [PyQt] Is there any screen manager?
From:       Maurizio Berti <maurizio.berti () gmail ! com>
Date:       2020-02-28 18:31:01
Message-ID: CAPn+-XSHsycVn53yowUbTDcD=PhKvj1J7uvYrm5ANL6nAaFtFw () mail ! gmail ! com
[Download RAW message or body]

[Attachment #2 (multipart/alternative)]


Il giorno ven 28 feb 2020 alle ore 19:01 Souvik Dutta Chowdhury <
souvik.viksou@outlook.com> 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

[Attachment #5 (text/html)]

<div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div \
dir="ltr"><div dir="ltr"><div class="gmail_quote"><div dir="ltr" \
class="gmail_attr">Il giorno ven 28 feb 2020 alle ore 19:01 Souvik Dutta Chowdhury \
&lt;<a href="mailto:souvik.viksou@outlook.com" \
target="_blank">souvik.viksou@outlook.com</a>&gt; ha scritto:<br></div><blockquote \
class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid \
rgb(204,204,204);padding-left:1ex"><div dir="auto">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?  \
</div></blockquote><div>  </div></div></div><div dir="ltr">You have to create a \
QWidget that has the toplevel window as a parent, and ensure that it always occupies \
the whole interface.<div>To do that, you have to use setGeometry() as soon as \
it&#39;s shown the first time, and ensure that the geometry is updated everytime the \
parent (the top level window) is resized.</div><div><br>If you&#39;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 &quot;special&quot; widgets, like toolbars or dock \
widgets.</div><div><br></div><div><div><font face="monospace">class \
LoginTest(QtWidgets.QMainWindow):</font></div><div><font face="monospace">      def \
__init__(self):</font></div><div><font face="monospace">            \
super().__init__()</font></div><div><font face="monospace">            central = \
QtWidgets.QWidget()</font></div><div><font face="monospace">            \
self.setCentralWidget(central)</font></div><div><font face="monospace">            \
layout = QtWidgets.QGridLayout(central)</font></div><div><font face="monospace">      \
for row in range(2):</font></div><div><font face="monospace">                  for \
col in range(2):</font></div><div><font face="monospace">                        \
group = QtWidgets.QGroupBox(&#39;Group {} {}&#39;.format(row + 1, col + \
1))</font></div><div><font face="monospace">                        \
group.setMinimumSize(160, 120)</font></div><div><font face="monospace">               \
layout.addWidget(group, row, col)</font></div><div><font \
face="monospace"><br></font></div><div><font face="monospace">            groupLayout \
= QtWidgets.QHBoxLayout(group)</font></div><div><font face="monospace">            \
loginButton = QtWidgets.QPushButton(&#39;Login&#39;)</font></div><div><font \
face="monospace">            \
groupLayout.addWidget(loginButton)</font></div><div><font face="monospace">           \
loginButton.clicked.connect(self.showLogin)</font></div><div><font \
face="monospace"><br></font></div><div><font face="monospace">            \
self.loginWidget = None</font></div><div><font \
face="monospace"><br></font></div><div><font face="monospace">      def \
showLogin(self):</font></div><div><font face="monospace">            self.loginWidget \
= QtWidgets.QWidget(self)</font></div><div><font face="monospace">            \
self.loginWidget.setAutoFillBackground(True)</font></div><div><font face="monospace"> \
layout = QtWidgets.QGridLayout(self.loginWidget)</font></div><div><font \
face="monospace">            layout.setColumnStretch(0, 1)</font></div><div><font \
face="monospace">            layout.setColumnStretch(2, 1)</font></div><div><font \
face="monospace">            layout.setRowStretch(0, 1)</font></div><div><font \
face="monospace">            layout.setRowStretch(2, 1)</font></div><div><font \
face="monospace"><br></font></div><div><font face="monospace">            formWidget \
= QtWidgets.QWidget()</font></div><div><font face="monospace">            \
layout.addWidget(formWidget, 1, 1)</font></div><div><font face="monospace">           \
formLayout = QtWidgets.QFormLayout(formWidget)</font></div><div><font \
face="monospace">            userEdit = QtWidgets.QLineEdit()</font></div><div><font \
face="monospace">            formLayout.addRow(&#39;User&#39;, \
userEdit)</font></div><div><font face="monospace">            passwordEdit = \
QtWidgets.QLineEdit()</font></div><div><font face="monospace">            \
passwordEdit.setEchoMode(passwordEdit.Password)</font></div><div><font \
face="monospace">            formLayout.addRow(&#39;Password&#39;, \
passwordEdit)</font></div><div><font face="monospace">            buttonBox = \
QtWidgets.QDialogButtonBox(QtWidgets.QDialogButtonBox.Ok|QtWidgets.QDialogButtonBox.Cancel)</font></div><div><font \
face="monospace">            formLayout.addRow(buttonBox)</font></div><div><font \
face="monospace"><br></font></div><div><font face="monospace">            login = \
lambda: self.login(userEdit.text(), passwordEdit.text())</font></div><div><font \
face="monospace"><br></font></div><div><font face="monospace">            \
passwordEdit.returnPressed.connect(login)</font></div><div><font face="monospace">    \
buttonBox.accepted.connect(login)</font></div><div><font face="monospace">            \
buttonBox.rejected.connect(self.hideLogin)</font></div><div><font \
face="monospace"><br></font></div><div><font face="monospace">            \
self.resizeLogin()</font></div><div><font face="monospace">            \
self.loginWidget.show()</font></div><div><font \
face="monospace"><br></font></div><div><font face="monospace">      def login(self, \
user, password):</font></div><div><font face="monospace">            if user != \
&#39;user&#39; or password != &#39;password&#39;:</font></div><div><font \
face="monospace">                  QtWidgets.QMessageBox.warning(self, &#39;Wrong \
credentials&#39;, &#39;Invalid user or password&#39;, \
QtWidgets.QMessageBox.Ok)</font></div><div><font face="monospace">            \
else:</font></div><div><font face="monospace">                  \
self.hideLogin()</font></div><div><font face="monospace"><br></font></div><div><font \
face="monospace">      def hideLogin(self):</font></div><div><font face="monospace">  \
self.loginWidget.hide()</font></div><div><font face="monospace">            \
self.loginWidget.deleteLater()</font></div><div><font face="monospace">            \
self.loginWidget = None</font></div><div><font \
face="monospace"><br></font></div><div><font face="monospace">      def \
resizeLogin(self):</font></div><div><div><font face="monospace">            if \
self.loginWidget:</font></div><div><font face="monospace">                  # resize \
while keeping menubar and statusbar visible:</font></div><div><font face="monospace"> \
self.loginWidget.setGeometry(self.centralWidget().geometry())</font></div></div><div><font \
face="monospace"><br></font></div><div><font face="monospace">                  # \
alternatively, completely hide the whole interface:</font></div><div><font \
face="monospace"><div>                  # \
self.loginWidget.setGeometry(self.rect())</div><div><br></div></font></div><div><font \
face="monospace"><br></font></div><div></div><div><font face="monospace">      def \
resizeEvent(self, event):</font></div><div><font face="monospace">            \
super().resizeEvent(event)</font></div></div><div><font face="monospace">            \
self.resizeLogin()</font></div><div><font \
face="monospace"><br></font></div></div></div></div></div></div>-- <br><div \
dir="ltr">È difficile avere una convinzione precisa quando si parla delle ragioni \
del cuore. - &quot;Sostiene Pereira&quot;, Antonio Tabucchi<br><a \
href="http://www.jidesk.net" target="_blank">http://www.jidesk.net</a></div> \
</div></div>


[Attachment #6 (text/plain)]

_______________________________________________
PyQt mailing list    PyQt@riverbankcomputing.com
https://www.riverbankcomputing.com/mailman/listinfo/pyqt


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic