Comments (2)
I don't think it's easy to do this in the current version. If you want to set background for widget item, you should inherit ListItemDelegate, rewrite _drawBackground or paint fuction. Here is an simple example:
# coding: utf-8
import sys
from PySide6.QtCore import *
from PySide6.QtGui import *
from PySide6.QtWidgets import *
from qfluentwidgets import *
class MyListItemDelegate(ListItemDelegate):
def __init__(self, parent: QListView):
super().__init__(parent)
def _drawBackground(self, painter: QPainter, option: QStyleOptionViewItem, index: QModelIndex):
painter.save()
isHover = self.hoverRow == index.row()
isPressed = self.pressedRow == index.row()
if index.row() not in self.selectedRows:
if isPressed:
painter.setBrush(QColor(0x66, 0xcc, 0xff, 0xff))
elif isHover:
painter.setBrush(QColor(0x66, 0xcc, 0xff, 0x88))
else:
painter.setBrush(QColor(0x66, 0xcc, 0xff, 0x44))
else:
painter.setBrush(QColor(0x66, 0xcc, 0xff, 0xff))
painter.drawRoundedRect(option.rect, 5, 5)
painter.restore()
class Demo(QWidget):
def __init__(self):
super().__init__()
# setTheme(Theme.DARK)
self.hBoxLayout = QHBoxLayout(self)
self.listWidget = ListWidget(self)
self.listWidget.setItemDelegate(MyListItemDelegate(self.listWidget))
# self.listWidget.setAlternatingRowColors(True)
# self.listWidget.setSelectRightClickedRow(True)
stands = [
'白金之星', '绿色法皇', "天堂制造", "绯红之王",
'银色战车', '疯狂钻石', "壮烈成仁", "败者食尘",
"黑蚊子多", '杀手皇后', "金属制品", "石之自由",
"砸瓦鲁多", '钢链手指', "臭氧宝宝", "华丽挚爱",
"隐者之紫", "黄金体验", "虚无之王", "纸月之王",
"骇人恶兽", "男子领域", "20世纪男孩", "牙 Act 4",
"铁球破坏者", "性感手枪", 'D4C • 爱之列车', "天生完美",
"软又湿", "佩斯利公园", "奇迹于你", "行走的心",
"护霜旅行者", "十一月雨", "调情圣手", "片刻静候"
]
for stand in stands:
item = QListWidgetItem(stand)
self.listWidget.addItem(item)
self.setStyleSheet("Demo{background: rgb(249, 249, 249)} ")
self.hBoxLayout.setContentsMargins(0, 0, 0, 0)
self.hBoxLayout.addWidget(self.listWidget)
self.resize(300, 400)
if __name__ == "__main__":
app = QApplication(sys.argv)
w = Demo()
w.show()
app.exec()
But this example did not consider the dark theme, you can refer to the source code of TableItemDelegate to get more detail about how this package draw item background.
from pyqt-fluent-widgets.
thank you, worked like charm
class MyListItemDelegate(ListItemDelegate):
def __init__(self, parent: QListView):
super().__init__(parent)
def _drawBackground(self, painter: QPainter, option: QStyleOptionViewItem, index: QModelIndex):
painter.save()
isHover = self.hoverRow == index.row()
isPressed = self.pressedRow == index.row()
if isDarkTheme():
pressBrush = QColor(255, 255, 255, 10)
hoverBrush = QColor(255, 255, 255, 15)
otherBrush = QColor(255, 255, 255, 5)
else:
pressBrush = QColor(0, 0, 0, 15)
hoverBrush = QColor(0, 0, 0, 30)
otherBrush = QColor(0, 0, 0, 10)
if index.row() not in self.selectedRows:
if isPressed:
painter.setBrush(pressBrush)
elif isHover:
painter.setBrush(hoverBrush)
else:
painter.setBrush(otherBrush)
painter.drawRoundedRect(option.rect, 5, 5)
painter.restore()
from pyqt-fluent-widgets.
Related Issues (20)
- [pro]: 建议给calendar_picker之类的组件增加恢复默认的接口
- [Bug]: AMD独立显卡Mica云母特效显示异常
- [Bug]: 在FlowLayout添加很多相同控件后拖动窗体,窗体尺寸突变 HOT 4
- [Bug]: SearchLineEdit 设置QCompleter 后,Completer 的 void activated(const QModelIndex &index); 没触发
- Windows系统的主题色发生改变时,整个界面会自动变白[Bug]: HOT 6
- 希望作者大佬在QConfig中支持普通字符串
- 新组件提议:InputDialog(已实现完整功能) HOT 1
- [Bug]: 在FlowLayout中启用QTimer刷新时,最后一个元素的形状会异常 HOT 5
- [Bug]: 使用pyqtgraph绘制动态曲线后切换interface报错 HOT 1
- 多屏下启用亚克力效果的导航菜单背景异常
- 对话框偶尔报错 QPainter::begin HOT 3
- [Bug]: InfoBadge covers navigation menu, but it can't be clicked.
- [Bug]: FramelessWebEngineView加载url很慢 HOT 2
- [Bug]: TableWidget掉帧问题
- FlowLayout中的控件在ExpandSettingCard中无法全部显示问题
- [Bug]: Pro组件中的GuideWindow在Mac下显示了Mac自带的最小化最大化和关闭按钮,且下一步和启动按钮的文案未翻译
- table显示较长文本时,希望能偶有方法看清完整文本。 HOT 1
- [Bug]: 继承'SplitFluentWindow'加载html会导致侧边栏显示异常,图标消失
- [Bug]: TableWidget安装了事件过滤器后无法监听鼠标按下的事件 HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pyqt-fluent-widgets.