在 PyQT 中向 QPlainTextEdit 添加文本(结果是状态日志) [英] Add text to QPlainTextEdit in PyQT (the result is a status log)
问题描述
我正在尝试构建一个 StatusLog,应用程序上发生的每一个动作.例如,用户单击了 button1,progressBox 必须接收类似用户单击了 button1"的字符串.
Iam trying to build a StatusLog, every move that happens on application. For example, the user clicked in the button1, the progressBox must receive a string like "user clicked the button1".
我已经搜索并找到了一个链接,但我无法解决这个问题,我不知道如何解决.
I've searched and found a link but I can't put that working out, I dont know how.
每次尝试都会给我,AttributeError: 'QPlainTextEdit' object has no attribute 'insertText'
Is given me everytime I try, AttributeError: 'QPlainTextEdit' object has no attribute 'insertText'
谢谢大家.最好的问候.
Thanks for all. Best Regards.
from PyQt4 import QtCore, QtGui
import sys, os
from math import ceil
from PyQt4 import QtCore, QtGui, QtNetwork
from qrtools import QR
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
_fromUtf8 = lambda s: s
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
(..)
#The button
self.groupBoxAuthbtn.setSizePolicy(sizePolicy)
self.groupBoxAuthbtn.setTitle(_fromUtf8(""))
self.groupBoxAuthbtn.setObjectName(_fromUtf8("groupBoxAuthbtn"))
self.horizontalLayout_2 = QtGui.QHBoxLayout(self.groupBoxAuthbtn)
self.horizontalLayout_2.setObjectName(_fromUtf8("horizontalLayout_2"))
self.btnAuthenticate = QtGui.QPushButton(self.groupBoxAuthbtn)
self.btnAuthenticate.setEnabled(True)
self.btnAuthenticate.setText(QtGui.QApplication.translate("MainWindow", "Auth from Webcam", None, QtGui.QApplication.UnicodeUTF8))
self.btnAuthenticate.setObjectName(_fromUtf8("btnAuthenticate"))
self.horizontalLayout_2.addWidget(self.btnAuthenticate)
self.horizontalLayout.addWidget(self.groupBoxAuthbtn)
#self.btnAuthenticate.clicked.connect(self.handleTest)
(...)
# THE STATUS LOG
self.groupBoxProgress = QtGui.QGroupBox(self.centralwidget)
sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.groupBoxProgress.sizePolicy().hasHeightForWidth())
self.groupBoxProgress.setSizePolicy(sizePolicy)
self.groupBoxProgress.setTitle(QtGui.QApplication.translate("MainWindow", "Status", None, QtGui.QApplication.UnicodeUTF8))
self.groupBoxProgress.setAlignment(QtCore.Qt.AlignCenter)
self.groupBoxProgress.setObjectName(_fromUtf8("groupBoxProgress"))
self.verticalLayout_3 = QtGui.QVBoxLayout(self.groupBoxProgress)
self.verticalLayout_3.setObjectName(_fromUtf8("verticalLayout_3"))
#Progress
self.progressBox = QtGui.QPlainTextEdit(self.groupBoxProgress)
self.progressBox.setPlainText(QtGui.QApplication.translate("MainWindow", "This is the all work report..", None, QtGui.QApplication.UnicodeUTF8))
self.progressBox.setObjectName(_fromUtf8("progressBox"))
self.verticalLayout_3.addWidget(self.progressBox)
self.verticalLayout.addWidget(self.groupBoxProgress)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 847, 25))
self.menubar.setObjectName(_fromUtf8("menubar"))
self.menuFile = QtGui.QMenu(self.menubar)
self.menuFile.setTitle(QtGui.QApplication.translate("MainWindow", "File", None, QtGui.QApplication.UnicodeUTF8))
self.menuFile.setObjectName(_fromUtf8("menuFile"))
self.menuAbout = QtGui.QMenu(self.menubar)
self.menuAbout.setTitle(QtGui.QApplication.translate("MainWindow", "About", None, QtGui.QApplication.UnicodeUTF8))
self.menuAbout.setObjectName(_fromUtf8("menuAbout"))
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
MainWindow.setStatusBar(self.statusbar)
self.toolBar = QtGui.QToolBar(MainWindow)
self.toolBar.setWindowTitle(QtGui.QApplication.translate("MainWindow", "toolBar", None, QtGui.QApplication.UnicodeUTF8))
self.toolBar.setObjectName(_fromUtf8("toolBar"))
MainWindow.addToolBar(QtCore.Qt.TopToolBarArea, self.toolBar)
self.actionExit = QtGui.QAction(MainWindow)
self.actionExit.setText(QtGui.QApplication.translate("MainWindow", "Exit", None, QtGui.QApplication.UnicodeUTF8))
self.actionExit.setShortcut(QtGui.QApplication.translate("MainWindow", "Q", None, QtGui.QApplication.UnicodeUTF8))
self.actionExit.setObjectName(_fromUtf8("actionExit"))
self.actionCredits = QtGui.QAction(MainWindow)
self.actionCredits.setText(QtGui.QApplication.translate("MainWindow", "Credits", None, QtGui.QApplication.UnicodeUTF8))
self.actionCredits.setShortcut(QtGui.QApplication.translate("MainWindow", "A", None, QtGui.QApplication.UnicodeUTF8))
self.actionCredits.setObjectName(_fromUtf8("actionCredits"))
self.menuFile.addSeparator()
self.menuFile.addAction(self.actionExit)
self.menuAbout.addAction(self.actionCredits)
self.menubar.addAction(self.menuFile.menuAction())
self.menubar.addAction(self.menuAbout.menuAction())
self.retranslateUi(MainWindow)
QtCore.QObject.connect(self.actionExit, QtCore.SIGNAL(_fromUtf8("activated()")), MainWindow.close)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
MainWindow.setTabOrder(self.btnAuthenticate, self.progressBox)
def retranslateUi(self, MainWindow):
pass
推荐答案
尝试 insertPlainText()
代替.文档没有提到insertText
所以错误是非常具有描述性.
Try insertPlainText()
instead. The documentation does not mention insertText
so the error is quite descriptive.
这篇关于在 PyQT 中向 QPlainTextEdit 添加文本(结果是状态日志)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!