Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
427 views
in Technique[技术] by (71.8m points)

python - Remove QPushButton from QVBoxLayout in QScrollArea

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...