Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
ChaoII opened this issue Oct 16, 2024 · 4 comments
Labels
need more info Further information is requested

Comments

@ChaoII
Copy link

ChaoII commented Oct 16, 2024

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

@ChaoII ChaoII added the bug Something isn't working label Oct 16, 2024
@zhiyiYo
Copy link
Owner

zhiyiYo commented Oct 16, 2024

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

@ChaoII
Copy link
Author

ChaoII commented Oct 17, 2024

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

@zhiyiYo zhiyiYo added need more info Further information is requested and removed bug Something isn't working labels Oct 19, 2024
@ChaoII
Copy link
Author

ChaoII commented Oct 21, 2024

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

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

@x-lancer
Copy link

x-lancer commented Nov 9, 2024

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

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
need more info Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants