简体   繁体   中英

Accessing dynamically added widgets (pyqt)

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

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!')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM