python - 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_())
解决方案
我不明白你想做什么,但是如果你把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_())
推荐阅读
- python - Pymysql 未知编码“latin1”
- android - 有没有办法在应用级别覆盖 Android 的平台属性值?
- python - Python PDF Randomizer 位置参数错误
- python - 从 Torchvision 模型中的 DICOM 文件上传数据
- javascript - NodeJS将文本缓冲区转换为可查看的HTML页面
- mysql - 是否可以在 GORM 的 OnConflict 子句中添加两个值?
- python - glOrtho 不工作它仍然被映射到默认坐标
- reactjs - 259 / 5000 Wyniki tłumaczenia 搜索引擎 - 从状态数组中消失的项目
- python - 尝试使用 Python 将解析的数据导出到 CSV 文件,但我不知道如何导出多于一行
- typescript - 如何确保 ionic-selectable 可以运行 onChange 事件两次?