访问动态添加的小部件 (pyqt) [英] Accessing dynamically added widgets (pyqt)

查看:126
本文介绍了访问动态添加的小部件 (pyqt)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将文本添加到我使用按钮添加的文本框中,该文本框基于此问题的已接受答案:在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

但是我的问题是我无法访问我添加的文本框.这篇 帖子告诉我我可以使用 itemAt() 循环使用 addWidget() 添加到布局时添加的项目.但是我只能访问 QWidgetIem

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

这是我的代码:-

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

推荐答案

QWidgetItem 有一个 widget() 用于检索它包含的对象的函数:

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

这篇关于访问动态添加的小部件 (pyqt)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆