In the example below I am using PyQt6 to create text with long lines that exceed the QtextEdit widget’s viewing size vertically and horizontally. The vertical scroll bar shows up however, the horizontal scroll bar does not show up. Any help with this issue is appreciated.
import sys from PyQt6.QtWidgets import (QApplication, QMainWindow, QPushButton, QWidget, QHBoxLayout, QVBoxLayout, QTextEdit) from PyQt6.QtCore import QSize class MainWindow(QMainWindow): def __init__(self): super().__init__() self.setWindowTitle("Example") self.setContentsMargins(20,20,20,20) self.setFixedSize(QSize(800, 600)) self.setWindowTitle("Example") layout1 = QHBoxLayout() layout2 = QVBoxLayout() self.output_text = QTextEdit() self.button_start = QPushButton("Start") self.button_start.clicked.connect(self.start) layout1.addLayout(layout2) layout2.addWidget(self.output_text) layout2.addWidget(self.button_start) widget = QWidget() widget.setLayout(layout1) self.setCentralWidget(widget) def start(self): for i in range (1, 30): self.output_text.append("Line " + str(i) + ": This is a long line. This is a long line. This is a long line. This is a long line. This is a long line. This is a long line. This is a long line. This is a long line. ") app = QApplication(sys.argv) window = MainWindow() window.show() app.exec()
Advertisement
Answer
If you want a horizontal scrollbar, you will need to set the QTextEdit column width higher than the QTextEdit area. So let’s say for example 1000 will work for your code.
import sys from PyQt6.QtWidgets import (QApplication, QMainWindow, QPushButton, QWidget, QHBoxLayout, QVBoxLayout, QTextEdit) from PyQt6.QtCore import QSize, Qt class MainWindow(QMainWindow): def __init__(self): super().__init__() self.setWindowTitle("Example") self.setContentsMargins(20,20,20,20) self.setFixedSize(QSize(800, 600)) self.setWindowTitle("Example") layout1 = QHBoxLayout() layout2 = QVBoxLayout() self.output_text = QTextEdit() self.output_text.setLineWrapColumnOrWidth(1000) #Here you set the width you want self.output_text.setLineWrapMode(QTextEdit.LineWrapMode.FixedPixelWidth) self.button_start = QPushButton("Start") self.button_start.clicked.connect(self.start) layout1.addLayout(layout2) layout2.addWidget(self.output_text) layout2.addWidget(self.button_start) widget = QWidget() widget.setLayout(layout1) self.setCentralWidget(widget) def start(self): for i in range (1, 30): self.output_text.append("Line " + str(i) + ": This is a long line. This is a long line. This is a long line. This is a long line. This is a long line. This is a long line. This is a long line. This is a long line. ") app = QApplication(sys.argv) window = MainWindow() window.show() app.exec()
Tell me if this wasn’t what you were looking for or if it doesn’t work for you.