【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)
相关推荐
zx_741484813 天前
PyQt5 环境搭建保姆级教程:PyCharm + QtDesigner + PyUIC 完整配置
开发语言·qt·pycharm·pyqt
zhanghaha131418 天前
Python进阶教程:35_PyQt (保姆级零基础教程)
开发语言·python·pyqt
线上放牧人1 个月前
Windows删除图标缓存
windows·python·pyqt
典典分享指南1 个月前
用飞书 Wiki 搭内容作战室:从零搭建团队内容协作中枢
django·flask·numpy·pyqt
千里码aicood1 个月前
pyqt-基于Qt的公共卫生间管理系统的设计与实现
pyqt
千里码aicood1 个月前
pyqt基于pytorch的人脸交换系统设计(opencv)
人工智能·pyqt
楚莫识1 个月前
Qt show()、raise()、activateWindow() 调用顺序区别
qt·pyqt
小灰灰搞电子1 个月前
PyQt 实现蜂巢菜单源码分享
pyqt·鱼眼凸起
znnnk1 个月前
【Python】GUI 开发从入门到实战(三):PyQt/PySide 进阶之路
开发语言·python·pyqt
民乐团扒谱机1 个月前
【超详细】PyQt6 实现 AU 风格波形图编辑器:多级缩放与采样点逐级渲染,附全过程代码
开发语言·python·编辑器·pyqt·audition·时域·频谱图