该插槽不适用于 QListWidget itemClicked (pyqt)

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

我创建了一个 QListWidget,我想将 itemClicked 连接到我的插槽。下面的代码是可以的:当我点击该项目时,listwidgetclicked 已经被调用

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

但是,我想把GUI的所有代码放在一起。所以我创建了一个class,并在这个class中连接信号和槽。当我点击该项目时,listwidgetclicked 不起作用,代码为:

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

我不知道这两个代码有什么区别。

试一试:

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

更新:

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