从 QScrollArea 的 QVBoxLayout 中删除 QPushButton

问题描述

我使用了以下代码https://www.learnpyqt.com/tutorials/qscrollarea/。 我只将标签更改为 QPushButtons。 我的问题是我无法从 QScrollArea 中删除 PushButton 'Delete Me'... 有没有人有想法?

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_())

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)