I have used the Code as given by:https://www.learnpyqt.com/tutorials/qscrollarea/.
I only changed the Labels to QPushButtons.
My Problem is that I cannot remove the PushButton 'Delete Me' from my QScrollArea... Does any one have an idea?
from PyQt5.QtCore import Qt, QSize
from PyQt5 import QtWidgets, uic
import sys
from PyQt5.QtWidgets import (QWidget, QSlider, QLineEdit, QLabel, QPushButton, QScrollArea,QApplication,
QHBoxLayout, QVBoxLayout, QMainWindow)
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.scroll = QScrollArea() # Scroll Area which contains the widgets, set as the centralWidget
self.widget = QWidget() # Widget that contains the collection of Vertical Box
self.vbox = QVBoxLayout() # The Vertical Box that contains the Horizontal Boxes of labels and buttons
for i in range(1,20):
if i is not 4:
object = QPushButton("TextLabel")
else:
object = QPushButton("Delete Me")
self.vbox.addWidget(object)
self.widget.setLayout(self.vbox)
#Scroll Area Properties
self.scroll.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
self.scroll.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.scroll.setWidgetResizable(True)
self.scroll.setWidget(self.widget)
self.setCentralWidget(self.scroll)
self.setGeometry(600, 100, 1000, 900)
self.setWindowTitle('Scroll Area Demonstration')
self.show()
for index in range(self.vbox.count()):
print(self.vbox.itemAt(index).widget().text())
print(self.vbox.itemAt(index).widget().text())
if self.vbox.itemAt(index).widget().text() == 'Delete Me':
print('found it')
self.vbox.removeWidget(self.vbox.itemAt(index).widget())
return
def main():
app = QtWidgets.QApplication(sys.argv)
main = MainWindow()
sys.exit(app.exec_())
question from:
https://stackoverflow.com/questions/65924429/remove-qpushbutton-from-qvboxlayout-in-qscrollarea 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…