Accessing dynamically added widgets (pyqt)

给你一囗甜甜゛ 提交于 2019-12-08 04:35:52

问题


I want add text into a text box which I've added using a push button based on the accepted answer for this question : Dynamically adding and removing widgets in pyqt

My problem however is that I cannot access the text boxes I've added. This post tells me that I can use itemAt() to loop through items added when using addWidget() to add to layout. However I can only access the 'inner layout' of type QWidgetIem

Here's my code : -

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)

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

        # 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.scrollArea)

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

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





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

        # This doesn't work
        print self.scrollLayout.itemAt(0)

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

     # Sensor Indicator
        self.pushButton = QtGui.QPushButton()
        self.pushButton.setStyleSheet("background-color: green")

     # Console Window to display sensor data
        self.logOutput = QtGui.QTextEdit()
        self.logOutput.setReadOnly(True)
        self.logOutput.setLineWrapMode(QtGui.QTextEdit.NoWrap)
        self.font = self.logOutput.font()
        self.font.setFamily("Courier")
        self.font.setPointSize(10)


        layout = QtGui.QVBoxLayout()
        layout.addWidget(self.pushButton)
        layout.addWidget(self.logOutput)
        self.setLayout(layout)



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

回答1:


A QWidgetItem has a widget() function for retrieving the object it contains:

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

        index = self.scrollLayout.count() - 1
        widget = self.scrollLayout.itemAt(index).widget()
        if widget is not None:
            widget.logOutput.setText('Hello World!')


来源:https://stackoverflow.com/questions/28819643/accessing-dynamically-added-widgets-pyqt

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!