2016-09-18 3 views
0

오류가 있습니다. 어떻게 제공합니까? 제거 버튼이있는 위젯을 제거하고 싶습니다. 괜찮습니까 self.removeButton.clicked.connect(self.removing.remove_widget)? 푸시 버튼으로 다른 클래스에 연결하려고했습니다.다른 클래스 (PyQt)에서 버튼을 클릭하여 위젯을 제거하는 방법

from PyQt4 import QtGui, QtCore 
import sys 

class Main(QtGui.QMainWindow): 
    def __init__(self, parent = None): 
     super(Main, self).__init__(parent) 

     # main button 
     self.addButton = QtGui.QPushButton('button to add other widgets') 
     self.addButton.clicked.connect(self.addWidget) 

     self.removing=Test() 
     self.removeButton=QtGui.QPushButton("remove widget") 
     self.removeButton.clicked.connect(self.removing.remove_widget) 

     # scroll area widget contents - layout 
     self.scrollLayout = QtGui.QFormLayout() 

     # scroll area widget contents 
     self.scrollWidget = QtGui.QWidget() 
     self.scrollWidget.setLayout(self.scrollLayout) 

     # scroll area 
     self.scrollArea = QtGui.QScrollArea() 
     self.scrollArea.setWidgetResizable(True) 
     self.scrollArea.setWidget(self.scrollWidget) 

     # main layout 
     self.mainLayout = QtGui.QVBoxLayout() 

     # add all main to the main vLayout 
     self.mainLayout.addWidget(self.addButton) 
     self.mainLayout.addWidget(self.removeButton) 
     self.mainLayout.addWidget(self.scrollArea) 

     # central widget 
     self.centralWidget = QtGui.QWidget() 
     self.centralWidget.setLayout(self.mainLayout) 

     # set central widget 
     self.setCentralWidget(self.centralWidget) 

    def addWidget(self): 
     self.scrollLayout.addRow(Test()) 


class Test(QtGui.QWidget): 
    def __init__(self, parent=None): 
     super(Test, self).__init__(parent) 

     self.lineEdit = QtGui.QLineEdit('I am in Test widget') 

     layout = QtGui.QHBoxLayout() 
     layout.addWidget(self.lineEdit) 
     self.setLayout(layout) 

    def remove_widget(self): 
     self.lineEdit.deleteLater() 




app = QtGui.QApplication(sys.argv) 
myWidget = Main() 
myWidget.show() 
app.exec_() 
+0

어떤 오류가 발생합니까? –

답변

1

테스트를 새로 작성한 다음 새 테스트를 연결 한 다음 버튼을 클릭하십시오.

시도해 볼 수 있습니다.

self.kod = [] 
self.removeButton=QtWidgets.QPushButton("remove widget") 
self.removeButton.clicked.connect(self.remove_widget) 
... 

def addWidget(self): 
    temp = Test() 
    self.kod.append(temp) 
    self.scrollLayout.addRow(temp) 

def remove_widget(self): 
    self.kod.pop().deleteLater() 
+0

그것은 일했다, 감사한다. 그러나 couldnt는 이해한다. 왜 kod (list)와 scrollLayout 행 사이에 연결이 있는지. 목록에서 항목을 팝하는 경우 행이 삭제되는 이유는 무엇입니까? –

+0

@Tony Stark .. 죄송합니다.이 문제를 설명 할 수는 있지만 영어 계정을 사용할 수 없습니다. 나는 시험해 본다. 위젯을 추가하면 kod (목록) 및 scrollLayout에 추가됩니다. 우리는 pop()을 사용했습니다, 그것은 (kod) del 한 마지막 것 그리고 그것을 돌려줍니다 (마지막). 그런 다음 del 자체 (deleteLater()). 그리고 kod (목록) 목록 만 있으면 Test()를 찾으십시오. – Cyrbuzz

관련 문제