如何将QPushButton的不同实例连接到同一函数,相同类但具有不同类变量的不同实例

问题描述 投票:0回答:1

我无法通过单击任何按钮使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())
python pyqt pyqt5
1个回答
0
投票

尝试:

import sys
from PyQt5 import QtGui
from PyQt5.QtWidgets import (QApplication, QWidget, QScrollArea, QVBoxLayout, 
                             QGroupBox, QLabel, QPushButton, QFormLayout)

class test:
    def __init__(self, t):
        self.to = t

    def hello(self, t):                           # +++
        print('hello', t) #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
            comboList[i].clicked.connect(lambda ch, i=i: tst[i].hello(i))          # ++++

        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(100)
sys.exit(App.exec())

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.