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

时间:2019-01-30 09:09:43

标签: python-3.x pyqt connect qlistwidget

我创建了一个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的所有代码放在一起。因此,我创建了一个类,并在该类中连接信号和插槽。当我单击该项目时,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_()

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

1 个答案:

答案 0 :(得分:1)

尝试一下:

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

更新:

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