有错误,我该如何提供?
我想用删除按钮删除小部件。这没关系self.removeButton.clicked.connect(self.removing.remove_widget)
?我试图用按钮连接另一个类。
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)
您创建了一个新类Test,并连接了新的Test,然后是按钮del。
你可以试试这个。
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()