How to select a QListWidgetItem when click on a widget [child] inside it

a 夏天 提交于 2019-12-01 11:22:53

You have to order your code, and the best way is to use classes. In this case the widget (QLineEdit + QPushButton) must be a class and expose the clicked signal of the button through a signal belonging to the class.

In the same way another class is created that handles the QListWidget, to obtain the row of the button the task is to use the geometry, in this case we will obtain the widget using sender() (sender() returns the object that issued the signal), and then the position of the top-left of that widget is obtained with respect to the screen using mapToGlobal(), that global position is converted to a local position with respect to the viewport() of the QListWidget using mapFromGlobal(), then using the local position we obtain the item using itemAt(), and with the item the task is simple.

PyQt4:

import sys
from PyQt4 import QtGui, QtCore

class Widget(QtGui.QWidget):
    clicked = QtCore.pyqtSignal()

    def __init__(self, parent=None):
        super(Widget, self).__init__(parent)
        line_edit = QtGui.QLineEdit()
        delete_button = QtGui.QPushButton("Delete Row")
        hlay = QtGui.QHBoxLayout(self)
        hlay.addWidget(line_edit)
        hlay.addWidget(delete_button)
        delete_button.clicked.connect(self.clicked)

class MainWindow(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.setWindowTitle("PoC ListWidget")

        tab_widget = QtGui.QTabWidget()
        work_tab = QtGui.QWidget()
        tab_widget.addTab(work_tab, 'Tab.01')

        self.others_commands_widget = QtGui.QListWidget()
        add_button = QtGui.QPushButton("Add")
        add_button.clicked.connect(self.add_other_command)

        vlay = QtGui.QVBoxLayout(work_tab)
        vlay.addWidget(add_button, alignment=QtCore.Qt.AlignLeft)
        vlay.addWidget(self.others_commands_widget)

        self.setCentralWidget(tab_widget)

    @QtCore.pyqtSlot()
    def add_other_command(self):
        it = QtGui.QListWidgetItem()
        self.others_commands_widget.addItem(it)
        widget = Widget()
        widget.clicked.connect(self.remove_other_command)
        self.others_commands_widget.setItemWidget(it, widget)
        it.setSizeHint(widget.sizeHint())

    @QtCore.pyqtSlot()
    def remove_other_command(self):
        widget = self.sender()
        gp = widget.mapToGlobal(QtCore.QPoint())
        lp = self.others_commands_widget.viewport().mapFromGlobal(gp)
        row = self.others_commands_widget.row(self.others_commands_widget.itemAt(lp))
        t_it = self.others_commands_widget.takeItem(row)
        del t_it


if __name__ == '__main__':
    import sys
    app = QtGui.QApplication(sys.argv)
    w = MainWindow()
    w.show()
    sys.exit(app.exec_())

PyQt5:

import sys
from PyQt5 import QtCore, QtGui, QtWidgets

class Widget(QtWidgets.QWidget):
    clicked = QtCore.pyqtSignal()

    def __init__(self, parent=None):
        super(Widget, self).__init__(parent)
        line_edit = QtWidgets.QLineEdit()
        delete_button = QtWidgets.QPushButton("Delete Row")
        hlay = QtWidgets.QHBoxLayout(self)
        hlay.addWidget(line_edit)
        hlay.addWidget(delete_button)
        delete_button.clicked.connect(self.clicked)

class MainWindow(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.setWindowTitle("PoC ListWidget")

        tab_widget = QtWidgets.QTabWidget()
        work_tab = QtWidgets.QWidget()
        tab_widget.addTab(work_tab, 'Tab.01')

        self.others_commands_widget = QtWidgets.QListWidget()
        add_button = QtWidgets.QPushButton("Add")
        add_button.clicked.connect(self.add_other_command)

        vlay = QtWidgets.QVBoxLayout(work_tab)
        vlay.addWidget(add_button, alignment=QtCore.Qt.AlignLeft)
        vlay.addWidget(self.others_commands_widget)

        self.setCentralWidget(tab_widget)

    @QtCore.pyqtSlot()
    def add_other_command(self):
        it = QtWidgets.QListWidgetItem()
        self.others_commands_widget.addItem(it)
        widget = Widget()
        widget.clicked.connect(self.remove_other_command)
        self.others_commands_widget.setItemWidget(it, widget)
        it.setSizeHint(widget.sizeHint())

    @QtCore.pyqtSlot()
    def remove_other_command(self):
        widget = self.sender()
        gp = widget.mapToGlobal(QtCore.QPoint())
        lp = self.others_commands_widget.viewport().mapFromGlobal(gp)
        row = self.others_commands_widget.row(self.others_commands_widget.itemAt(lp))
        t_it = self.others_commands_widget.takeItem(row)
        del t_it


if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    w = MainWindow()
    w.show()
    sys.exit(app.exec_())
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!