【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)
相关推荐
一只大鸽子6 天前
Python中的并发编程(5)PyQt 多线程
数据库·python·pyqt
CyberSecurity_zhang7 天前
PyQt问题汇总(持续更新)
pyqt
深蓝海拓7 天前
PySide(PyQt)在图像上画线
pyqt
Jay_279 天前
【金】04Y? 人脸识别系统 | 前端PyQT
pyqt
LeetCode_C++13 天前
基于Python+OpenCV的车牌识别停车场管理系统(PyQt界面)【含Python源码 MX_009期】
python·opencv·pyqt
深蓝海拓13 天前
Pyside(PYQT)应用程序基本框架
python·pyqt
深蓝海拓15 天前
PySide(PyQt)利用回调和闭包定义信号与槽
python·qt·pyqt
清安无别事16 天前
SAdb项目第三章-Pyside6资源文件与UI文件的使用
python·qt·pyqt
linghyu16 天前
数据集MNIST手写体识别 pyqt5+Pytorch/TensorFlow
人工智能·pytorch·pyqt