如何添加嵌套层和小部件?

问题描述

我在处理以下程序的布局时遇到问题。我想将 QHBoxLayout 插入 QVBoxLayout 的第一行(或任何一行)。目标是实现这样的目标:

plan

这是我目前所拥有的:

import sys,os
from PyQt5.QtWidgets import *
from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5.QtGui import *
from PyQt5.QtCore import *

class MainWindow(QMainWindow):

    def __init__(self,*args,**kwargs):
        super(MainWindow,self).__init__(*args,**kwargs)

        self.setFixedSize(600,600)

        layout = QVBoxLayout()
        layout.setContentsMargins(0,0)
        layout.setAlignment(Qt.AlignLeft | Qt.AlignTop)

        self.webEngineView = QWebEngineView()
        self.loadPage()
        self.webEngineView.setFixedSize(QSize(600,280))
        layout.addWidget(self.webEngineView)

        button = QPushButton("CLICK",self) 
        button.setFixedSize(QSize(50,80))
        button.clicked.connect(self.clickme)
        button.setStyleSheet("""
        background-color: red;
        """)
        layout.addWidget(button)

        # without this part the code works fine:
        titlelayout = QHBoxLayout()
        titlelayout.setContentsMargins(0,0)
        titlelayout.setAlignment(Qt.AlignRight | Qt.AlignTop)
        button1 = QPushButton("CLICK",self) 
        button1.setFixedSize(QSize(40,40))
        titlelayout.addWidget(button1)
        titlelayout.addLayout(layout)

        widget = QWidget()
        widget.setLayout(layout)
        self.setCentralWidget(widget)

    def loadPage(self):

        html = '''
        <html style="width: 100%; height: 100%; margin: 0; padding: 0;">
        <body style="overflow: hidden; width: 100%; height: 100%; margin: 0; padding: 0; background-color: #E6E6FA;">
        
        </body>
        </html>'''

        self.webEngineView.setHtml(html)

    def clickme(self): 
        print("clicked")

app = QApplication(sys.argv)
app.setStyleSheet("QMainWindow {background-color:white}")
window = MainWindow()
window.show()
sys.exit(app.exec_())

解决方法

在我的设计中,我不喜欢将布局放置在另一个布局中,而是使用 QWidget 作为容器作为中介:

import sys

from PyQt5.QtCore import QSize,Qt
from PyQt5.QtWidgets import (
    QApplication,QGridLayout,QHBoxLayout,QLineEdit,QMainWindow,QPushButton,QVBoxLayout,QWidget,)
from PyQt5.QtWebEngineWidgets import QWebEngineView


class MainWindow(QMainWindow):
    def __init__(self,*args,**kwargs):
        super(MainWindow,self).__init__(*args,**kwargs)

        self.setFixedSize(600,600)

        self.webEngineView = QWebEngineView()

        button1 = QPushButton("CLICK")
        button1.setFixedSize(QSize(50,80))
        button1.setStyleSheet("""background-color: red;""")
        button1.clicked.connect(self.clickme)

        button2 = QPushButton("CLICK")
        button2.setFixedSize(QSize(50,80))

        button3 = QPushButton("CLICK")
        button3.setFixedSize(QSize(50,80))

        title_widget = QWidget()
        hlay = QHBoxLayout(title_widget)
        hlay.addWidget(button1,alignment=Qt.AlignLeft)
        hlay.addStretch()
        hlay.addWidget(button2,alignment=Qt.AlignRight)
        hlay.addWidget(button3,alignment=Qt.AlignRight)

        footer_widget = QWidget()

        hlay2 = QGridLayout(footer_widget)
        hlay2.addWidget(QPushButton("Foo"),0)
        hlay2.addWidget(QLineEdit("Bar"),1,2)

        central_widget = QWidget()
        self.setCentralWidget(central_widget)

        vlayout = QVBoxLayout(central_widget)
        vlayout.addWidget(title_widget)
        vlayout.addWidget(self.webEngineView,stretch=1)
        vlayout.addWidget(footer_widget)

        self.loadPage()

    def loadPage(self):

        html = """
        <html style="width: 100%; height: 100%; margin: 0; padding: 0;">
        <body style="overflow: hidden; width: 100%; height: 100%; margin: 0; padding: 0; background-color: #E6E6FA;">
        
        </body>
        </html>"""

        self.webEngineView.setHtml(html)

    def clickme(self):
        print("clicked")


app = QApplication(sys.argv)
app.setStyleSheet("QMainWindow {background-color:white}")
window = MainWindow()
window.show()
sys.exit(app.exec_())

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...