QFormLayout [중복]에서 clicked.connect () QPushButton 수 없습니다.

Dec 22 2020

이것은 전체 코드입니다. 왜 clicked.connect를 내 마음에 사용할 수 없는지 모르겠습니다. 여전히 논리 '-'이지만 왜 !!! ???

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


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")
    labelLis = []
    comboList = []
    for i in range(val):
        labelLis.append(QLabel("Label"))
        comboList.append(QPushButton("Click Me").clicked.connect(print("hello")))
        formLayout.addRow(labelLis[i], comboList[i])
    groupBox.setLayout(formLayout)

    scroll = QScrollArea()
    scroll.setWidget(groupBox)
    scroll.setWidgetResizable(True)
    scroll.setFixedHeight(400)
    layout = QVBoxLayout(self)
    layout.addWidget(scroll)
    self.show()


App = QApplication(sys.argv)
window = Window(30)
sys.exit(App.exec())

그리고 여기에 오류가 있습니다. 모든 항목을 클릭 할 수 있도록하고 싶지만 왜 이런 일이 발생하는지 모르겠습니다. "D

for i in range(val):
        labelLis.append(QLabel("Label"))
        comboList.append(QPushButton("Click Me").clicked.connect(print("hello")))
        formLayout.addRow(labelLis[i], comboList[i])
    groupBox.setLayout(formLayout)

이 코드는 작동하지 않습니다. comboList.append(QPushButton("Click Me").clicked.connect(print("hello")))

답변

dudulu Dec 22 2020 at 12:48

comboList.append(QPushButton("Click Me").clicked.connect(self.hello)오류가 발생하는 이유를 모르겠습니다.

하지만 나는 할 것이다

qbutton = QPushButton("Click Me")
qbutton.clicked.connect(self.hello)

결과

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


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")
        labelLis = []
        comboList = []

        for i in range(val):
            qbutton = QPushButton("Click Me")
            qbutton.clicked.connect(self.hello)
            labelLis.append(QLabel("Label"))
            comboList.append(qbutton)
            formLayout.addRow(labelLis[i], comboList[i])

        groupBox.setLayout(formLayout)

        scroll = QScrollArea()
        scroll.setWidget(groupBox)
        scroll.setWidgetResizable(True)
        scroll.setFixedHeight(400)
        layout = QVBoxLayout(self)
        layout.addWidget(scroll)
        self.show()

    def hello(self):
        print('hello')


App = QApplication(sys.argv)
window = Window(30)
sys.exit(App.exec())