【问题标题】:Buttons in QListWidget not working (Using PyQt5)QListWidget 中的按钮不起作用(使用 PyQt5)
【发布时间】:2020-01-19 17:05:24
【问题描述】:

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

【问题讨论】:

  • 将每个按钮连接到插槽,而不是列表小部件。在 slot 内使用self.sender() 找出点击了哪个按钮。
  • 感谢 Ekhumoro,我之所以尝试 QlistWidget 是因为在此之后,我需要应用 QstackWidget,在同一区域交替打开三个较小的窗口。我的逻辑有什么问题吗?或者我可以通过其他方式做到这一点?
  • @user1933836 根据文档,QListWidget 仅用于保存 QListWidgetItems 和 QListWidgetItems 是具有特定属性的特定对象,这些属性通常是 Text 和 Icons,其中 Text 使用 setText 设置,而 Icon 设置为setIcon - 从文档看来, QListWidget 对象并不意味着要保存 QPushButtons ,因为您似乎暗示您正在尝试这样做。那么,您实际上是在尝试将 QPushButton 放入 QListWidget 中吗?
  • @Dennis Jensen,是的,我试图将 QPushButton 放入 QListWidget。所以看来我不应该这样做,对吧?
  • @user1933836 是的,当然,您可以将其他小部件添加到列表小部件 - 这正是 setItemWidget 的用途。不过,在这种特殊情况下您是否真的需要这样做尚不清楚。请编辑您的问题并更清楚地解释您要达到的目标 - 您以前的 cmet 没有多大意义。

标签: python pyqt pyqt5 qlistwidget


【解决方案1】:

我不明白你想要做什么,但是如果你把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_())

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-12
    • 2015-12-22
    • 1970-01-01
    • 1970-01-01
    • 2021-07-21
    • 2020-10-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多