python - 访问动态添加的小部件(pyqt)

标签 python pyqt qtgui

我想将文本添加到文本框中,该文本框是我根据此问题的接受答案使用按钮添加的:Dynamically adding and removing widgets in pyqt

但是我的问题是我无法访问我添加的文本框。 This帖子告诉我,我可以使用 itemAt() 循环使用 addWidget() 添加到布局时添加的项目。但是我只能访问 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()用于检索其包含的对象的函数:

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

关于python - 访问动态添加的小部件(pyqt),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28819643/

相关文章:

c++ - 添加图标时QMenu项目文本消失

python - 将多个html文件抓取到CSV

python - Slack 机器人 [Python/Flask] : Wait for Direct Messages from specified users and save the responses

python - 使 Qt 菜单以编程方式出现

c++ - 如何设置 QFormLayout 第一列的最小宽度?

qt - 在PyQt中覆盖QPaintEvents

python - 带补丁的剪切轴

python - TensorFlow 的内存泄漏

python - 当状态更改为媒体结束时 QMediaPlayer 状态未停止

python - 具有 3 个不同选项的按钮