【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)
相关推荐
楚莫识3 天前
Qt show()、raise()、activateWindow() 调用顺序区别
qt·pyqt
小灰灰搞电子7 天前
PyQt 实现蜂巢菜单源码分享
pyqt·鱼眼凸起
znnnk7 天前
【Python】GUI 开发从入门到实战(三):PyQt/PySide 进阶之路
开发语言·python·pyqt
民乐团扒谱机12 天前
【超详细】PyQt6 实现 AU 风格波形图编辑器:多级缩放与采样点逐级渲染,附全过程代码
开发语言·python·编辑器·pyqt·audition·时域·频谱图
典典分享指南13 天前
Excel SUMIFS 跨表汇总实战:多表条件求和的完整指南
django·flask·numpy·pyqt·fastapi
狮子雨恋16 天前
【无标题】
pyqt
微小冷18 天前
pyqt+matplotlib多进程图像刷新
pyqt·matplotlib·进程通信·多进程·queue·process
千里码aicood18 天前
PyQt基于卷积神经网络的智慧校园的设计与实现
人工智能·cnn·pyqt
千里码aicood19 天前
基于PyQt心理咨询机器人智能系统的设计与实现
机器人·pyqt
典典分享指南19 天前
Windows 自动化踩坑实录
python·intellij-idea·pyqt·emacs·visual studio