[已解决]我想设置QTextEdit文档的大小(例如8.5 x 11或3 x 5)。如果用户缩小视图,则应显示水平滚动条。当用户缩小视图时,我找不到停止换行的设置。如果我完全停止换行,则将忽略所有页面尺寸设置。

#!/usr/bin/python
#Python version: 3.4.1
#Pyside.__version__ 1.2.2
#PySide.__version_info__  (1, 2, 2, 'final', 0)
#PySide.QtCore.__version__ 4.8.5
#PySide.QtCore.__version_info__  (4, 8, 5)
#PySide.QtCore.qVersion() 4.8.5
# -*- coding: utf-8 -*-
"""
Horizontal Scrollbar test
"""
import sys, os
from PySide.QtCore import QSizeF
from PySide.QtGui import (QApplication, QTextEdit, QTextOption)

class myTextEdit(QTextEdit):
    def __init__(self, parent=None):
        super(myTextEdit, self).__init__(parent)
    self.parent = parent

    #FixedWidth disables re-size - NO H-scrollbar will appear
    #        self.setFixedWidth(500)

    #LineWrapMode sets a Maximum line width for wrap, yet
    #re-sizing the view wraps text before maximum - NO H-scrollbar appear
    self.setLineWrapMode(QTextEdit.FixedColumnWidth)
    self.setLineWrapColumnOrWidth(80)

    #PageSize sets a Maximum for wrap (same as above) NO H-scrollbar
    #        pageSize = QSizeF()
    #        pageSize.setWidth(80)
    #        self.document().setPageSize(pageSize)

    #NoWrap disables all above code. H-bar will show, but line never wraps.
    #        self.setWordWrapMode(QTextOption.NoWrap)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    myWidget = myTextEdit()

    myWidget.show()
    sys.exit(app.exec_())

最佳答案

实际上,将linewrapmode设置为FixedPixelWidth即可实现所需功能,即出现水平滚动条,并且将行包装在固定位置。我不知道为什么您的示例(以及此处)中的FixedColumnWidth没有给出类似的结果。

例:

from PySide import QtGui, QtCore

app = QtGui.QApplication([])
window = QtGui.QWidget()
layout = QtGui.QVBoxLayout(window)
edit = QtGui.QTextEdit('jfdh afdhgfkjg fdnvfh vklkfjvkflj lddkl ljklfjkl jvkldjfkvljfgvjldf ll dl dljvklj ljljlbl  llkb jbgl')
edit.setLineWrapColumnOrWidth(200)
edit.setLineWrapMode(QtGui.QTextEdit.FixedPixelWidth)
layout.addWidget(edit)
window.show()
app.exec_()

关于python - 调整大小后,QTextEdit不显示水平滚动条,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25839669/

10-12 18:52