将QTextEdit添加到QScrollArea
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了将QTextEdit添加到QScrollArea相关的知识,希望对你有一定的参考价值。
我的QTextEdit区域中有一个文本块,但它对于该区域来说太大了。我尝试将textArea添加到QScrollArea以使我能够滚动文本。我想知道我在哪里出错,因为它似乎没有做任何事情。我想知道什么是正确的做法。这是我输出的堆栈跟踪代码。
# Import Statements
from PyQt5 import QtCore, QtGui, QtWidgets
from MainGuiWindow import Ui_MainWindow
# Main Class that holds User Interface Objects
class Ui_informationWindow(object):
# Function for Opening Main GUI window from login window by clicking login button
def openMainWindow(self):
self.window = QtWidgets.QMainWindow()
self.ui = Ui_MainWindow()
self.ui.setupUi(self.window)
loginWindow.hide()
self.window.show()
def setupUi(self, informationWindow):
informationWindow.setObjectName("User Information Window")
informationWindow.setFixedSize(393, 300)
self.centralwidget = QtWidgets.QWidget(informationWindow)
self.centralwidget.setObjectName("centralwidget")
informationWindow.setStyleSheet("background-color: Cornflowerblue")
# Proceed button
self.proceedButton = QtWidgets.QPushButton(self.centralwidget)
self.proceedButton.setGeometry(QtCore.QRect(280, 250, 101, 27))
font = QtGui.QFont()
font.setFamily("Arial")
font.setPointSize(12)
font.setBold(True)
font.setWeight(75)
self.proceedButton.setFont(font)
self.proceedButton.setStyleSheet("background-color: Silver")
self.proceedButton.setObjectName("proceedButton")
# passwordTF
self.textEdit = QtWidgets.QTextEdit(self.centralwidget)
self.textEdit.setGeometry(QtCore.QRect(20, 10, 350, 230))
self.textEdit.setObjectName("informationTF")
informationWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(loginWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 393, 21))
self.menubar.setObjectName("menubar")
informationWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(informationWindow)
self.statusbar.setObjectName("statusbar")
self.textEdit.setStyleSheet("background-color: White")
# Set Scroll Area
self.textEdit.append("This is an information manual to guide users through the use of the software.")
self.textEdit.append("
")
self.textEdit.append("To use this software, the user must first parse the data from each evidence item which will store the data in Microsoft Excel table format in an external directory")
self.textEdit.append("In turn, the corresponding report generation options will become available one by one to enable report generation for that piece of evidence.")
self.textEdit.append("
")
self.textEdit.append("Once each report has been generated, the user can then view a single report for one piece of evidence or view a full report containing all parsed evidence.")
self.textEdit.append("
")
self.textEdit.append("These reports, much like the parsed forensic data are stored in a Forensic Reports directory.")
self.textEdit.append("
")
self.textEdit.append("Please press the Proceed button to begin using the software.")
self.textEdit.setFont(font)
self.textEdit.setEnabled(False)
# This is where I am trying to put the textEdit text into a scrollArea to make it scrollable
self.scrollArea = QtWidgets.QScrollArea()
self.textEdit.setVerticalScrollBar(self.textEdit)
informationWindow.setStatusBar(self.statusbar)
self.retranslateUi(informationWindow)
QtCore.QMetaObject.connectSlotsByName(informationWindow)
# Function that sets the text on all the UI Buttons
def retranslateUi(self, loginWindow):
_translate = QtCore.QCoreApplication.translate
loginWindow.setWindowTitle(_translate("informationWindow", "User Manual"))
self.proceedButton.setText(_translate("informationWindow", "Proceed"))
# Event Handling Code Section
# Event Handling to open Main GUI Window
self.proceedButton.clicked.connect(self.openMainWindow)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
loginWindow = QtWidgets.QMainWindow()
ui = Ui_informationWindow()
ui.setupUi(loginWindow)
loginWindow.show()
sys.exit(app.exec_())
堆栈跟踪
self.textEdit.setVerticalScrollBar(self.textEdit)
TypeError: QAbstractScrollArea.setVerticalScrollBar(QScrollBar): argument 1 has unexpected type 'QTextEdit'
答案
QTextEdit
已经有一个QScrollBar
所以你不应该添加任何,如果你想要文本不可编辑你应该使用setReadOnly(True)
而不是setEnabled(False)
。
# passwordTF
self.textEdit = QtWidgets.QTextEdit(self.centralwidget)
[...]
self.textEdit.setFont(font)
self.textEdit.setReadOnly(True)
以上是关于将QTextEdit添加到QScrollArea的主要内容,如果未能解决你的问题,请参考以下文章
将垂直滚动条添加到嵌入式 matplotlib 画布,同时保持其水平大小占据整个 QScrollArea
QScrollArea 垂直 sizePolicy - 将内容调整到最大高度