简体   繁体   中英

Qscrollbar in PyQt5 nothing is shown

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

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)

在此处输入图片说明

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM