ymmx
ymmx

Reputation: 4967

How set two scroll bars (vertical and horizontal) to the same widget in PyQt environment?

I'm trying to make two scroll bars for a QGroupBox but I only succeed having one (the vertical one) I'm not sure what I need to do.

here is a short example of my code:

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

and here is the result:

enter image description here

In my more complex code I used QTabWidget, that why I included it in this example. What I would like to do is having a horizontal scrollbar to the bottom which allows me to shift the text left and right. Obviously I want keep the other one to shift the text up and down.

I also try to add a second scroll bar to the first one (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()

But I end up with a strange scroll bar:

enter image description here

So now I'm stuck. Any idea?

Upvotes: 2

Views: 2994

Answers (1)

eyllanesc
eyllanesc

Reputation: 243955

What you have to do is create a widget, and in that widget place the 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)
[...]

Output:

enter image description here

Upvotes: 2

Related Questions