Search code examples
pythonpyqtpyqt5window

How to show widgets in new window witch is widget without using layouts


I don't find anything how to show widgets like qlabel qpushbutton and qtextedit in new widget window.Here is my code:

This is the class where I create new widget window witch show but without the widgets.

class PasswordForm(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("Password")
        self.setFixedWidth(400)
        self.setFixedHeight(300)
        self.label = QLabel("Enter Admin Password:")
        self.label.setGeometry(QtCore.QRect(80, 20, 211, 31))
        self.label.setMinimumSize(QtCore.QSize(0, 31))
        self.label.setObjectName("label")
        self.admin_pass_input = QLineEdit()
        self.admin_pass_input.setGeometry(QtCore.QRect(80, 50, 161, 25))
        self.admin_pass_input.setEchoMode(QtWidgets.QLineEdit.Password)
        self.admin_pass_input.setObjectName("admin_pass_input")
        self.pushButton = QPushButton("OK")
        self.pushButton.setGeometry(QtCore.QRect(120, 80, 89, 25))

This is the function where I call the new window to show:

 def clicked(self, action):
    self.p_form = PasswordForm()
    self.p_form.show()

MineWindows is generated by PyQT5 Designer.


Solution

  • Basic rule of Qt: A QWidget (QLineEdit, QPushButton, etc) will be part of another QWidget if it is a child of this or a child of a QWidget that is part of the QWidget.

    So the task of a layout is not only to manage the geometry of the QWidgets but also to establish as the parent of the QWidgets that manages the QWidget where it is placed.

    In your case you just have to pass the parent:

    self.label = QLabel("Enter Admin Password:", self)
    
    self.admin_pass_input = QLineEdit(self)
    
    self.pushButton = QPushButton("OK", self)