PyQt-Fluent-Widgets
PyQt-Fluent-Widgets copied to clipboard
添加自定义侧边导航栏字体颜色的支持(#826)
支持自定义侧边导航栏的控件的文字颜色
通过设置lightColor
和darkColor
两个属性即可设置字体颜色,支持使用QSS样式设置。
示例代码:
from PyQt5.QtGui import QColor, QImage
from PyQt5.QtCore import Qt, QSize
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QVBoxLayout
from qfluentwidgets import FluentWindow, qconfig, Theme, isDarkTheme, QConfig, setTheme, NavigationAvatarWidget, NavigationItemPosition, SubtitleLabel
from qfluentwidgets import FluentIcon as FIF
import base64
img = QImage(QSize(32, 32), QImage.Format.Format_RGB888)
img.fill(Qt.GlobalColor.white)
class Widget(QWidget):
def __init__(self, name: str, parent: QWidget | None = None):
super().__init__(parent=parent)
self.setObjectName(name)
v = QVBoxLayout(self)
self.l = SubtitleLabel(name, self)
v.addWidget(self.l, 0, Qt.AlignmentFlag.AlignCenter)
COLORS = [
(255, 0, 0),
(255, 165, 0),
(255, 255, 0),
(0, 255, 0),
(0, 127, 255),
(0, 0, 255),
(139, 0, 255)
]
class Demo(FluentWindow):
def __init__(self):
super().__init__()
self.resize(800, 600)
self.widgets = []
for i, c in enumerate(COLORS):
w = Widget(f'Widget_{i}', self)
self.widgets.append(w)
self.addSubInterface(w, FIF.HOME, f'Widget {i}')
# You can use property `lightColor` and `darkColor` to modify the font color of navigation widget
nav_widget = self.navigationInterface.widget(w.objectName())
nav_widget.lightColor = QColor(*c)
nav_widget.darkColor = QColor(*map(lambda x: 255-x, c))
self.navigationInterface.addSeparator()
# Also you can use style sheet to modify the font color
# Use `qproperty-lightColor` and `qproperty-darkColor` to set custom font color
self.avatar = NavigationAvatarWidget(
"test",
img,
self
)
self.avatar.setStyleSheet(
r"""
NavigationAvatarWidget {
qproperty-lightColor: #66ccff;
qproperty-darkColor: #66ccff;
}
"""
)
self.navigationInterface.addWidget(
"avatar",
self.avatar,
position=NavigationItemPosition.BOTTOM
)
self.navigationInterface.addSeparator()
# Add button to switch theme
self.navigationInterface.addItem(
"switch_bright_dark",
FIF.FLAG,
"Switch Bright/Dark",
onClick=self.switch,
selectable=False
)
def switch(self):
setTheme(Theme.LIGHT if isDarkTheme() else Theme.DARK)
if __name__ == "__main__":
app = QApplication([])
w = Demo()
w.show()
app.exec()