zhiyiYo / PyQt-Fluent-Widgets

A fluent design widgets library based on C++ Qt/PyQt/PySide. Make Qt Great Again.
https://qfluentwidgets.com
GNU General Public License v3.0
5.8k stars 562 forks source link

[Bug]: ListWidget 在添加自定义widget item 后样式和交互出问题了 #989

Open ChaoII opened 1 month ago

ChaoII commented 1 month ago

What happened?

class LabelListItemWidget(QWidget):
    def __init__(self, label: str, color: QColor, parent=None):
        super().__init__(parent=parent)
        self.hly = QHBoxLayout(self)
        self.hly.setContentsMargins(0, 0, 0, 0)
        self.cb_label_status = ColorPickerButton(color, "")
        self.cb_label_status.setFixedSize(16, 16)
        self.hly.addWidget(self.cb_label_status)
        self.hly.addWidget(BodyLabel(text=label))
        self.label = label

    def get_label(self):
        return self.label

class AnnotationEnsureMessageBox(MessageBoxBase):
    """ Custom message box """

    def __init__(self, labels_color: dict, parent=None):
        super().__init__(parent)
        self.title_label = StrongBodyLabel(self.tr("Select the label"), self)
        self.le_label = LineEdit()
        self.list_widget = ListWidget()
        # 将组件添加到布局中
        self.viewLayout.addWidget(self.title_label)
        self.viewLayout.addWidget(self.le_label)
        self.viewLayout.addWidget(self.list_widget)
        # self.widget.setFixedSize(300, 200)
        self.set_labels(labels_color)
        self.list_widget.itemClicked.connect(self.on_label_item_clicked)

    def set_labels(self, labels_color: dict):
        self.list_widget.clear()
        for label, color in labels_color.items():
            item = QListWidgetItem()
            item.setSizeHint(QSize(item.sizeHint().width(), 100))
            self.list_widget.addItem(item)
            label_item = LabelListItemWidget(label, color)
            self.list_widget.setItemWidget(item, label_item)

        labels = list(labels_color.keys())
        if len(labels) > 0:
            self.list_widget.setCurrentIndex(self.list_widget.model().index(0, 0))
            self.le_label.setText(labels[0])

    def on_label_item_clicked(self, item: QListWidgetItem):
        widget = self.list_widget.itemWidget(item)
        if isinstance(widget, LabelListItemWidget):
            self.le_label.setText(widget.get_label())

    def get_label(self):
        return self.le_label.text()

在使用核心代码:

        item = QListWidgetItem()
        item.setSizeHint(QSize(item.sizeHint().width(), 100))
        self.list_widget.addItem(item)
        label_item = LabelListItemWidget(label, color)
        self.list_widget.setItemWidget(item, label_item)

在添加item时,最后一个item的样式对不齐,并且在鼠标hover时好像被自定义的widget拦截了,hover的高亮出现异常,每一个item的行间距是否可以修改? 2

Operation System

WIndows11 23H2

Python Version

python 3.10 64bit

PyQt/PySide Version

Pyside6.7.2

PyQt/PySide-Fluent-Widgets Version

v1.6.0

How to Reproduce?

只要添加自定义widget item就会出现

Minimum code

zhiyiYo commented 1 month ago

这个组件只改了样式,对齐的问题和你的代码有关系

ChaoII commented 1 month ago

这个组件只改了样式,对齐的问题和你的代码有关系 只有列表的最后一项没对齐,前面几项时对齐的,你看那个gif中labels里面

ChaoII commented 1 month ago

这个组件只改了样式,对齐的问题和你的代码有关系

我试了一下,只改写stylesheet,对齐问题就不存在了。应该是一些Item委托导致的问题,您可以随便找个例子尝试一下,只需要self.list_widget.setItemWidget(item, label_item)就会存在这个问题

x-lancer commented 3 weeks ago

item = QListWidgetItem() item.setSizeHint(QSize(item.sizeHint().width(), 100))

setSizeHint这里错误了,setSizeHint里面应该是实际的widget的size。 应该是 item.setSizeHint(widget.sizeHint()) widget为你实际的控件,否则的话item的大小是有问题的