Search code examples
pythonpython-3.xpyqtpyqt5qscrollarea

How to add Scroll bar on the QMainwindow


I display a window where more than 30 Checkbox(on the basic of results in database), This code is static it creates Checkbox but above the window size doesn't show, i want to add scroll to show all Checkbox and select which user want, how can i do that? [It is the second window after we passing and selecting some fields from first window, self.main_query is the query which is selected from first page by user]

import sys
from PyQt5.QtWidgets import QScrollBar, QSlider, QMainWindow, QApplication, QPushButton, QWidget, QAction, QTabWidget, QVBoxLayout, QLabel, QCheckBox
from PyQt5 import QtGui
from PyQt5.QtCore import pyqtSlot
import pymysql

class FormTwo(QWidget):
    def __init__(self):
        super().__init__()
        self.title = 'Second Page which Showing Analytics'
        self.title = 'First Main  Window'
        self.left = 0
        self.top = 0
        self.width = 700
        self.height = 600
        self.main_query = " select data from database where "
        self.initform2ui()
        self.show()

    def initform2ui(self):
        conn = pymysql.connect(host = 'localhost', user = 'root', password = '********', db = 'db_name')
        cur4 = conn.cursor()
        query_for_analytics = "select distinct Analytics from analyticsreport"
        cur4.execute(query_for_analytics)
        self.no_of_analytics = cur4.rowcount
        result = cur4.fetchall()
        checkbox = 'checkbox3'

        r_move = 95
        c_move = 75
        myFont = QtGui.QFont()
        myFont.setBold(True)
        self.label2 = QLabel('Analytics', self)
        self.label2.setFont(myFont)
        self.label2.setStyleSheet('QLabel {Color:blue}')
        self.label2.move(100, 50)
        self.layout = QVBoxLayout()
        #self.layout.addWidget(self.tableWidget)
        self.s1 = QSlider()

        self.setLayout(self.layout)
        self.setWindowTitle('Proceed for the result of Analytics')
        self.setGeometry(self.top, self.left, self.width, self.height)

        self.button1 = QPushButton('Proceed For Result', self)
        self.button1.setStyleSheet('background-color:darkblue; color: white')
        self.button1.move(140,300)
        self.button1.clicked.connect(self.on_button_pushed)
        self.list_of_checkbox_for_analytics = []
        for i in range(self.no_of_analytics):
            name = str(list(result[i]))
            print("name", name)
            name = name.replace("[", "")
            name = name.replace("]", "")
            name = name.replace("'", "")
            cb1 = checkbox + str(i)
            self.list_of_checkbox_for_analytics.append(name)
            self.list_of_checkbox_for_analytics[i] = QCheckBox(name, self)
            self.list_of_checkbox_for_analytics[i].adjustSize()
            self.list_of_checkbox_for_analytics[i].move(r_move, c_move)
            c_move = c_move + 20

    def on_button_pushed(self):
        initialize_ai = 0
        flag = 0
        ana_query = ''
        for i in range(self.no_of_analytics):
            if self.list_of_checkbox_for_analytics[i].isChecked():
                print("Checked", self.list_of_checkbox_for_analytics[i].text())
                flag = 1
            if initialize_ai == 0 and flag == 1:
                ana_query = " '" + self.list_of_checkbox_for_analytics[i].text() + "' "
                initialize_ai = initialize_ai + 1
                flag = 0
            if initialize_ai > 0 and flag == 1:
                ana_query = ana_query + " or '" + self.list_of_checkbox_for_analytics[i].text() + "' "
                flag = 0
        if len(ana_query)>2:
            ana_query = " and (Analytics = " + ana_query + ")"
            main_query = self.main_query + ana_query
        else:
            main_query = self.main_query
        print(main_query)
        self.window = QMainWindow()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = FormTwo()
    sys.exit(app.exec_())

Solution

  • You have to use a QScrollArea, on the other hand to handle the positions in these cases it is advisable to use layouts.

    import sys
    from PyQt5 import QtCore, QtGui, QtWidgets
    import pymysql
    
    class FormTwo(QtWidgets.QWidget):
        def __init__(self):
            super().__init__()
            self.title = 'Second Page which Showing Analytics'
            self.left, self.top, self.width, self.height = 0, 0, 700, 600
            self.main_query = " select data from database where "
            self.initform2ui()
            self.show()
    
        def initform2ui(self):
            self.setWindowTitle('Proceed for the result of Analytics')
            self.setGeometry(self.top, self.left, self.width, self.height)
    
            myFont = QtGui.QFont()
            myFont.setBold(True)
            self.label2 = QtWidgets.QLabel('Analytics')
            self.label2.setFont(myFont)
            self.label2.setStyleSheet('QLabel {Color:blue}')
    
            self.button1 = QtWidgets.QPushButton('Proceed For Result')
            self.button1.setStyleSheet('background-color:darkblue; color: white')
    
            self.button1.clicked.connect(self.on_button_pushed)
    
            self.list_of_checkbox_for_analytics = []
    
            scrollArea = QtWidgets.QScrollArea()
            content_widget = QtWidgets.QWidget()
            scrollArea.setWidget(content_widget)
            scrollArea.setWidgetResizable(True)
            lay = QtWidgets.QVBoxLayout(content_widget)
    
            conn = pymysql.connect(host = 'localhost', user = 'root', password = '********', db = 'db_name')
            cur = conn.cursor()
            query_for_analytics = "select distinct Analytics from analyticsreport"
            cur.execute(query_for_analytics)
            for row in cur.fetchall():
                name = str(list(row))
                name = name.replace("[", "").replace("]", "").replace("'", "")
                checkbox = QtWidgets.QCheckBox(name)
                checkbox.adjustSize()
                lay.addWidget(checkbox)
                self.list_of_checkbox_for_analytics.append(checkbox)
    
            layout = QtWidgets.QVBoxLayout(self)
            layout.addWidget(self.label2)
            layout.addWidget(self.button1)
            layout.addWidget(scrollArea)
    
        @QtCore.pyqtSlot()
        def on_button_pushed(self):
            initialize_ai = 0
            flag = False
            ana_query = ''
            for checkbox in self.list_of_checkbox_for_analytics:
                if checkbox.isChecked():
                    print("Checked", checkbox.text())
                    flag = True
                if initialize_ai == 0 and flag:
                    ana_query = " '" + checkbox.text() + "' "
                    initialize_ai += 1
                    flag = False
                if initialize_ai > 0 and flag:
                    ana_query += " or '" + checkbox.text() + "' "
                    flag = False
            main_query = self.main_query
            if len(ana_query) > 2:
                ana_query = " and (Analytics = " + ana_query + ")"
                main_query += ana_query
            print(main_query)
    
    if __name__ == '__main__':
        app = QtWidgets.QApplication(sys.argv)
        ex = FormTwo()
        sys.exit(app.exec_())