Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

添加自定义侧边导航栏字体颜色的支持(#826) #830

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

AlexZhu2001
Copy link
Contributor

支持自定义侧边导航栏的控件的文字颜色
通过设置lightColordarkColor两个属性即可设置字体颜色,支持使用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()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant