setGeometry 在 pyqt5 中不起作用?

setGeometry not workingin pyqt5?

我正在尝试以百分比方式设置 QWidget 的宽度和高度。但是因为我们不能使用 QSS 做类似 min-height:20% 的事情。我尝试使用 setGeometry 函数并在 window 调整大小事件中调用此函数,但不知何故它不起作用,下面是我的代码:

class mainwindow(QMainWindow):
    def __init__(self):
        super().__init__()

        stylesheet = """
                    #mainwindow{
                        border: 0px;
                        background-color: #222;
                    }
                """
        scanelestylesheet = """
                    #scanelecontainer{
                        border: 1px solid #1a1a1a;
                        background-color: #222;
                    }
                """
        scanstusconstylesheet = """
                    #scanstuscon{
                        border: 1px solid #1a1a1a;
                        background-color: #fff;
                    }
                """


        container = QWidget()
        container.setObjectName("mainwindow")
        container.setContentsMargins(100, 100, 100, 100)
        self.setCentralWidget(container)
        self.setStyleSheet(stylesheet)
        self.setWindowTitle("Neehack")

        widget = QWidget()
        widget.setObjectName("scanelecontainer")
        widget.setAutoFillBackground(True)
        lay = QVBoxLayout(container)
        lay.addWidget(widget)
        effect = QGraphicsDropShadowEffect(
            offset=QPoint(3, 3), blurRadius=25, color=QColor("#1a1a1a")
        )
        widget.setGraphicsEffect(effect)
        widget.setGeometry(0, 0,200, 20)
        widget.setStyleSheet(scanelestylesheet)

        scanstuscon = QWidget()
        scanstuscon.setObjectName("scanstuscon")
        scanstuscon.setAutoFillBackground(False)
        scanstusconlay = QVBoxLayout(widget)
        scanstusconlay.addWidget(scanstuscon)
        scanstuscon.setGeometry(10,10, 200,200)
        scanstuscon.setStyleSheet(scanstusconstylesheet)

        self.resize(1200, 800)

如何根据其父项的百分比设置 QWidget 大小,或者为什么 setGeomtry 在这里不起作用?

布局的 objective 用于处理小部件的几何形状,因此 setGeometry 将不起作用。另一方面,使用 setGeometry 修改大小和 resizeEvent 来确定大小的百分比是不必要的,因为在布局中设置拉伸因子就足够了:

class Mainwindow(QMainWindow):
    def __init__(self):
        super().__init__()

        stylesheet = """
                    #mainwindow{
                        border: 0px;
                        background-color: #222;
                    }
                """
        scanelestylesheet = """
                    #scanelecontainer{
                        border: 1px solid #1a1a1a;
                        background-color: #222;
                    }
                """
        scanstusconstylesheet = """
                    #scanstuscon{
                        border: 1px solid #1a1a1a;
                        background-color: #fff;
                    }
                """

        container = QWidget()
        container.setObjectName("mainwindow")
        container.setContentsMargins(100, 100, 100, 100)
        self.setCentralWidget(container)
        self.setStyleSheet(stylesheet)
        self.setWindowTitle("Neehack")

        widget = QWidget()
        widget.setObjectName("scanelecontainer")
        widget.setAutoFillBackground(True)
        lay = QVBoxLayout(container)
        lay.addWidget(widget)
        effect = QGraphicsDropShadowEffect(
            offset=QPoint(3, 3), blurRadius=25, color=QColor("#1a1a1a")
        )
        widget.setGraphicsEffect(effect)
        widget.setStyleSheet(scanelestylesheet)

        scanstuscon = QWidget()
        scanstuscon.setObjectName("scanstuscon")
        scanstuscon.setAutoFillBackground(False)
        scanstuscon.setStyleSheet(scanstusconstylesheet)

        # 20% = 20/(20 + 80)
        lay = QVBoxLayout(widget)
        lay.addWidget(scanstuscon, stretch=20)
        lay.addStretch(80)

        self.resize(1200, 800)