0

I need to add info of students (it looks like a table) to the window. It's done in a function called "addStudentStats" and it's added to a main widget. I was trying to make another widget insert scrollbar, and this widget insert into a main one. But it didn't work. Then i tried to make the whole window scrollable, but i couldn't do it either. If anyone can can write those few lines of code (i think there are few, but don't know which) i would be pleased.

Read here

I've added a scrollbar to my window. The program fills it with data, which goes down the screen, but i cannot scroll

from PyQt5.QtWidgets import QPushButton, QWidget, QLabel,QScrollArea, QApplication, QMainWindow
from PyQt5.QtCore import Qt
import sys

class MainWindow(QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.uiMain = UIMainTeacher()
        self.setUpUI(self)

    def setUpUI(self, MainWindow):
        MainWindow.setGeometry(50, 50, 400, 450)
        MainWindow.setFixedSize(1800, 1000)
        MainWindow.setWindowTitle("Login")
        self.uiMain = UIMainTeacher()
        self.gotoTeacher()

    def gotoTeacher(self):
        self.uiMain.setupUI(self, type)
        self.show()


class UIMainTeacher(object):

    def setupUI(self, MainWindow, userId):
        MainWindow.setGeometry(50, 50, 400, 450)
        MainWindow.setFixedSize(1800, 1000)
        MainWindow.setWindowTitle("Main")

        self.mainWindow = MainWindow
        self.centralwid = QScrollArea(MainWindow)
        self.centralwid.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
        self.centralwidget = QWidget(self.centralwid)

        self.exitButton = QPushButton("Exit", self.centralwid)
        self.exitButton.move(1700, 0)
        self.CourseLabel = QLabel("No Kourse yet", self.centralwid)
        self.CourseLabel.move(900, 20)
        self.AddCourseButton = QPushButton('Add Course', self.centralwid)
        self.AddCourseButton.move(1700, 25)
        self.CourseLabel = QLabel("Course", self.centralwidget)
        self.CourseLabel.move(900, 20)
        self.AddStudentsButton = QPushButton("Add Students", self.centralwid)
        self.AddStudentsButton.move(1700, 100)

        self.taskLabel = QLabel("TASKS:", self.centralwidget)
        self.taskLabel.move(160, 20)
        self.AddTaskButton = QPushButton('Add Task', self.centralwid)
        self.AddTaskButton.move(1700, 50)

        self.centralwid.setWidget(self.centralwidget)

        MainWindow.setCentralWidget(self.centralwid)

        x, y = 600, 0
        for _ in range(200):
            label = QLabel("Test Test", self.centralwid)
            label.move(x, y)
            y += 50

if __name__ == '__main__':
    app = QApplication(sys.argv)
    w = MainWindow()
    sys.exit(app.exec_())
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Caus Deuoq
  • 13
  • 8
  • please provide a [mcve], in your case you have too much code, so take the time and eliminate the unnecessary. – eyllanesc Oct 25 '18 at 17:41
  • [https://stackoverflow.com/users/6622587/eyllanesc] UPDATED. Can you take a look on it? – Caus Deuoq Oct 25 '18 at 18:20
  • use `@eyllanesc` please – eyllanesc Oct 25 '18 at 18:21
  • on the other hand, is your code verifiable? Well, no, I want to copy it, paste it and execute it to verify your problem but I can not, is it complete ?, no, for the same reasons above, read the link so you understand what is important to provide a [mcve] – eyllanesc Oct 25 '18 at 18:23
  • @eyllanesc Did it. must work – Caus Deuoq Oct 25 '18 at 19:33
  • You could show an image of what you want to obtain, now I'm already correcting the code but that does not imply that it is what you want to obtain since it is not very clear which design you want. – eyllanesc Oct 25 '18 at 19:42

1 Answers1

1

The problem is simple QScrollArea is a container that serves to show another widget that has a large size, and that widget is what you pass through the setWidget() method. QScrollArea calculates the necessary size using the sizeHint() of the widget, so in your case the one that is caudding the problem is that widget since its sizeHint() is 0, 0. Why is it 0, 0? because it has nothing inside since the labels are children of the QScrollArea and not the widget, and even if it did not have the sizeHint() since there are at least 2 ways to calculate them: the layout gives you that information but in your case you use it, and the second is to set a fixed size to the widget, and that is the solution I have chosen, I do not know if it is what you want since I do not know what type of output you want, but you can see that the QScrollArea is visible.

class UIMainTeacher(object):
    def setupUI(self, MainWindow):
        MainWindow.setGeometry(50, 50, 400, 450)
        MainWindow.setFixedSize(1800, 1000)
        MainWindow.setWindowTitle("Main")

        self.mainWindow = MainWindow
        self.centralwid = QScrollArea(MainWindow)
        self.centralwid.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
        self.centralwidget = QWidget(self.centralwid)

        self.exitButton = QPushButton("Exit", self.centralwid)
        self.exitButton.move(1700, 0)
        self.CourseLabel = QLabel("No Kourse yet", self.centralwid)
        self.CourseLabel.move(900, 20)
        self.AddCourseButton = QPushButton('Add Course', self.centralwid)
        self.AddCourseButton.move(1700, 25)
        self.CourseLabel = QLabel("Course", self.centralwidget)
        self.CourseLabel.move(900, 20)
        self.AddStudentsButton = QPushButton("Add Students", self.centralwid)
        self.AddStudentsButton.move(1700, 100)

        self.taskLabel = QLabel("TASKS:", self.centralwidget)
        self.taskLabel.move(160, 20)
        self.AddTaskButton = QPushButton('Add Task', self.centralwid)
        self.AddTaskButton.move(1700, 50)

        self.centralwid.setWidget(self.centralwidget)
        MainWindow.setCentralWidget(self.centralwid)

        x, y = 600, 0
        for _ in range(200):
            label = QLabel("Test Test", self.centralwidget)
            label.move(x, y)
            y += 50
        self.centralwidget.setFixedWidth(800)
        self.centralwidget.setFixedHeight(y)

enter image description here

so if you are going to add more elements you will have to calculate and set the setFixedHeight(), another option is to use layouts but since I do not know what design you expect I will not be able to provide you with that second solution.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241