Search code examples
pythonqtpysideqtextedit

QTextEdit doesn't show horizontal scrollbar upon resizing


[SOLVED]I would like to set the size for a QTextEdit document (say 8.5 x 11 or 3 x 5). If the user shrinks the view, then the horizontal scroll bar should appear. I cannot find the settings to stop linewrap when the user shrinks the view. If I stop linewrap completely, then all page size settings are ignored.

#!/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_())

Solution

  • Setting the linewrapmode to FixedPixelWidth actually does what you want, i.e. horizontal scrollbars appear and lines are wrapped at a fixed position. I don't know why FixedColumnWidth in your example (and also here) doesn't give a similar result.

    Example:

    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_()