将背景颜色添加到作为子布局的布局(在另一个布局内)
add background color to a layout which is a sublayout (inside another layout)
布局结构:
self.mainLayout = QGridLayout()
self.subLayout1 = QGridLayout()
self.subLayout2 = QVBoxLayout()
...
...
...
self.mainLayout.addLayout(subLayout1,0,0)
我试过:
self.mainLayout = QGridLayout()
self.panel = QWidget()
self.subLayout1 = QGridLayout(self.panel)
self.mainLayout.addLayout(subLayout1,0,0)
self.panel.setStylesheet("background-color: red;")
Error: QLayout::addChildLayout: layout "" already has a parent
这就是我设置 mainLayout 颜色的方式
self.setAutoFillBackground(True)
p = self.palette()
p.setColor(self.backgroundRole(), Qt.black)
self.setPalette(p)
显示subLayout1、subLayout2等时需要在背面
我该怎么做?
编辑:添加了 MCVE(最小、完整、可验证的示例)
from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel, QGridLayout
from PyQt5.QtWidgets import QWidget, QVBoxLayout, QHBoxLayout
from PyQt5.QtCore import Qt
class Window(QMainWindow):
def __init__(self, parent=None):
super(Window, self).__init__(parent)
self.setWindowTitle('MCVE')
'''Window Background'''
self.setAutoFillBackground(True)
p = self.palette()
p.setColor(self.backgroundRole(), Qt.darkYellow)
self.setPalette(p)
'''Core Layouts'''
self.mainLayout = QGridLayout()
self.picLayout = QHBoxLayout()
self.redditSubs = QVBoxLayout()
self.downloadBar = QHBoxLayout()
self.panel = QWidget()
self.profileInfo = QGridLayout(self.panel)
'''Nested Layout'''
self.mainLayout.addLayout(self.profileInfo, 0, 0)
self.mainLayout.addLayout(self.picLayout, 0, 1)
self.mainLayout.addLayout(self.redditSubs, 1, 0)
self.mainLayout.addLayout(self.downloadBar, 1, 1)
'''Widgets'''
self.display = QLabel('QHBoxLayout()')
self.download = QLabel('QHBoxLayout()')
self.subs = QLabel('QVBoxLayout()')
self.fileInfo = QLabel('QGridLayout()')
'''AddWidgets'''
self.picLayout.addWidget(self.display)
self.downloadBar.addWidget(self.download)
self.redditSubs.addWidget(self.subs)
self.profileInfo.addWidget(self.fileInfo, 0, 0)
'''Stylesheet'''
self.panel.setStyleSheet("background-color: red;")
'''Initiating mainLayout '''
self.window = QWidget()
self.window.setLayout(self.mainLayout)
self.setCentralWidget(self.window)
if __name__ == '__main__':
app = QApplication([])
w = Window()
w.showNormal()
app.exec_()
如上所说,代码错误QLayout::addChildLayout: layout "" already has a parent, 如果 Stylesheet 部分被注释掉,QGridLayout() 标签会出现在深黄色背景之上,我想要那个背景部分为红色。
QXLayout 不是视觉元素,而是 class 控制位置,尤其是分配的小部件的大小。因此,想要为不是视觉元素的 class 设置颜色是没有意义的。
另一方面,错误是因为以下2个代码是等价的:
x = QXLayout(w)
x = QXLayout()
w.setLayout(x)
在您的情况下,您已使用以下代码指示布局 "profileInfo" 属于 "panel":self.profileInfo = QGridLayout(self.panel)
,但随后您指示同一布局属于 "mainLayout" 通过以下代码:self.mainLayout.addLayout(self.profileInfo, 0, 0)
。因此,为避免警告,您必须通过将最后一个代码替换为 self.mainLayout.addWidget(self.panel, 0, 0)
。
更新:
如果您希望一个小部件位于另一个小部件内,那么它必须是第二个小部件的子项,因此 "fileInfo" 必须是 "panel" 的子项,但是既然您希望它占据父级的整个尺寸,那么你必须使用布局。
from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel, QGridLayout
from PyQt5.QtWidgets import QWidget, QVBoxLayout, QHBoxLayout
from PyQt5.QtCore import Qt
class Window(QMainWindow):
def __init__(self, parent=None):
super(Window, self).__init__(parent)
self.setWindowTitle("MCVE")
"""Window Background"""
self.setAutoFillBackground(True)
p = self.palette()
p.setColor(self.backgroundRole(), Qt.darkYellow)
self.setPalette(p)
"""Core Layouts"""
self.mainLayout = QGridLayout()
self.picLayout = QHBoxLayout()
self.redditSubs = QVBoxLayout()
self.downloadBar = QHBoxLayout()
self.profileInfo = QGridLayout()
"""Nested Layout"""
self.mainLayout.addLayout(self.profileInfo, 0, 0)
self.mainLayout.addLayout(self.picLayout, 0, 1)
self.mainLayout.addLayout(self.redditSubs, 1, 0)
self.mainLayout.addLayout(self.downloadBar, 1, 1)
"""Widgets"""
self.display = QLabel("QHBoxLayout()")
self.download = QLabel("QHBoxLayout()")
self.subs = QLabel("QVBoxLayout()")
self.fileInfo = QLabel("QGridLayout()")
self.panel = QWidget()
"""AddWidgets"""
self.picLayout.addWidget(self.display)
self.downloadBar.addWidget(self.download)
self.redditSubs.addWidget(self.subs)
self.profileInfo.addWidget(self.panel, 0, 0)
lay = QVBoxLayout(self.panel)
lay.addWidget(self.fileInfo)
"""Stylesheet"""
self.panel.setStyleSheet("background-color: red;")
"""Initiating mainLayout """
self.window = QWidget()
self.window.setLayout(self.mainLayout)
self.setCentralWidget(self.window)
if __name__ == "__main__":
app = QApplication([])
w = Window()
w.showNormal()
app.exec_()
布局结构:
self.mainLayout = QGridLayout()
self.subLayout1 = QGridLayout()
self.subLayout2 = QVBoxLayout()
...
...
...
self.mainLayout.addLayout(subLayout1,0,0)
我试过:
self.mainLayout = QGridLayout()
self.panel = QWidget()
self.subLayout1 = QGridLayout(self.panel)
self.mainLayout.addLayout(subLayout1,0,0)
self.panel.setStylesheet("background-color: red;")
Error: QLayout::addChildLayout: layout "" already has a parent
这就是我设置 mainLayout 颜色的方式
self.setAutoFillBackground(True)
p = self.palette()
p.setColor(self.backgroundRole(), Qt.black)
self.setPalette(p)
显示subLayout1、subLayout2等时需要在背面
我该怎么做?
编辑:添加了 MCVE(最小、完整、可验证的示例)
from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel, QGridLayout
from PyQt5.QtWidgets import QWidget, QVBoxLayout, QHBoxLayout
from PyQt5.QtCore import Qt
class Window(QMainWindow):
def __init__(self, parent=None):
super(Window, self).__init__(parent)
self.setWindowTitle('MCVE')
'''Window Background'''
self.setAutoFillBackground(True)
p = self.palette()
p.setColor(self.backgroundRole(), Qt.darkYellow)
self.setPalette(p)
'''Core Layouts'''
self.mainLayout = QGridLayout()
self.picLayout = QHBoxLayout()
self.redditSubs = QVBoxLayout()
self.downloadBar = QHBoxLayout()
self.panel = QWidget()
self.profileInfo = QGridLayout(self.panel)
'''Nested Layout'''
self.mainLayout.addLayout(self.profileInfo, 0, 0)
self.mainLayout.addLayout(self.picLayout, 0, 1)
self.mainLayout.addLayout(self.redditSubs, 1, 0)
self.mainLayout.addLayout(self.downloadBar, 1, 1)
'''Widgets'''
self.display = QLabel('QHBoxLayout()')
self.download = QLabel('QHBoxLayout()')
self.subs = QLabel('QVBoxLayout()')
self.fileInfo = QLabel('QGridLayout()')
'''AddWidgets'''
self.picLayout.addWidget(self.display)
self.downloadBar.addWidget(self.download)
self.redditSubs.addWidget(self.subs)
self.profileInfo.addWidget(self.fileInfo, 0, 0)
'''Stylesheet'''
self.panel.setStyleSheet("background-color: red;")
'''Initiating mainLayout '''
self.window = QWidget()
self.window.setLayout(self.mainLayout)
self.setCentralWidget(self.window)
if __name__ == '__main__':
app = QApplication([])
w = Window()
w.showNormal()
app.exec_()
如上所说,代码错误QLayout::addChildLayout: layout "" already has a parent, 如果 Stylesheet 部分被注释掉,QGridLayout() 标签会出现在深黄色背景之上,我想要那个背景部分为红色。
QXLayout 不是视觉元素,而是 class 控制位置,尤其是分配的小部件的大小。因此,想要为不是视觉元素的 class 设置颜色是没有意义的。
另一方面,错误是因为以下2个代码是等价的:
x = QXLayout(w)
x = QXLayout()
w.setLayout(x)
在您的情况下,您已使用以下代码指示布局 "profileInfo" 属于 "panel":self.profileInfo = QGridLayout(self.panel)
,但随后您指示同一布局属于 "mainLayout" 通过以下代码:self.mainLayout.addLayout(self.profileInfo, 0, 0)
。因此,为避免警告,您必须通过将最后一个代码替换为 self.mainLayout.addWidget(self.panel, 0, 0)
。
更新:
如果您希望一个小部件位于另一个小部件内,那么它必须是第二个小部件的子项,因此 "fileInfo" 必须是 "panel" 的子项,但是既然您希望它占据父级的整个尺寸,那么你必须使用布局。
from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel, QGridLayout
from PyQt5.QtWidgets import QWidget, QVBoxLayout, QHBoxLayout
from PyQt5.QtCore import Qt
class Window(QMainWindow):
def __init__(self, parent=None):
super(Window, self).__init__(parent)
self.setWindowTitle("MCVE")
"""Window Background"""
self.setAutoFillBackground(True)
p = self.palette()
p.setColor(self.backgroundRole(), Qt.darkYellow)
self.setPalette(p)
"""Core Layouts"""
self.mainLayout = QGridLayout()
self.picLayout = QHBoxLayout()
self.redditSubs = QVBoxLayout()
self.downloadBar = QHBoxLayout()
self.profileInfo = QGridLayout()
"""Nested Layout"""
self.mainLayout.addLayout(self.profileInfo, 0, 0)
self.mainLayout.addLayout(self.picLayout, 0, 1)
self.mainLayout.addLayout(self.redditSubs, 1, 0)
self.mainLayout.addLayout(self.downloadBar, 1, 1)
"""Widgets"""
self.display = QLabel("QHBoxLayout()")
self.download = QLabel("QHBoxLayout()")
self.subs = QLabel("QVBoxLayout()")
self.fileInfo = QLabel("QGridLayout()")
self.panel = QWidget()
"""AddWidgets"""
self.picLayout.addWidget(self.display)
self.downloadBar.addWidget(self.download)
self.redditSubs.addWidget(self.subs)
self.profileInfo.addWidget(self.panel, 0, 0)
lay = QVBoxLayout(self.panel)
lay.addWidget(self.fileInfo)
"""Stylesheet"""
self.panel.setStyleSheet("background-color: red;")
"""Initiating mainLayout """
self.window = QWidget()
self.window.setLayout(self.mainLayout)
self.setCentralWidget(self.window)
if __name__ == "__main__":
app = QApplication([])
w = Window()
w.showNormal()
app.exec_()