【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)
相关推荐
酷飞飞2 天前
PyQt 界面布局与交互组件使用指南
python·qt·交互·pyqt
qq_340474023 天前
Q3.1 PyQt 中的控件罗列
pyqt
万粉变现经纪人3 天前
如何解决pip安装报错ModuleNotFoundError: No module named ‘sympy’问题
python·beautifulsoup·pandas·scikit-learn·pyqt·pip·scipy
Goona_4 天前
PyQt数字转大写金额GUI工具开发及财务规范实现
python·小程序·交互·pyqt
小叮当⇔4 天前
PYcharm——pyqt音乐播放器
ide·pycharm·pyqt
青铜发条5 天前
【Qt】PyQt、原生QT、PySide6三者的多方面比较
开发语言·qt·pyqt
Goona_6 天前
pyqt+python之二进制生肖占卜
pyqt
大学生毕业题目8 天前
毕业项目推荐:83-基于yolov8/yolov5/yolo11的农作物杂草检测识别系统(Python+卷积神经网络)
人工智能·python·yolo·目标检测·cnn·pyqt·杂草识别
凯子坚持 c11 天前
当Python遇见高德:基于PyQt与JS API构建桌面三维地形图应用实战
javascript·python·pyqt·高德地图