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

前端 未结 1 1071
醉话见心
醉话见心 2021-01-15 03:16

I created a QListWidget... The QListWidgetItem composes of a QPushButton and QLineEdit aligned inside QHBoxLayout...

The QPushButton within the QListWidgetItem is li

1条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-15 03:37

    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_())
    

    0 讨论(0)
提交回复
热议问题