如何在PyQt环境中将两个滚动条(垂直和水平)设置为同一个小部件?

时间:2017-12-15 16:03:02

标签: python pyqt pyqt5 qscrollarea

我试图为QGroupBox制作两个滚动条,但我只成功拥有一个(垂直一个) 我不确定自己需要做什么。

这是我的代码的简短示例:

from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
import sys


class SurfViewer(QMainWindow):
    def __init__(self, parent=None):
        super(SurfViewer, self).__init__()
        self.parent = parent

        self.centralTabs= QTabWidget()
        self.setCentralWidget(self.centralTabs)
        self.setFixedWidth(200)
        self.setFixedHeight(200)

        #tab Model selection
        self.tab_ModelSelect = QWidget()
        self.centralTabs.addTab(self.tab_ModelSelect,"Label")


        self.groupscrolllayouttest = QHBoxLayout()  
        self.groupscrollbartest = QGroupBox()  


        self.mainHBOX_param_scene = QVBoxLayout()
        for i in range(10):
            Label = QLabel('BlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBla')
            Label.setFixedWidth(200)
            self.mainHBOX_param_scene.addWidget(Label)


        #
        scroll = QScrollArea()
        scroll.setWidget(self.groupscrollbartest)
        scroll.setWidgetResizable(True)
        scroll.setFixedWidth(20)
        scroll.setVerticalScrollBarPolicy(Qt.ScrollBarAsNeeded)
        scroll.setHorizontalScrollBarPolicy(Qt.ScrollBarAsNeeded) 
        self.groupscrollbartest.setLayout(self.mainHBOX_param_scene)
        self.groupscrolllayouttest.addWidget(self.groupscrollbartest)
        self.groupscrolllayouttest.addWidget(scroll)


        self.tab_ModelSelect.setLayout(self.groupscrolllayouttest)

def main():
    app = QApplication(sys.argv)
    ex = SurfViewer(app)
    ex.setWindowTitle('window')
    # ex.showMaximized()
    ex.show()
    sys.exit(app.exec_( ))


if __name__ == '__main__':
    main()

以下是结果:

enter image description here

在我更复杂的代码中,我使用了QTabWidget,这就是我在这个例子中包含它的原因。我想要做的是在底部有一个水平滚动条,允许我左右移动文本。显然我想让另一个人上下移动文本。

我还尝试将第二个滚动条添加到第一个滚动条(groupscrolllayouttest)

from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
import sys


class SurfViewer(QMainWindow):
    def __init__(self, parent=None):
        super(SurfViewer, self).__init__()
        self.parent = parent

        self.centralTabs= QTabWidget()
        self.setCentralWidget(self.centralTabs)
        self.setFixedWidth(200)
        self.setFixedHeight(200)

        #tab Model selection
        self.tab_ModelSelect = QWidget()
        self.centralTabs.addTab(self.tab_ModelSelect,"Label")


        self.groupscrolllayouttest2 = QVBoxLayout() ####
        self.groupscrollbartest2 = QGroupBox() ####

        self.groupscrolllayouttest = QHBoxLayout() ####
        self.groupscrollbartest = QGroupBox() ####


        self.mainHBOX_param_scene = QVBoxLayout()
        for i in range(10):
            Label = QLabel('BlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBlaBla')
            Label.setFixedWidth(200)
            self.mainHBOX_param_scene.addWidget(Label)


        #
        scroll = QScrollArea()
        scroll.setWidget(self.groupscrollbartest)
        scroll.setWidgetResizable(True)
        scroll.setFixedWidth(20)
        scroll.setVerticalScrollBarPolicy(Qt.ScrollBarAsNeeded)
        scroll.setHorizontalScrollBarPolicy(Qt.ScrollBarAsNeeded)
        # self.mainHBOX_param_scene.addWidget(scroll)
        self.groupscrollbartest.setLayout(self.mainHBOX_param_scene)
        self.groupscrolllayouttest.addWidget(self.groupscrollbartest)
        self.groupscrolllayouttest.addWidget(scroll)

        scroll2 = QScrollArea()
        scroll2.setWidget(self.groupscrollbartest2)
        scroll2.setWidgetResizable(True)
        scroll2.setFixedWidth(20)
        scroll2.setVerticalScrollBarPolicy(Qt.ScrollBarAsNeeded)
        scroll2.setHorizontalScrollBarPolicy(Qt.ScrollBarAsNeeded)
        self.groupscrollbartest2.setLayout(self.groupscrolllayouttest)
        self.groupscrolllayouttest2.addWidget(self.groupscrollbartest2)
        self.groupscrolllayouttest2.addWidget(scroll2)


        self.tab_ModelSelect.setLayout(self.groupscrolllayouttest2)

def main():
    app = QApplication(sys.argv)
    ex = SurfViewer(app)
    ex.setWindowTitle('window')
    # ex.showMaximized()
    ex.show()
    sys.exit(app.exec_( ))


if __name__ == '__main__':
    main()

但我最终得到了一个奇怪的滚动条:

enter image description here

所以现在我被卡住了。有什么想法吗?

1 个答案:

答案 0 :(得分:2)

您需要做的是创建一个小部件,并在该小部件中放置QGroupBox:

[...]
scroll = QScrollArea()
widget = QWidget(self)
widget.setLayout(QVBoxLayout())
widget.layout().addWidget(self.groupscrollbartest)
scroll.setWidget(widget)
scroll.setWidgetResizable(True)
self.groupscrollbartest.setLayout(self.mainHBOX_param_scene)
self.groupscrolllayouttest.addWidget(scroll)

self.tab_ModelSelect.setLayout(self.groupscrolllayouttest)
[...]

输出:

enter image description here