pyQt:Scolling QScrollArea 不是每个 QTextEdit 单独

pyQt: Scolling QScrollArea Not Each QTextEdit Individually

有许多 QTextEdit 彼此相邻(我在其中有一个 table 子集 HTML)我不希望它们在溢出 window 时各自获得一个滚动条space.

我希望 window 有一个全局滚动条。 最佳做法是什么?

此处以代码为例:

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

我可以在上面添加一个容器,它摆脱了滚动条,但是我根本无法滚动任何东西

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

看来您只需要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()