简体   繁体   English

PyQt5:将按钮添加到 QListWidget 的每个项目

[英]PyQt5: Add button to each item of a QListWidget

In my app I'm displaying a list using the QListwidget (I can't use other widget for this task), but I was wondering if it is possible to add to each item of the list a button since I'd like to create a delete item button.在我的应用程序中,我正在使用 QListwidget 显示一个列表(我不能为此任务使用其他小部件),但我想知道是否可以向列表的每个项目添加一个按钮,因为我想创建删除项目按钮。 Basically I'd need something like this:基本上我需要这样的东西:

在此处输入图像描述

So I'd need to know how to create this button for each item in the list and a way to know which button is pressed.所以我需要知道如何为列表中的每个项目创建这个按钮,以及知道按下哪个按钮的方法。

This is my code right now:这是我现在的代码:

import sys
from PyQt5.QtWidgets import (
    QApplication,
    QHBoxLayout,
    QVBoxLayout,
    QGroupBox,
    QListWidget,
    QWidget,
)

class Window(QWidget):
    def __init__(self):
        super().__init__()

        self.setWindowTitle("List Item with button")

        self.centralwidgetHorizontalLayout = QHBoxLayout(self)

        self.Frame = QGroupBox()  
        self.FrameHorizontalLayout = QHBoxLayout(self.Frame)

        self.ListWidget = QListWidget(self.Frame)
        self.ListWidget.setSpacing(11)
        self.ListWidget.setStyleSheet( 
            "QListWidget { background: palette(window); border: none;}"
            "QListWidget::item {"
                "border-style: solid;" 
                "border-width:1px;" 
                "border-color:  black;"
                "margin-right: 30px;"
            "}"
            "QListWidget::item:hover {"
                "border-color: green;"
            "}")

        self.FrameHorizontalLayout.addWidget(self.ListWidget)
        self.centralwidgetHorizontalLayout.addWidget(self.Frame)


        for i in range(13):
            self.ListWidget.addItem(f"Line {i+1}")


if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = Window()
    window.show()
    window.showMaximized()
    sys.exit(app.exec_())

Is there a way to accomplish this?有没有办法做到这一点?

Just change the for code and the rest remains the same.只需更改 for 代码,rest 保持不变。

This is an example, should be what you want.这是一个例子,应该是你想要的。

Typesetting solves it yourself.排版自己解决。

class Window(QWidget):
    def __init__(self):
        ...
        for i in range(13):
            item = QListWidgetItem()
            item_widget = QWidget()
            line_text = QLabel("I love PyQt!")
            line_push_button = QPushButton("Push Me")
            line_push_button.setObjectName(str(i))
            line_push_button.clicked.connect(self.clicked)
            item_layout = QHBoxLayout()
            item_layout.addWidget(line_text)
            item_layout.addWidget(line_push_button)
            item_widget.setLayout(item_layout)
            item.setSizeHint(item_widget.sizeHint())
            self.ListWidget.addItem(item)
            self.ListWidget.setItemWidget(item, item_widget)
        ...

    def clicked(self):
        sender = self.sender()
        push_button = self.findChild(QPushButton, sender.objectName())
        print(f'click: {push_button.objectName()}')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM