我想在一个QscrollArea中创建一堆QcheckBox,并且我需要修改QGridLayeCheckbox的数量.

我的问题是当我增加复选框的数量时,它们被压缩在QscrollArea中,而不是在右侧创建scrollBar.

下面是我的简单例子:

import sys
import numpy as np
from PyQt6.QtGui import *
from PyQt6.QtCore import *
from PyQt6.QtWidgets import *


class MainWindow(QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__()
        self.centralWidget = QWidget()
        self.setCentralWidget(self.centralWidget)
        self.mainHBOX = QVBoxLayout()

        layout_toApply = QGroupBox('Checkboxes')
        self.layout_toApply_V = QVBoxLayout()
        self.grid_toApply_V = QGridLayout()
        self.Stimulation_NbSources_Edit =QLineEdit('100')
        self.Stimulation_NbSources_PB =QPushButton('Apply')
        self.ChangeNBSource_fun()
        layout_toApply.setLayout(self.layout_toApply_V)
        self.layout_toApply_V.addLayout(self.grid_toApply_V)

        self.layout_toApply_V.addWidget(self.Stimulation_NbSources_Edit)
        self.layout_toApply_V.addWidget(self.Stimulation_NbSources_PB)

        self.mainHBOX.addWidget(layout_toApply)

        self.mainHBOX_W = QWidget()
        self.mainHBOX_W.setLayout(self.mainHBOX)


        self.widgetparam_w = QWidget()
        layout_toApplyparam_w = QHBoxLayout()
        self.widgetparam_w.setLayout(layout_toApplyparam_w)
        self.scrolltoparam_w = QScrollArea(self.widgetparam_w)
        self.scrolltoparam_w.setWidget(self.mainHBOX_W)

        self.mainlay = QHBoxLayout()
        self.mainlay.addWidget(self.scrolltoparam_w)



        self.centralWidget.setLayout(self.mainlay)
        self.centralWidget.setFixedSize(QSize(500,500))
        self.Stimulation_NbSources_PB.clicked.connect(self.ChangeNBSource_fun)


    def ChangeNBSource_fun(self):
        for i in reversed(range(self.grid_toApply_V.count())):
            widgetToRemove = self.grid_toApply_V.itemAt(i).widget()
            widgetToRemove.setParent(None)
            widgetToRemove.deleteLater()


        N = int(self.Stimulation_NbSources_Edit.text())
        nb_column = 10
        nb_line = int(np.ceil(N / nb_column))


        self.Apply_to_pop = []
        for l in np.arange(nb_line):
            for c in np.arange(nb_column):
                idx = (l) * nb_column + c + 1
                if idx <= N:
                    CB = QCheckBox(str(idx - 1))
                    CB.setFixedWidth(int(30))
                    CB.setChecked(False)
                    self.grid_toApply_V.addWidget(CB, l, c)
                    self.Apply_to_pop.append(CB)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec())

下面是我启动代码的时候:

enter image description here

And here's what I get when I double the number of Check Boxes: enter image description here

推荐答案

QCheckBox个小部件被压缩,因为QScrollArea的小部件没有一个可以扩展到QScrollArea可见边界之外的布局.

您应该在小部件上设置网格布局,然后将其设置为QScrollArea的子级.确保QScrollAreaQSizePolicy.Expanding允许它扩展.

同时,设定QScrollAreaQScrollArea.SetWidgetResizable(True)的最优策略,

import sys
import numpy as np
from PyQt6.QtGui import *
from PyQt6.QtCore import QSize
from PyQt6.QtWidgets import (
    QApplication, QMainWindow, QWidget, QVBoxLayout, QHBoxLayout,
    QGridLayout, QGroupBox, QLineEdit, QPushButton, QCheckBox, QScrollArea
)
class MainWindow(QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__()
        self.centralWidget = QWidget()
        self.setCentralWidget(self.centralWidget)
        self.mainHBOX = QVBoxLayout()
        layout_toApply = QGroupBox('Checkboxes')
        self.layout_toApply_V = QVBoxLayout()
        self.grid_toApply_V = QGridLayout()
        self.Stimulation_NbSources_Edit = QLineEdit('100')
        self.Stimulation_NbSources_PB = QPushButton('Apply')
        self.scrollContent = QWidget()
        self.scrollContent.setLayout(self.grid_toApply_V)
        self.scrollArea = QScrollArea()
        self.scrollArea.setWidgetResizable(True)
        self.scrollArea.setWidget(self.scrollContent)
        layout_toApply.setLayout(self.layout_toApply_V)
        self.layout_toApply_V.addWidget(self.scrollArea)
        self.layout_toApply_V.addWidget(self.Stimulation_NbSources_Edit)
        self.layout_toApply_V.addWidget(self.Stimulation_NbSources_PB)
        self.mainHBOX.addWidget(layout_toApply)
        self.centralWidget.setLayout(self.mainHBOX)
        self.centralWidget.setFixedSize(QSize(500, 500))   
        self.Stimulation_NbSources_PB.clicked.connect(self.ChangeNBSource_fun)
        self.ChangeNBSource_fun()
    def ChangeNBSource_fun(self):
        for i in reversed(range(self.grid_toApply_V.count())):
            widgetToRemove = self.grid_toApply_V.itemAt(i).widget()
            if widgetToRemove:
                widgetToRemove.setParent(None)
                widgetToRemove.deleteLater()
        N = int(self.Stimulation_NbSources_Edit.text())
        nb_column = 10
        nb_line = int(np.ceil(N / nb_column))
        self.Apply_to_pop = []
        for l in range(nb_line):
            for c in range(nb_column):
                idx = l * nb_column + c + 1
                if idx <= N:
                    CB = QCheckBox(str(idx - 1))
                    CB.setFixedWidth(30)
                    CB.setChecked(False)
                    self.grid_toApply_V.addWidget(CB, l, c)
                    self.Apply_to_pop.append(CB)
        self.scrollContent.setMinimumSize(nb_column * 30, nb_line * 30)
if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec())

correct

Python相关问答推荐

Polars比较了两个预设-有没有方法在第一次不匹配时立即失败

DataFrame groupby函数从列返回数组而不是值

如何将ctyles.POINTER(ctyles.c_float)转换为int?

如何使用LangChain和AzureOpenAI在Python中解决AttribeHelp和BadPressMessage错误?

如何列举Pandigital Prime Set

Polars:用氨纶的其他部分替换氨纶的部分

ThreadPoolExecutor和单个线程的超时

移动条情节旁边的半小提琴情节在海运

Scrapy和Great Expectations(great_expectations)—不合作

解决调用嵌入式函数的XSLT中表达式的语法移位/归约冲突

从Windows Python脚本在WSL上运行Linux应用程序

Python Pandas—时间序列—时间戳缺失时间精确在00:00

在Admin中显示从ManyToMany通过模型的筛选结果

使用Openpyxl从Excel中的折线图更改图表样式

将链中的矩阵乘法应用于多组值

如何在Gekko中处理跨矢量优化

高效生成累积式三角矩阵

如何提高Pandas DataFrame中随机列 Select 和分配的效率?

将数字数组添加到Pandas DataFrame的单元格依赖于初始化

为什么在安装了64位Python的64位Windows 10上以32位运行?