提交 395279cb 编写于 作者: 之一Yo's avatar 之一Yo

添加 Pivot

上级 7046b722
# coding:utf-8
import sys
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication, QWidget, QStackedWidget, QVBoxLayout, QLabel
from qfluentwidgets import Pivot, setTheme, Theme
class Demo(QWidget):
def __init__(self):
super().__init__()
# setTheme(Theme.DARK)
self.setStyleSheet("""
Demo{background: white}
QLabel{
font: 20px 'Segoe UI';
background: rgb(242,242,242);
border-radius: 8px;
}
""")
self.resize(400, 400)
self.pivot = Pivot(self)
self.stackedWidget = QStackedWidget(self)
self.vBoxLayout = QVBoxLayout(self)
self.songInterface = QLabel('Song Interface', self)
self.albumInterface = QLabel('Album Interface', self)
self.artistInterface = QLabel('Artist Interface', self)
# add items to pivot
self.addSubInterface(self.songInterface, 'songInterface', 'Song')
self.addSubInterface(self.albumInterface, 'albumInterface', 'Album')
self.addSubInterface(self.artistInterface, 'artistInterface', 'Artist')
self.vBoxLayout.addWidget(self.pivot, 0, Qt.AlignHCenter)
self.vBoxLayout.addWidget(self.stackedWidget)
self.vBoxLayout.setContentsMargins(30, 0, 30, 30)
self.stackedWidget.currentChanged.connect(self.onCurrentIndexChanged)
self.stackedWidget.setCurrentWidget(self.songInterface)
self.pivot.setCurrentItem(self.songInterface.objectName())
def addSubInterface(self, widget: QLabel, objectName, text):
widget.setObjectName(objectName)
widget.setAlignment(Qt.AlignCenter)
self.stackedWidget.addWidget(widget)
self.pivot.addItem(
routeKey=objectName,
text=text,
onClick=lambda: self.stackedWidget.setCurrentWidget(widget)
)
def onCurrentIndexChanged(self, index):
widget = self.stackedWidget.widget(index)
self.pivot.setCurrentItem(widget.objectName())
if __name__ == '__main__':
# enable dpi scale
QApplication.setHighDpiScaleFactorRoundingPolicy(Qt.HighDpiScaleFactorRoundingPolicy.PassThrough)
QApplication.setAttribute(Qt.AA_EnableHighDpiScaling)
QApplication.setAttribute(Qt.AA_UseHighDpiPixmaps)
app = QApplication(sys.argv)
w = Demo()
w.show()
app.exec_()
\ No newline at end of file
......@@ -2,7 +2,7 @@
from PyQt5.QtCore import Qt
from PyQt5.QtDesigner import QPyDesignerCustomWidgetPlugin
from qfluentwidgets import NavigationInterface, NavigationPanel
from qfluentwidgets import NavigationInterface, NavigationPanel, Pivot
from plugin_base import PluginBase
......@@ -37,3 +37,21 @@ class NavigationPanelPlugin(NavigationPlugin, QPyDesignerCustomWidgetPlugin):
def name(self):
return "NavigationPanel"
class PivotPlugin(NavigationPlugin, QPyDesignerCustomWidgetPlugin):
""" Navigation panel plugin """
def createWidget(self, parent):
p = Pivot(parent)
for i in range(1, 4):
p.addItem(f'Item{i}', f'Item{i}', print)
p.setCurrentItem('Item1')
return p
def icon(self):
return super().icon("Pivot")
def name(self):
return "Pivot"
PivotItem {
padding: 10px 12px;
color: white;
background-color: transparent;
border: none;
outline: none;
}
PivotItem[isSelected=true]:hover {
color: rgba(255, 255, 255, 0.63);
}
PivotItem[isSelected=true]:pressed {
color: rgba(255, 255, 255, 0.53);
}
PivotItem[isSelected=false]:pressed {
color: rgba(255, 255, 255, 0.75);
}
Pivot {
border: none;
background-color: transparent;
}
#view {
background-color: transparent;
}
\ No newline at end of file
PivotItem {
padding: 10px 12px;
color: black;
background-color: transparent;
border: none;
outline: none;
margin: 0;
}
PivotItem[isSelected=true]:hover {
color: rgba(0, 0, 0, 0.63);
}
PivotItem[isSelected=true]:pressed {
color: rgba(0, 0, 0, 0.53);
}
PivotItem[isSelected=false]:pressed {
color: rgba(0, 0, 0, 0.75);
}
Pivot {
border: none;
background-color: transparent;
}
#view {
background-color: transparent;
}
\ No newline at end of file
此差异已折叠。
......@@ -230,6 +230,7 @@
<file>qss/dark/tree_view.qss</file>
<file>qss/dark/table_view.qss</file>
<file>qss/dark/time_picker.qss</file>
<file>qss/dark/pivot.qss</file>
<file>qss/light/color_dialog.qss</file>
<file>qss/light/dialog.qss</file>
......@@ -254,6 +255,7 @@
<file>qss/light/table_view.qss</file>
<file>qss/light/tree_view.qss</file>
<file>qss/light/time_picker.qss</file>
<file>qss/light/pivot.qss</file>
<file>i18n/qfluentwidgets.zh_CN.qm</file>
<file>i18n/qfluentwidgets.zh_HK.qm</file>
......
......@@ -87,6 +87,7 @@ class FluentStyleSheet(StyleSheetBase, Enum):
""" Fluent style sheet """
MENU = "menu"
PIVOT = "pivot"
BUTTON = "button"
DIALOG = "dialog"
SLIDER = "slider"
......
from .navigation_widget import NavigationWidget, NavigationPushButton, NavigationSeparator, NavigationToolButton
from .navigation_panel import NavigationPanel, NavigationItemPosition, NavigationDisplayMode
from .navigation_interface import NavigationInterface
\ No newline at end of file
from .navigation_interface import NavigationInterface
from .pivot import Pivot, PivotItem
\ No newline at end of file
# coding:utf-8
from typing import Dict
from PyQt5.QtCore import Qt, pyqtSignal
from PyQt5.QtGui import QPainter, QFont
from PyQt5.QtWidgets import QApplication, QPushButton, QWidget, QHBoxLayout, QSizePolicy
from ...common.style_sheet import themeColor, FluentStyleSheet
from ..widgets.scroll_area import SingleDirectionScrollArea
class PivotItem(QPushButton):
""" Pivot item """
itemClicked = pyqtSignal(bool)
def __init__(self, text: str, parent=None):
super().__init__(text, parent)
self.isSelected = False
self.setProperty('isSelected', False)
self.clicked.connect(lambda: self.itemClicked.emit(True))
font = QFont()
font.setFamilies(['Segoe UI', 'Microsoft YaHei'])
font.setPixelSize(18)
self.setFont(font)
def setSelected(self, isSelected: bool):
if self.isSelected == isSelected:
return
self.isSelected = isSelected
self.setProperty('isSelected', isSelected)
self.setStyle(QApplication.style())
self.update()
def paintEvent(self, e):
super().paintEvent(e)
if not self.isSelected:
return
painter = QPainter(self)
painter.setRenderHints(QPainter.Antialiasing)
painter.setPen(Qt.NoPen)
painter.setBrush(themeColor())
x = int(self.width() / 2 - 8)
painter.drawRoundedRect(x, self.height() - 3, 16, 3, 1.5, 1.5)
class Pivot(QWidget):
""" Pivot """
def __init__(self, parent=None):
super().__init__(parent)
self.items = {} # type: Dict[str, PivotItem]
self.hBoxLayout = QHBoxLayout(self)
# self.setWidget(self.view)
# self.setWidgetResizable(True)
# self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
# self.setViewportMargins(0, 0, 0, 0)
FluentStyleSheet.PIVOT.apply(self)
self.hBoxLayout.setSpacing(0)
self.hBoxLayout.setAlignment(Qt.AlignLeft)
self.hBoxLayout.setContentsMargins(0, 0, 0, 0)
self.hBoxLayout.setSizeConstraint(QHBoxLayout.SetMinimumSize)
self.setSizePolicy(QSizePolicy.Minimum, QSizePolicy.Minimum)
def addItem(self, routeKey: str, text: str, onClick):
""" add item
Parameters
----------
routeKey: str
the unique name of item
text: str
the text of navigation item
onClick: callable
the slot connected to item clicked signal
"""
return self.insertItem(-1, routeKey, text, onClick)
def insertItem(self, index: int, routeKey: str, text: str, onClick):
""" insert item
Parameters
----------
index: int
insert position
routeKey: str
the unique name of item
text: str
the text of navigation item
onClick: callable
the slot connected to item clicked signal
"""
if routeKey in self.items:
return
item = PivotItem(text, self)
item.setProperty('routeKey', routeKey)
item.itemClicked.connect(self._onItemClicked)
item.itemClicked.connect(onClick)
self.items[routeKey] = item
self.hBoxLayout.insertWidget(index, item, 0, Qt.AlignLeft)
return item
def setCurrentItem(self, routeKey: str):
""" set current selected item
Parameters
----------
routeKey: str
the unique name of item
"""
if routeKey not in self.items:
return
for k, item in self.items.items():
item.setSelected(k == routeKey)
def setItemFontSize(self, size: int):
""" set the pixel font size of items """
for item in self.items.values():
font = item.font()
font.setPixelSize(size)
item.setFont(font)
item.adjustSize()
def _onItemClicked(self):
item = self.sender() # type: PivotItem
self.setCurrentItem(item.property('routeKey'))
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册