请问PySide中为何一点要设置setLayout,布局才会生效?
import sys
from PySide6.QtWidgets import (QLineEdit, QPushButton, QApplication,
QVBoxLayout, QDialog)
class Form(QDialog):
def __init__(self, parent=None):
super(Form, self).__init__(parent)
# Create widgets
self.edit = QLineEdit("Write my name here")
self.button = QPushButton("Show Greetings")
# Create layout and add widgets
self.layout = QVBoxLayout()
self.layout.addWidget(self.edit)
self.layout.addWidget(self.button)
# Set dialog layout
self.setLayout(self.layout) # 为何这里一定要运行这一行setLayout才有效?
# Add button signal to greetings slot
self.button.clicked.connect(self.greetings)
# Greets the user
def greetings(self):
print(f"Hello {self.edit.text()}")
if __name__ == '__main__':
# Create the Qt Application
app = QApplication(sys.argv)
# Create and show the form
form = Form()
form.show()
# Run the main Qt loop
sys.exit(app.exec())
我以为是:self.layout 这里就定义了layout。不必做其他的设置了呢。