简体   繁体   中英

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. 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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