单击任何按钮都无法执行hello函数。
那么如何将comboList []的QPushButton连接到属于tst []中测试对象的hello方法的不同实例,以便函数hello()可以针对t的不同参数运行? >
由于我们无法使用QpushButton的connect()方法将args传递给函数。除了使用类的实现之外,实现该目标(使用许多按钮为不同参数运行相同功能)的解决方法有哪些?
from PyQt5 import QtGui
from PyQt5.QtWidgets import QApplication, QWidget, QScrollArea, QVBoxLayout, QGroupBox, QLabel, QPushButton, QFormLayout
import sys
class test:
def __init__(self, t):
self.to = t
def hello(self):
print('hello',str(self.to))
class Window(QWidget):
def __init__(self, val):
super().__init__()
self.title = "PyQt5 Scroll Bar"
self.top = 200
self.left = 500
self.width = 400
self.height = 300
self.setWindowIcon(QtGui.QIcon("icon.png"))
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
formLayout =QFormLayout()
groupBox = QGroupBox("This Is Group Box")
labelList = []
comboList = []
tst = []
for i in range(val):
labelList.append(QLabel("Label"))
comboList.append(QPushButton(str(i)))
tst.append(test(i))
formLayout.addRow(labelList[i], comboList[i])
comboList[i].clicked.connect(tst[i].hello)# Connect here
groupBox.setLayout(formLayout)
scroll = QScrollArea()
scroll.setWidget(groupBox)
scroll.setWidgetResizable(True)
layout = QVBoxLayout(self)
layout.addWidget(scroll)
self.show()
App = QApplication(sys.argv)
window = Window(1500)
sys.exit(App.exec())