QT 所有窗口使用同一个样式表 怎么选择某个窗口的组件
要选择某个窗口的组件,可以使用QWidget对象的setStyleSheet()方法为该组件设置样式表。
首先,要确保所有窗口都使用同一个样式表,可以在应用程序的主窗口中设置样式表,然后使用QWidget对象的setStyleSheet()方法为每个窗口的组件设置样式表。
以下是一个示例,演示如何选择某个窗口的组件:
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QLabel, QVBoxLayout
from PyQt5.QtCore import Qt
app = QApplication([])
# 创建一个样式表字符串
style_sheet = "QLabel {color: red;}"
# 创建主窗口
main_window = QMainWindow()
# 创建一个QWidget对象作为主窗口的中心部件
central_widget = QWidget(main_window)
main_window.setCentralWidget(central_widget)
# 创建一个垂直布局
layout = QVBoxLayout(central_widget)
# 创建一个标签并将其添加到布局中
label1 = QLabel("Label 1")
layout.addWidget(label1)
# 为标签1设置样式表
label1.setStyleSheet(style_sheet)
# 创建另一个标签并将其添加到布局中
label2 = QLabel("Label 2")
layout.addWidget(label2)
# 显示主窗口
main_window.show()
app.exec_()
在上述示例中,我们创建了两个标签,其中一个标签(label1)使用了我们定义的样式表。使用setStyleSheet()方法,我们可以为任何QWidget对象设置样式表,而不仅限于标签
原文地址: https://www.cveoy.top/t/topic/iDQ5 著作权归作者所有。请勿转载和采集!