user3755529
user3755529

Reputation: 1186

pyQt: Scolling QScrollArea Not Each QTextEdit Individually

having many QTextEdit next to each other (I have a table subset HTML in them) I don't want them to get a scrollbar each when they overflow the window space.

I want the window to have a global scrollbar. What's the best practice?

here the code as example:

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import QCoreApplication, QRect, Qt

class MainWindow(QScrollArea):
    def __init__(self):
        super().__init__()

        layout = QHBoxLayout(self)

        text = ''
        for i in range(0,1000):
            text = '{0} {1}\n'.format(text, i)

        for i in range(0,10):
            textEdit = QTextEdit()
            layout.addWidget(textEdit)
            textEdit.setText(text)

        self.resize(600,400)

        self.show()

if __name__ == '__main__':

    app = QApplication(sys.argv)
    ex = MainWindow()
    sys.exit(app.exec_())

enter image description here

I can add a container to the above and it gets rid of the scrollbars, but then I can not scroll anything at all

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import QCoreApplication, QRect, Qt

class MainWindow(QScrollArea):
    def __init__(self):
        super().__init__()

        container = QFrame(self)
        container.resize(600,15000)

        layout = QHBoxLayout(container)

        text = ''
        for i in range(0,1000):
            text = '{0} {1}\n'.format(text, i)

        for i in range(0,10):
            textEdit = QTextEdit()
            layout.addWidget(textEdit)
            textEdit.setText(text)

        self.resize(600,400)

        self.show()

if __name__ == '__main__':

    app = QApplication(sys.argv)
    ex = MainWindow()
    sys.exit(app.exec_())

Upvotes: 2

Views: 1616

Answers (1)

ekhumoro
ekhumoro

Reputation: 120578

It looks like all you need to do is set the vertical scroll-bar policy on the text-edits and then add the container widget to the scroll-area:

class MainWindow(QScrollArea):
    def __init__(self):
        super().__init__()

        container = QFrame(self)
        container.resize(600,15000)

        layout = QHBoxLayout(container)

        text = ''
        for i in range(0,1000):
            text = '{0} {1}\n'.format(text, i)

        for i in range(0,10):
            textEdit = QTextEdit()
            layout.addWidget(textEdit)
            textEdit.setText(text)
            textEdit.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)

        self.setWidget(container)

        self.resize(625,400)

        self.show()

Upvotes: 1

Related Questions