Search code examples
python-3.xdata-bindingpyqtqlistwidget

the slot don't work for QListWidget itemClicked (pyqt)


I create a QListWidget, and I want to connect the itemClicked with my slot. And the following code is OK: when I click the item, the listwidgetclicked has been called

from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
import sys


class MainWindow(QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)

        self.imagelabel = QLabel()
        layout = QVBoxLayout()
        self.imagelabel.setLayout(layout)
        self.setCentralWidget(self.imagelabel)

        label1 = QLabel('hello, world')
        layout.addWidget(label1)

        newlistWidget = QListWidget()
        newlistWidget.addItem(QListWidgetItem('item3'))
        newlistWidget.addItem(QListWidgetItem('item4'))
        layout.addWidget(newlistWidget)
        newlistWidget.itemClicked.connect(self.listwidgetclicked)

    def listwidgetclicked(self):
        print('click')

app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec_()

However, I want to put all the code of GUI together. So I create a class, and connect the signal and slot in this class. When I click the item, the listwidgetclicked do not work, and the code is:

from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
import sys

class GUI(QObject):
    def __init__(self):
        super(GUI, self).__init__()
        self.layout = QVBoxLayout()
        self.initGUI()

    def getLayout(self):
        return self.layout

    def initGUI(self):
        self.listWidget = QListWidget()
        self.listWidget.addItem(QListWidgetItem('item1'))
        self.listWidget.addItem(QListWidgetItem('item2'))
        self.listWidget.itemClicked.connect(self.listwidgetclicked)
        self.layout.addWidget(self.listWidget)

    def listwidgetclicked(self, item):
        print('click')

class MainWindow(QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)

        self.imagelabel = QLabel()
        layout = QVBoxLayout()
        self.imagelabel.setLayout(layout)
        self.setCentralWidget(self.imagelabel)

        label1 = QLabel('hello, world')
        layout.addWidget(label1)
        listWidget = GUI()
        layout.addLayout(listWidget.getLayout())

app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec_()

I don't know what's the difference between the two codes.


Solution

  • Try it:

    import sys
    from PyQt5.QtWidgets import *
    from PyQt5.QtCore import *
    
    class GUI(QListWidget):
        def __init__(self):
            super(GUI, self).__init__()
            self.layout = QVBoxLayout()
            self.initGUI()
    
    #    def getLayout(self):
    #        return self.layout
    
        def initGUI(self):
    #        self.listWidget = QListWidget()
            self.addItem(QListWidgetItem('item1'))
            self.addItem(QListWidgetItem('item2'))
            self.itemClicked.connect(self.listwidgetclicked)
            self.layout.addWidget(self)       #.listWidget)
    
        def listwidgetclicked(self, item):
            print('!!! click {}'.format(item.text()))
    
    class MainWindow(QMainWindow):
        def __init__(self, parent=None):
            super(MainWindow, self).__init__(parent)
    
            self.imagelabel = QLabel()
            layout = QVBoxLayout()
            self.imagelabel.setLayout(layout)
            self.setCentralWidget(self.imagelabel)
    
            label1 = QLabel('hello, world')
            layout.addWidget(label1)
            listWidget = GUI()
    #        layout.addLayout(listWidget.getLayout())  # ---
            layout.addWidget(listWidget)               # +++
    
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    app.exec_()
    

    enter image description here

    Update:

    from PyQt5.QtWidgets import *
    from PyQt5.QtCore import *
    import sys
    
    class GUI(QObject):
        def __init__(self):
            super(GUI, self).__init__()
            self.layout = QVBoxLayout()
            self.initGUI()
    
        def getLayout(self):
            return self.layout
    
        def initGUI(self):
            self.listWidget = QListWidget()
            self.listWidget.addItem(QListWidgetItem('item1'))
            self.listWidget.addItem(QListWidgetItem('item2'))
            self.listWidget.itemClicked.connect(self.listwidgetclicked)
            self.layout.addWidget(self.listWidget)
    
        def listwidgetclicked(self, item):
            print('click -> {}'.format(item.text()))
    
    class MainWindow(QMainWindow):
        def __init__(self, parent=None):
            super(MainWindow, self).__init__(parent)
    
            self.imagelabel = QLabel()
            layout = QVBoxLayout()
            self.imagelabel.setLayout(layout)
            self.setCentralWidget(self.imagelabel)
    
            label1 = QLabel('hello, world')
            layout.addWidget(label1)
    
    
            self.listWidget = GUI()                        # <--- + self.
            layout.addLayout(self.listWidget.getLayout())  # <--- + self.
    
    
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    app.exec_()
    

    enter image description here