将QTextEdit添加到QScrollArea

时间:2018-03-03 15:12:44

标签: python pyqt pyqt5 qtextedit

我的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("\n")
        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("\n")
        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("\n")
        self.textEdit.append("These reports, much like the parsed forensic data are stored in a Forensic Reports directory.")
        self.textEdit.append("\n")
        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'

1 个答案:

答案 0 :(得分:1)

QTextEdit已经有QScrollBar因此您不应添加任何内容,如果您希望文字不可编辑,则应使用setReadOnly(True)代替setEnabled(False)

# passwordTF
self.textEdit = QtWidgets.QTextEdit(self.centralwidget)
[...]
self.textEdit.setFont(font)
self.textEdit.setReadOnly(True)