首页 > 解决方案 > QListWidget 中的按钮不起作用(使用 PyQt5)

问题描述

我是 PyQt5 的新手,正在学习在项目中使用 QListWidget。我的问题是当我将三个 PushButtons 放入 QListWidget 时。但是当我单击按钮时,什么也没发生,我找不到问题的原因。那么,有人可以帮帮我吗?非常感谢,以下是我的全部代码。

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

class App(QWidget):
    def __init__(self):
        super().__init__()
        self.title = 'Main Window'
        self.left = 300
        self.top = 300
        self.width = 840
        self.height = 580
        self.Contents = QStackedWidget()
        self.initUI()

    def initUI(self):

        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)
        # the following is to create a ToolBar  
        self.toolbar = QToolBar()
        self.toolbar.setStyleSheet("background-color: rgb(200, 155, 155);" 'spacing:20px;')
        self.toolbar.setFixedHeight(86)
        self.toolbar.setMovable( False)

        # create three pushbutton called Button1, Button2, Button3
        Button1 = QPushButton(self)
        Button1.setText("Home Button")


        Button2 = QPushButton(self)
        Button2.setText("Simulation")

        Button3 = QPushButton(self)
        Button3.setText("Practice")

        # create QListWidget and add the buttons into it
        self.itemN = QListWidgetItem() 
        self.funList = QListWidget()
        self.widget = QWidget()

        widgetLayout = QHBoxLayout()
        widgetLayout.addWidget(Button1)
        widgetLayout.addWidget(Button2)
        widgetLayout.addWidget(Button3)
        widgetLayout.addStretch()
        widgetLayout.setSizeConstraint(QLayout.SetFixedSize)
        self.widget.setLayout(widgetLayout) 

        self.funList.addItem(self.itemN)
        self.funList.setItemWidget(self.itemN, self.widget)
        self.funList.clicked.connect(self.clicked_check) # this click seems not working

        #put the QlistWidget into the toolbar
        self.toolbar.addWidget(self.widget)
        vbox = QVBoxLayout()
        vbox.addWidget(self.toolbar)
        self.setLayout(vbox)      
        self.show()

    @pyqtSlot()
    def clicked_check(self):
        alert = QMessageBox()
        alert.setText('This Button works')
        alert.exec_()   

if not QApplication.instance():
    app = QApplication(sys.argv)
else:
    app = QApplication.instance()
ex = App()
sys.exit(app.exec_())

标签: pythonpyqtpyqt5qlistwidget

解决方案


我不明白你想做什么,但是如果你把self.funList布局放进去,我们会得到以下结果:

在此处输入图像描述

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

class App(QWidget):
    def __init__(self):
        super().__init__()
        self.title = 'Main Window'
        self.left = 300
        self.top = 100
        self.width = 840
        self.height = 380
        self.Contents = QStackedWidget()
        self.initUI()

    def initUI(self):

        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)
        # the following is to create a ToolBar  
        self.toolbar = QToolBar()                                                        
        self.toolbar.setStyleSheet("background-color: rgb(200, 155, 155);" 'spacing:20px;')
        self.toolbar.setFixedHeight(86)
        self.toolbar.setMovable( False)

        # create three pushbutton called Button1, Button2, Button3
        Button1 = QPushButton(self)
        Button1.setText("Home Button")
        Button1.clicked.connect(lambda: print(Button1.text()))        # +

        Button2 = QPushButton(self)
        Button2.setText("Simulation")

        Button3 = QPushButton(self)
        Button3.setText("Practice")

        # create QListWidget and add the buttons into it
        self.itemN = QListWidgetItem() 

        self.funList = QListWidget()
        self.widget = QWidget()
#        self.widget.setStyleSheet("background-color: rgb(255, 155, 000);")
#        self.funList.setStyleSheet("background-color: rgb(255, 000, 155);")

        widgetLayout = QHBoxLayout()
        widgetLayout.addWidget(Button1)
        widgetLayout.addWidget(Button2)
        widgetLayout.addWidget(Button3)
        widgetLayout.addStretch()
        widgetLayout.setSizeConstraint(QLayout.SetFixedSize)
        self.widget.setLayout(widgetLayout) 

        self.funList.addItem(self.itemN)
        self.itemN.setSizeHint(self.widget.sizeHint())

        self.funList.setItemWidget(self.itemN, self.widget)
        self.funList.clicked.connect(self.clicked_check)      # this click seems not working

        #put the QlistWidget into the toolbar
        self.toolbar.addWidget(self.widget)

        vbox = QVBoxLayout(self)

        vbox.addWidget(self.funList)                                            # <<<-----<

        vbox.addWidget(self.toolbar)
        self.setLayout(vbox)      
        self.show()

    @pyqtSlot()
    def clicked_check(self):
        alert = QMessageBox()
        alert.setText('This Button works')
        alert.exec_()   

if not QApplication.instance():
    app = QApplication(sys.argv)
else:
    app = QApplication.instance()
ex = App()
sys.exit(app.exec_())

推荐阅读