提交 537109f3 编写于 作者: 之一Yo's avatar 之一Yo

添加直接更换 item 字体和前景色的功能

上级 0f48ed69
...@@ -33,7 +33,7 @@ class Demo(QWidget): ...@@ -33,7 +33,7 @@ class Demo(QWidget):
] ]
for stand in stands: for stand in stands:
item = QListWidgetItem(stand) item = QListWidgetItem(stand)
# item.setIcon(QIcon('docs/source/_static/logo.png')) # item.setIcon(QIcon(':/qfluentwidgets/images/logo.png'))
# item.setCheckState(Qt.Unchecked) # item.setCheckState(Qt.Unchecked)
self.listWidget.addItem(item) self.listWidget.addItem(item)
......
...@@ -12,7 +12,7 @@ Examples are available at https://github.com/zhiyiYo/PyQt-Fluent-Widgets/tree/ma ...@@ -12,7 +12,7 @@ Examples are available at https://github.com/zhiyiYo/PyQt-Fluent-Widgets/tree/ma
:license: GPLv3, see LICENSE for more details. :license: GPLv3, see LICENSE for more details.
""" """
__version__ = "0.9.2" __version__ = "0.9.3"
from .components import * from .components import *
from .common import * from .common import *
......
此差异已折叠。
<RCC> <RCC>
<qresource prefix="/qfluentwidgets"> <qresource prefix="/qfluentwidgets">
<file>images/logo.png</file>
<file>images/color_dialog/Clear_black.svg</file> <file>images/color_dialog/Clear_black.svg</file>
<file>images/color_dialog/Clear_white.svg</file> <file>images/color_dialog/Clear_white.svg</file>
<file>images/color_dialog/HuePanel.png</file> <file>images/color_dialog/HuePanel.png</file>
......
...@@ -5,8 +5,10 @@ from PyQt5.QtCore import Qt, pyqtSignal ...@@ -5,8 +5,10 @@ from PyQt5.QtCore import Qt, pyqtSignal
from PyQt5.QtGui import QPainter, QFont from PyQt5.QtGui import QPainter, QFont
from PyQt5.QtWidgets import QApplication, QPushButton, QWidget, QHBoxLayout, QSizePolicy from PyQt5.QtWidgets import QApplication, QPushButton, QWidget, QHBoxLayout, QSizePolicy
from ...common.router import qrouter
from ...common.style_sheet import themeColor, FluentStyleSheet from ...common.style_sheet import themeColor, FluentStyleSheet
from ..widgets.scroll_area import SingleDirectionScrollArea from ..widgets.scroll_area import SingleDirectionScrollArea
from .navigation_panel import RouteKeyError
class PivotItem(QPushButton): class PivotItem(QPushButton):
...@@ -71,7 +73,7 @@ class Pivot(QWidget): ...@@ -71,7 +73,7 @@ class Pivot(QWidget):
self.setSizePolicy(QSizePolicy.Minimum, QSizePolicy.Minimum) self.setSizePolicy(QSizePolicy.Minimum, QSizePolicy.Minimum)
def addItem(self, routeKey: str, text: str, onClick): def addItem(self, routeKey: str, text: str, onClick=None):
""" add item """ add item
Parameters Parameters
...@@ -87,7 +89,23 @@ class Pivot(QWidget): ...@@ -87,7 +89,23 @@ class Pivot(QWidget):
""" """
return self.insertItem(-1, routeKey, text, onClick) return self.insertItem(-1, routeKey, text, onClick)
def insertItem(self, index: int, routeKey: str, text: str, onClick): def addWidget(self, routeKey: str, widget: PivotItem, onClick=None):
""" add widget
Parameters
----------
routeKey: str
the unique name of item
widget: PivotItem
navigation widget
onClick: callable
the slot connected to item clicked signal
"""
self.insertWidget(-1, routeKey, widget, onClick)
def insertItem(self, index: int, routeKey: str, text: str, onClick=None):
""" insert item """ insert item
Parameters Parameters
...@@ -108,14 +126,62 @@ class Pivot(QWidget): ...@@ -108,14 +126,62 @@ class Pivot(QWidget):
return return
item = PivotItem(text, self) item = PivotItem(text, self)
item.setProperty('routeKey', routeKey) self.insertWidget(index, routeKey, item, onClick)
item.itemClicked.connect(self._onItemClicked)
item.itemClicked.connect(onClick)
self.items[routeKey] = item
self.hBoxLayout.insertWidget(index, item, 0, Qt.AlignLeft)
return item return item
def insertWidget(self, index: int, routeKey: str, widget: PivotItem, onClick=None):
""" insert item
Parameters
----------
index: int
insert position
routeKey: str
the unique name of item
widget: PivotItem
navigation widget
onClick: callable
the slot connected to item clicked signal
"""
if routeKey in self.items:
return
widget.setProperty('routeKey', routeKey)
widget.itemClicked.connect(self._onItemClicked)
if onClick:
widget.itemClicked.connect(onClick)
self.items[routeKey] = widget
self.hBoxLayout.insertWidget(index, widget, 0, Qt.AlignLeft)
def removeWidget(self, routeKey: str):
""" remove widget
Parameters
----------
routeKey: str
the unique name of item
"""
if routeKey not in self.items:
return
item = self.items.pop(routeKey)
self.hBoxLayout.removeWidget(item)
qrouter.remove(routeKey)
item.deleteLater()
def clear(self):
""" clear all navigation items """
for k, w in self.items.items():
self.hBoxLayout.removeWidget(w)
qrouter.remove(k)
w.deleteLater()
self.items.clear()
def setCurrentItem(self, routeKey: str): def setCurrentItem(self, routeKey: str):
""" set current selected item """ set current selected item
...@@ -141,3 +207,9 @@ class Pivot(QWidget): ...@@ -141,3 +207,9 @@ class Pivot(QWidget):
def _onItemClicked(self): def _onItemClicked(self):
item = self.sender() # type: PivotItem item = self.sender() # type: PivotItem
self.setCurrentItem(item.property('routeKey')) self.setCurrentItem(item.property('routeKey'))
def widget(self, routeKey: str):
if routeKey not in self.items:
raise RouteKeyError(f"`{routeKey}` is illegal.")
return self.items[routeKey]
\ No newline at end of file
...@@ -2,9 +2,9 @@ ...@@ -2,9 +2,9 @@
from typing import List, Union from typing import List, Union
from PyQt5.QtCore import Qt, QMargins, QModelIndex, QItemSelectionModel from PyQt5.QtCore import Qt, QMargins, QModelIndex, QItemSelectionModel
from PyQt5.QtGui import QPainter, QColor, QKeyEvent, QPalette from PyQt5.QtGui import QPainter, QColor, QKeyEvent, QPalette, QBrush
from PyQt5.QtWidgets import (QStyledItemDelegate, QApplication, QStyleOptionViewItem, from PyQt5.QtWidgets import (QStyledItemDelegate, QApplication, QStyleOptionViewItem,
QTableView, QTableWidget, QWidget, QTableWidgetItem) QTableView, QTableWidget, QWidget, QTableWidgetItem, QHeaderView)
from ...common.font import getFont from ...common.font import getFont
from ...common.style_sheet import isDarkTheme, FluentStyleSheet, themeColor from ...common.style_sheet import isDarkTheme, FluentStyleSheet, themeColor
...@@ -79,13 +79,18 @@ class TableItemDelegate(QStyledItemDelegate): ...@@ -79,13 +79,18 @@ class TableItemDelegate(QStyledItemDelegate):
def initStyleOption(self, option: QStyleOptionViewItem, index: QModelIndex): def initStyleOption(self, option: QStyleOptionViewItem, index: QModelIndex):
super().initStyleOption(option, index) super().initStyleOption(option, index)
option.font = getFont(13)
if isDarkTheme(): # font
option.palette.setColor(QPalette.Text, Qt.white) option.font = index.data(Qt.FontRole) or getFont(13)
option.palette.setColor(QPalette.HighlightedText, Qt.white)
else: # text color
option.palette.setColor(QPalette.Text, Qt.black) textColor = Qt.white if isDarkTheme() else Qt.black
option.palette.setColor(QPalette.HighlightedText, Qt.black) textBrush = index.data(Qt.TextColorRole) # type: QBrush
if textBrush is not None:
textColor = textBrush.color()
option.palette.setColor(QPalette.Text, textColor)
option.palette.setColor(QPalette.HighlightedText, textColor)
def paint(self, painter, option, index): def paint(self, painter, option, index):
painter.save() painter.save()
......
...@@ -41,13 +41,18 @@ class TreeItemDelegate(QStyledItemDelegate): ...@@ -41,13 +41,18 @@ class TreeItemDelegate(QStyledItemDelegate):
def initStyleOption(self, option, index): def initStyleOption(self, option, index):
super().initStyleOption(option, index) super().initStyleOption(option, index)
option.font = getFont(13)
if isDarkTheme(): # font
option.palette.setColor(QPalette.Text, Qt.white) option.font = index.data(Qt.FontRole) or getFont(13)
option.palette.setColor(QPalette.HighlightedText, Qt.white)
else: # text color
option.palette.setColor(QPalette.Text, Qt.black) textColor = Qt.white if isDarkTheme() else Qt.black
option.palette.setColor(QPalette.HighlightedText, Qt.black) textBrush = index.data(Qt.TextColorRole)
if textBrush is not None:
textColor = textBrush.color()
option.palette.setColor(QPalette.Text, textColor)
option.palette.setColor(QPalette.HighlightedText, textColor)
class TreeViewBase: class TreeViewBase:
......
...@@ -6,7 +6,7 @@ with open('README.md', encoding='utf-8') as f: ...@@ -6,7 +6,7 @@ with open('README.md', encoding='utf-8') as f:
setuptools.setup( setuptools.setup(
name="PyQt-Fluent-Widgets", name="PyQt-Fluent-Widgets",
version="0.9.2", version="0.9.3",
keywords="pyqt fluent widgets", keywords="pyqt fluent widgets",
author="zhiyiYo", author="zhiyiYo",
author_email="shokokawaii@outlook.com", author_email="shokokawaii@outlook.com",
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册