我的 .qss 文件无法应用于我的 .ui 文件
my .qss file cannot be applied to my .ui file
我有一个用 Qt Designer 制作的 .ui
文件。很简单,只有一个QWidget和一个QLabel,没有任何Qt StyleSheet代码
我有另一个 .qss
文件只有一行:
background: rgb(39, 44, 54);
我想把它们组合在一起,这是我的代码:
# This is a "demo" of the file path.
# and I'm 100% sure that the original path is working.
UI_FILE = "untitled2.ui"
QSS_FILE = "test.qss"
class MyWidget(QWidget):
def __init__(self, parent=None):
super(MyWidget, self).__init__(parent)
"""
load UI file and QSS file
"""
ui_file = QFile(UI_FILE)
ui_file.open(QFile.ReadOnly)
loader = QUiLoader()
self.ui = loader.load(ui_file, parentWidget=self)
ui_file.close()
style_file = QFile(QSS_FILE)
style_file.open(QFile.ReadOnly)
styleSheet = str(style_file.readAll())
self.setStyleSheet(styleSheet)
style_file.close()
""" this button and text edit are used to
confirm whether qss is loaded.
"""
bt = QPushButton("show style sheet")
bt.clicked.connect(self.show_sheet)
self.te = QTextEdit()
self.te2 = QTextEdit()
main_layout = QVBoxLayout(self)
main_layout.addWidget(self.ui)
main_layout.addWidget(bt)
main_layout.addWidget(self.te)
main_layout.addWidget(self.te2)
def show_sheet(self, clicked):
self_ss = self.styleSheet()
self.te.setText(self_ss)
ui_ss = self.ui.styleSheet()
self.te2.setText(ui_ss)
## run
if __name__ == '__main__':
if not QApplication.instance():
app = QApplication(sys.argv)
else:
app = QApplication.instance()
widget = MyWidget()
widget.show()
sys.exit(app.exec_())
这是我的结果:
Qt Style Sheet 不工作,但是当我单击“显示样式 sheet”时,我可以看到 .qss
已经加载。
我试过把qss码改成这个,没用
QWidget{
background: rgb(39, 44, 54);
}
我试过用this code来自定义paintEvent()
,但也没用。
理论上只要我在顶层设置样式sheet,下面的控件就会继承。但似乎并非如此。
更新:.ui 文件
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Form</class>
<widget class="QWidget" name="Form">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>352</width>
<height>218</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QLabel" name="label">
<property name="minimumSize">
<size>
<width>0</width>
<height>100</height>
</size>
</property>
<property name="font">
<font>
<family>Arial</family>
<pointsize>18</pointsize>
</font>
</property>
<property name="text">
<string>TextLabel</string>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>
问题是因为您错误地将 readAll() returns 的 QByteArray 转换为字符串,您不应该使用 str.
当您使用 str(style_file.readAll())
时,您会得到 "b'background-color: red'"
而不是 "background-color: red"
。
要转换为字符串,有以下选项:
styleSheet = style_file.readAll().data().decode("utf-8")
或
ba = style_file.readAll()
codec = QTextCodec.codecForName("UTF-8")
styleSheet = codec.toUnicode(ba)
输出:
我有一个用 Qt Designer 制作的 .ui
文件。很简单,只有一个QWidget和一个QLabel,没有任何Qt StyleSheet代码
我有另一个 .qss
文件只有一行:
background: rgb(39, 44, 54);
我想把它们组合在一起,这是我的代码:
# This is a "demo" of the file path.
# and I'm 100% sure that the original path is working.
UI_FILE = "untitled2.ui"
QSS_FILE = "test.qss"
class MyWidget(QWidget):
def __init__(self, parent=None):
super(MyWidget, self).__init__(parent)
"""
load UI file and QSS file
"""
ui_file = QFile(UI_FILE)
ui_file.open(QFile.ReadOnly)
loader = QUiLoader()
self.ui = loader.load(ui_file, parentWidget=self)
ui_file.close()
style_file = QFile(QSS_FILE)
style_file.open(QFile.ReadOnly)
styleSheet = str(style_file.readAll())
self.setStyleSheet(styleSheet)
style_file.close()
""" this button and text edit are used to
confirm whether qss is loaded.
"""
bt = QPushButton("show style sheet")
bt.clicked.connect(self.show_sheet)
self.te = QTextEdit()
self.te2 = QTextEdit()
main_layout = QVBoxLayout(self)
main_layout.addWidget(self.ui)
main_layout.addWidget(bt)
main_layout.addWidget(self.te)
main_layout.addWidget(self.te2)
def show_sheet(self, clicked):
self_ss = self.styleSheet()
self.te.setText(self_ss)
ui_ss = self.ui.styleSheet()
self.te2.setText(ui_ss)
## run
if __name__ == '__main__':
if not QApplication.instance():
app = QApplication(sys.argv)
else:
app = QApplication.instance()
widget = MyWidget()
widget.show()
sys.exit(app.exec_())
这是我的结果:
Qt Style Sheet 不工作,但是当我单击“显示样式 sheet”时,我可以看到 .qss
已经加载。
我试过把qss码改成这个,没用
QWidget{
background: rgb(39, 44, 54);
}
我试过用this code来自定义paintEvent()
,但也没用。
理论上只要我在顶层设置样式sheet,下面的控件就会继承。但似乎并非如此。
更新:.ui 文件
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Form</class>
<widget class="QWidget" name="Form">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>352</width>
<height>218</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QLabel" name="label">
<property name="minimumSize">
<size>
<width>0</width>
<height>100</height>
</size>
</property>
<property name="font">
<font>
<family>Arial</family>
<pointsize>18</pointsize>
</font>
</property>
<property name="text">
<string>TextLabel</string>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>
问题是因为您错误地将 readAll() returns 的 QByteArray 转换为字符串,您不应该使用 str.
当您使用 str(style_file.readAll())
时,您会得到 "b'background-color: red'"
而不是 "background-color: red"
。
要转换为字符串,有以下选项:
styleSheet = style_file.readAll().data().decode("utf-8")
或
ba = style_file.readAll()
codec = QTextCodec.codecForName("UTF-8")
styleSheet = codec.toUnicode(ba)
输出: