【问题标题】:setGeometry not workingin pyqt5?setGeometry 在 pyqt5 中不起作用?
【发布时间】:2021-07-23 12:14:37
【问题描述】:

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

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 在这里不起作用?

【问题讨论】:

    标签: python pyqt pyqt5


    【解决方案1】:

    布局的目的是处理小部件的几何形状,因此 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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-08-30
      • 2018-06-10
      • 2022-01-21
      • 1970-01-01
      • 1970-01-01
      • 2014-11-26
      相关资源
      最近更新 更多