【发布时间】:2021-07-05 09:45:42
【问题描述】:
我有一张我想要的图像作为我的 Qframe 小部件边框的背景。我希望框架内的 treewidget 具有优先于框架图像的背景颜色。到目前为止,我的尝试已经在复选框下找到了由父/子树小部件设置样式的区域。如何使框架边框内的所有背景区域颜色相同?
import PyQt5.QtGui
import PyQt5.QtWidgets
import PyQt5.QtCore
import sys
foods = [['Cookie dough', '5.2'],
['Hummus', '7.9'],
['Spaghetti', '9.92'],
['Dal makhani', '4.2'],
['Chocolate whipped cream', '1.2']]
class TreeWidget(PyQt5.QtWidgets.QTreeWidget):
def __init__(self, parent = None):
super().__init__(parent)
for product, price in foods:
parent = PyQt5.QtWidgets.QTreeWidgetItem([product])
parent.setBackground(0, PyQt5.QtGui.QBrush(PyQt5.QtGui.QColor('#e6dddc')))
parent.setFlags(parent.flags() | PyQt5.QtCore.Qt.ItemIsUserCheckable)
parent.setTextAlignment(PyQt5.QtCore.Qt.AlignLeft, PyQt5.QtCore.Qt.AlignLeft)
check_state = PyQt5.QtCore.Qt.Checked
parent.setCheckState(0, check_state)
child = PyQt5.QtWidgets.QTreeWidgetItem([price])
child.setBackground(0, PyQt5.QtGui.QBrush(PyQt5.QtGui.QColor('#e6dddc')))
parent.addChild(child)
self.addTopLevelItem(parent)
self.setRootIsDecorated(False)
self.setHeaderHidden(True)
self.expandAll()
class Section(PyQt5.QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.setStyleSheet("QFrame {background-image: url('assets/wrap3.jpeg')}")
layout = PyQt5.QtWidgets.QGridLayout()
self.treeWidget = TreeWidget()
self.frame = PyQt5.QtWidgets.QFrame()
self.frame.setFrameShape(PyQt5.QtWidgets.QFrame.StyledPanel)
self.frame.setFrameShadow(PyQt5.QtWidgets.QFrame.Raised)
tree_layout = PyQt5.QtWidgets.QVBoxLayout(self.frame)
tree_layout.addWidget(self.treeWidget)
layout.addWidget(self.frame,0,0)
self.setLayout(layout)
if __name__ == '__main__':
app = PyQt5.QtWidgets.QApplication(sys.argv)
window = Section()
window.show()
sys.exit(app.exec_())
【问题讨论】:
-
您的意思是希望背景只对父 QFrame 可见,而 不 对树视图的背景可见,它会有自己的颜色?
-
你的问题不清楚,能不能把你想要得到的图片放一张
-
@eyllanesc 查看更新的意图。干杯
标签: python pyqt pyqt5 qtstylesheets