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)
- [Bug]: Menu 显示不全,上半部分无法显示 HOT 3
- [Request] for LineEdit to show the text in the placeholder at the top HOT 4
- [Bug]: LineTableWidget表头排序箭头的颜色和风格没有统一,表现为黑色
- 添加卡片和 IconWidget 的文档
- FluentIconBase 支持自定义亮暗主题下的颜色
- 调整流畅窗口默认背景颜色 HOT 5
- [Bug]: InfoBar causing stack overflow when created (Only PyQt6) HOT 4
- [Bug]: Themes change incorrectly HOT 1
- 建议增加支持python3.11及以上版本 HOT 6
- Qcompleter的匹配机制问题[Bug]: HOT 3
- [Bug]: 向导窗口点击下一页报错
- QCompleter的匹配与补全建议面板展示问题[Bug]: HOT 3
- [Bug]: Qt Designer中如何将组件提升为SimpleCardWidget HOT 2
- 优化滚动区域在 mac 触控板的平滑滚动
- 如何取消 每次执行前出现的 📢 Tips: QFluentWidgets Pro is now released. Click https://qfluentwidgets.com/pages/pro to learn more about it. HOT 1
- [Bug]: 使用QWebEngineView后,FluentWindow的主题颜色变成纯黑色的 HOT 2
- [Bug]: ExpandLayout 中错误的 y 值计算? HOT 1
- [Bug]: example tab_view 删除标签页异常 HOT 1
- [Bug]: Nuitka打包编译后的示例navigation1中的demo程序运行后提示“RuntimeError: wrapped C/C++ object of type NavigationTreeWidget has been deleted”
- [Bug]: setThemeColor函数对color参数处理错误 HOT 2
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.