【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)
相关推荐
小灰灰搞电子10 天前
PyQt QtWaitingSpinner详解-为你的 Qt 程序带来丝滑等待动画
开发语言·qt·pyqt
懷淰メ13 天前
【AI加持】基于PyQt+YOLO+DeepSeek的舌苔情况检测系统(详细介绍)
python·yolo·目标检测·计算机视觉·pyqt·舌苔
深蓝海拓17 天前
PySide6,图形按钮使用系统内置图标
笔记·python·学习·pyqt
懷淰メ18 天前
【AI加持】基于PyQt+YOLO+DeepSeek的钢材焊接缺陷检测系统(详细介绍)
yolo·目标检测·计算机视觉·pyqt·缺陷检测·deepseek·钢材缺陷
爱码小白21 天前
排除LhPyQt5疑难bug的经验
python·pyqt
懷淰メ22 天前
【AI加持】基于PyQt+YOLO+DeepSeek的安全帽检测系统(详细介绍)
yolo·目标检测·计算机视觉·pyqt·安全帽检测·deepseek·安全帽
懷淰メ22 天前
【AI加持】基于PyQt+YOLO+DeepSeek的PCB缺陷检测系统(详细介绍)
yolo·计算机视觉·pyqt·缺陷检测·pcb·检测系统·pcb缺陷
懷淰メ23 天前
【AI加持】基于PyQt+YOLO+DeepSeek的布匹缺陷检测系统(详细介绍)
yolo·目标检测·计算机视觉·pyqt·缺陷检测·布匹·布匹缺陷
深蓝海拓23 天前
基于QtPy (PySide6) 的PLC-HMI工程项目(十二)最后的工作
网络·笔记·python·学习·pyqt·plc
TOOLS指南24 天前
Python-PyQt界面开发入门-计算器例子
pyqt