【pyqt】listwidget中的删除按钮

python 复制代码
    def create_custom_widget(self, text):
        widget = QWidget()
        widget.setStyleSheet("QWidget{border-bottom: 3px solid white;"
                             "border-top: 2px solid white;}")
        layout = QVBoxLayout()
        label = QLabel(text)
        label.setStyleSheet("background-color:transparent;"
                            "border-bottom:0px;border-top: 0px;")
        current_time = QDateTime.currentDateTime().toString(Qt.DefaultLocaleShortDate)

        label2 = QLabel(f"{current_time}")
        label2.setStyleSheet("background-color:transparent;"
                             "border-bottom:0px;border-top: 0px;")
        deleteBtn = QPushButton("删除")
        deleteBtn.setStyleSheet("QPushButton{background-color:transparent;"
                                "border-bottom:0px;border-top: 0px;}"
                                "QPushButton::hover{color:red;}")
                                
        deleteBtn.clicked.connect(self.remove_item)  # 连接信号和槽

        h_layout = QHBoxLayout()
        h_layout.addWidget(label2)
        h_layout.addWidget(deleteBtn)
        h_layout.setStretch(0, 1)
        h_layout.setStretch(1, 0)

        layout.addWidget(label)
        layout.addLayout(h_layout)
        widget.setLayout(layout)
        return widget

    # 定义移除列表项的槽函数
    def remove_item(self):
        button = self.sender()  # 获取发出信号的按钮
        if button:
            row = self.listWidget.indexAt(button.parent().pos()).row()
            # 从列表视图中移除项
            self.listWidget.takeItem(row)

tablewiget是removeRow

python 复制代码
            self.tableWidget.removeRow(row)
相关推荐
深蓝海拓1 小时前
PySide6从0开始学习的笔记(七) 控件(Widget)之文字输入类控件
笔记·python·qt·学习·pyqt
python开发笔记3 小时前
PyQt(12)TreeWidget与TreeView对比
pyqt
Java Fans10 小时前
PyQt多页面切换教程
pyqt
深蓝海拓1 天前
PySide6从0开始学习的笔记(五) 信号与槽
笔记·qt·学习·pyqt
深蓝海拓2 天前
PySide6从0开始学习的笔记(四)QMainWindow
笔记·python·学习·pyqt
深蓝海拓2 天前
PySide6 的 QSettings简单应用学习笔记
python·学习·pyqt
深蓝海拓3 天前
PySide6从0开始学习的笔记(三) 布局管理器与尺寸策略
笔记·python·qt·学习·pyqt
微尘hjx4 天前
【目标检测软件 01】YOLO识别软件功能与操作指南
人工智能·测试工具·yolo·目标检测·计算机视觉·ai·pyqt
深蓝海拓5 天前
自用pyside6项目模板
学习·pyqt
YANshangqian5 天前
跨平台桌面RSS阅读器 MrRSS
pyqt