I'm trying to make it so that my Window isn't completely occupied by a single QScrollArea.
Basically, I have a Scroll Area, and I want to add something next to it.
I've already seen it created in PySimpleGUI, so I'm sure it can also be created in PySide2, but I'm having trouble creating it. This is where I found the code for the PySimpleGUI. How can I create a column that is scrollable in PySimpleGUI
So far, I've tried to put the QScrollArea and the QVLayoutBox into a QHLayoutBox, with the addLayout() method, which I thought would allow me to put more widgets next to the QScrollArea, but this just gave me the following error.
self.scroll = QScrollArea()
self.hbox = QHBoxLayout()
self.widget = QWidget()
self.vbox = QVBoxLayout()
self.hbox.addLayout(self.vbox)
RuntimeError: QWidget::setLayout: Attempting to set QLayout "" on QWidget "", when the QLayout already has a parent
I've tried searching it on GitHub for some examples, but everything I found was just people expanding their QScrollArea, and not limiting the Scroll Area to a certain part of the window.
Here is a small piece of code I found online that shows the scroll area, with the code that produces my error added in.
from PySide2.QtWidgets import (QWidget, QSlider, QLineEdit, QLabel, QPushButton, QScrollArea,QApplication,
QHBoxLayout, QVBoxLayout, QMainWindow)
from PySide2.QtCore import Qt
from PySide2 import QtWidgets
import sys
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.hbox = QHBoxLayout() # remove this line for the code to work
self.scroll = QScrollArea()
self.widget = QWidget()
self.vbox = QVBoxLayout()
self.hbox.addLayout(self.vbox) # also this line
for i in range(1,50):
object = QLabel("TextLabel")
self.vbox.addWidget(object)
self.widget.setLayout(self.vbox)
self.scroll.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
self.scroll.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.scroll.setWidgetResizable(True)
self.scroll.setWidget(self.widget)
self.setCentralWidget(self.scroll)
self.setGeometry(600, 100, 1000, 900)
self.setWindowTitle('Scroll Area Demonstration')
self.show()
return
def main():
app = QtWidgets.QApplication(sys.argv)
main = MainWindow()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
Without the lines I added in, a scrollbox is created, however, it takes up the entire window, which isn't what I'm trying to create.