Giter Site home page Giter Site logo

Comments (2)

AlexZhu2001 avatar AlexZhu2001 commented on June 8, 2024

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.

AmirMahdaviAM avatar AmirMahdaviAM commented on June 8, 2024

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()

image

from pyqt-fluent-widgets.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.