PyQt动态添加自定义控件QScrollArea + QGridLayout

 

功能:

1.自定义按钮

2.动态显示


UI布局设计 QScrollArea + QGridLayout

PyQt动态添加自定义控件QScrollArea + QGridLayout_第1张图片

最后实现效果

PyQt动态添加自定义控件QScrollArea + QGridLayout_第2张图片

源码

button

#!/usr/bin/env python3.6
# -*- coding:utf-8 -*-
from PyQt5.QtWidgets import QPushButton

class IOControlBtn(QPushButton):
    def __init__(self, parent=None, text="", width = 130, hight=30):
        super(IOControlBtn, self).__init__(parent)
        self.setText(text)
        self.io_open_style_sheet = """
                                   background-color: rgb(112, 182, 3);
                                   border: 1px solid;
                                   border-color: rgb(2, 139, 157);
                                   """
        self.io_close_style_sheet = """
                                    background-color: rgb(170, 170, 170);
                                    border: 1px solid;
                                    border-color: rgb(2, 139, 157);
                                    """
        self.setStyleSheet(self.io_close_style_sheet)
        self.setFixedSize(width, hight)

    def setIsOpen(self, is_open):
        if is_open:
            self.setStyleSheet(self.io_open_style_sheet)
        else:
            self.setStyleSheet(self.io_close_style_sheet)


ui

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'UI_mainWin.ui'
#
# Created by: PyQt5 UI code generator 5.14.1
#
# WARNING! All changes made in this file will be lost!


from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 493)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget)
        self.verticalLayout.setObjectName("verticalLayout")
        self.groupBox = QtWidgets.QGroupBox(self.centralwidget)
        self.groupBox.setMinimumSize(QtCore.QSize(0, 71))
        self.groupBox.setObjectName("groupBox")
        self.widget = QtWidgets.QWidget(self.groupBox)
        self.widget.setGeometry(QtCore.QRect(40, 30, 701, 28))
        self.widget.setObjectName("widget")
        self.horizontalLayout_3 = QtWidgets.QHBoxLayout(self.widget)
        self.horizontalLayout_3.setContentsMargins(0, 0, 0, 0)
        self.horizontalLayout_3.setObjectName("horizontalLayout_3")
        self.label = QtWidgets.QLabel(self.widget)
        self.label.setObjectName("label")
        self.horizontalLayout_3.addWidget(self.label)
        self.spinBox_num = QtWidgets.QSpinBox(self.widget)
        self.spinBox_num.setObjectName("spinBox_num")
        self.horizontalLayout_3.addWidget(self.spinBox_num)
        self.label_2 = QtWidgets.QLabel(self.widget)
        self.label_2.setObjectName("label_2")
        self.horizontalLayout_3.addWidget(self.label_2)
        self.spinBox_max_col = QtWidgets.QSpinBox(self.widget)
        self.spinBox_max_col.setObjectName("spinBox_max_col")
        self.horizontalLayout_3.addWidget(self.spinBox_max_col)
        self.pushButton_set = QtWidgets.QPushButton(self.widget)
        self.pushButton_set.setObjectName("pushButton_set")
        self.horizontalLayout_3.addWidget(self.pushButton_set)
        self.verticalLayout.addWidget(self.groupBox)
        self.groupBox_2 = QtWidgets.QGroupBox(self.centralwidget)
        self.groupBox_2.setObjectName("groupBox_2")
        self.horizontalLayout_2 = QtWidgets.QHBoxLayout(self.groupBox_2)
        self.horizontalLayout_2.setObjectName("horizontalLayout_2")
        self.scrollArea = QtWidgets.QScrollArea(self.groupBox_2)
        self.scrollArea.setWidgetResizable(True)
        self.scrollArea.setObjectName("scrollArea")
        self.scrollAreaWidgetContents = QtWidgets.QWidget()
        self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, 0, 756, 314))
        self.scrollAreaWidgetContents.setObjectName("scrollAreaWidgetContents")
        self.horizontalLayout = QtWidgets.QHBoxLayout(self.scrollAreaWidgetContents)
        self.horizontalLayout.setObjectName("horizontalLayout")
        self.ioGridLayout = QtWidgets.QGridLayout()
        self.ioGridLayout.setObjectName("ioGridLayout")
        self.horizontalLayout.addLayout(self.ioGridLayout)
        self.scrollArea.setWidget(self.scrollAreaWidgetContents)
        self.horizontalLayout_2.addWidget(self.scrollArea)
        self.verticalLayout.addWidget(self.groupBox_2)
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 19))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "动态添加按钮"))
        self.groupBox.setTitle(_translate("MainWindow", "设置button数量"))
        self.label.setText(_translate("MainWindow", "个数"))
        self.label_2.setText(_translate("MainWindow", "最大列数"))
        self.pushButton_set.setText(_translate("MainWindow", "设置"))
        self.groupBox_2.setTitle(_translate("MainWindow", "动态添加按钮"))

src

from PyQt5.QtWidgets import QWidget, QMainWindow
from demo.demo1.buttons import IOControlBtn
from demo.demo1.UI_mainWin import Ui_MainWindow

class MainWindows(Ui_MainWindow, QMainWindow):
    def __init__(self, parent=None):
        super(MainWindows, self).__init__(parent)
        self.setupUi(self)
        self.max_io_display_cols = 6
        self.max_io_num = 15
        self._initWidget()
        self._initConnect()

    def _initWidget(self):
        self.spinBox_max_col.setValue(self.max_io_display_cols)
        self.spinBox_num.setValue(self.max_io_num)

    def _initConnect(self):
        self.pushButton_set.clicked.connect(self.onDisplayButtons)

    def onDisplayButtons(self):
        self.max_io_display_cols = self.spinBox_max_col.value()
        self.max_io_num = self.spinBox_num.value()
        print(self.max_io_display_cols, self.max_io_num)
        self.addButtons()

    def addButtons(self):
        self.removeButtons()
        for i in range(self.max_io_num):
            io_control_btn = IOControlBtn(self, "IO_{}".format(i))
            self.ioGridLayout.addWidget(
                io_control_btn,
                i / self.max_io_display_cols,
                i % self.max_io_display_cols + 1, 1,1)

    def removeButtons(self):
        while self.ioGridLayout.count():
            item = self.ioGridLayout.takeAt(0)
            widget = item.widget()
            widget.deleteLater()

if __name__ == '__main__':
    import sys
    from PyQt5.QtWidgets import QApplication
    app = QApplication(sys.argv)
    window = MainWindows()
    window.show()
    sys.exit(app.exec_())

交流群号:245022761(IT项目交流群)

 

你可能感兴趣的:(QT学习之路,PyQt,动态添加)