QScrollArea 动态添加控件不显示的问题和其他一些坑

最近使用QT6写项目时遇到了一些问题,会导致 QScrollArea 上不能正常显示(或显示了看不到)添加的组件。

第一个,使用QScrollArea::setWidget函数时需要注意一些问题

看了一下 QScrollArea 的帮助文档,发现了QScrollArea::setWidget 函数的一些问题:

void QScrollArea::setWidget(QWidget *widget)

Sets the scroll area’s widget.

设置 QScrollArea 的子widget,子widget其实就是 QScrollArea 的幕布,我们添加的组件实际是添加在了这个子widget上。

The widget becomes a child of the scroll area, and will be destroyed when the scroll area is deleted or when a new widget is set.

这个子widget是 QScrollArea 的一部分,如果QScrollArea 被销毁或者 QScrollArea 设置了新的子widget,当前的子widget会被删除。

The widget’s autoFillBackground property will be set to true.

这个子widget的autoFillBackground会自动设置为true。

If the scroll area is visible when the widget is added, you must show() it explicitly.

如果添加子widget的时候, QScrollArea 已经是可见状态,则必须要显式调用子widget的show函数。

Note that You must add the layout of widget before you call this function; if you add it later, the widget will not be visible - regardless of when you show() the scroll area. In this case, you can also not show() the widget later.

重点来了:你必须在调用此函数之前完成子widget的布局的添加,不然的话,无论你什么时候show() 这个QScrollArea ,它的子widget都会不可见 ,并且你之后也不能show() 这个子widget。

第二个,使用 Designer 而非代码创建 QScrollArea 时的问题

QScrollArea 的 widgetResizable 属性默认值为 false,但当使用Designer 而非代码来创建 QScrollArea 时,QScrollArea 的 widgetResizable 属性自动变为了True,这在某些情况下并不是我们想要的,需要注意。

下面是这个属性的用处
widgetResizable : bool
This property holds whether the scroll area should resize the view widget
If this property is set to false (the default), the scroll area honors the size of its widget. Regardless of this property, you can programmatically resize the widget using widget()->resize(), and the scroll area will automatically adjust itself to the new size.
If this property is set to true, the scroll area will automatically resize the widget in order to avoid scroll bars where they can be avoided, or to take advantage of extra space.

此属性包含滚动区域是否应该调整子widget的大小,如果该属性被设置为false(默认值),滚动区域将遵循其子widget的大小。不管这个属性如何,您都可以使用widget()->resize()以编程方式调整子widget的大小,滚动区域将自动调整为新的大小。如果此属性设置为true,滚动区域将自动调整小部件的大小,以避免滚动条,或者利用额外的空间。

第三个,一些很奇怪的问题

在测试代码时有些细节问题也会导致添加的空间不显示(或显示了看不见)

1.当创建的子widget 不指定父窗口时,生成的尺寸正常(因为视作单独窗口)。
但当指定了父窗口(如this),生成的尺寸会很小。之后添加组件后,组件可能会小到看不到。
值得一提的是,当初始化代码全写在构造函数里时,并不会出现这个问题,猜测与上面所写 “Designer 创建 QScrollArea 时widgetResizable 属性为 true” 的问题有关。

构造函数(){
    this->grid_widget = new QWidget;    //不指定父窗口
    qDebug()<<this->grid_widget->size(); //QSize(640, 480) 
    this->gridLayout = new QGridLayout(this->grid_widget);
}
其他函数(){
    ui->scrollArea->setWidgetResizable(false);
    QPushButton * b = new QPushButton("123",this);
    b->setFixedSize(100,100);
    gridLayout->addWidget(b);
    ui->scrollArea->setWidget(this->grid_widget);
}

在这里插入图片描述

构造函数(){
    this->grid_widget = new QWidget(this);//指定父窗口
    qDebug()<<this->grid_widget->size(); //QSize(100, 30)
    this->gridLayout = new QGridLayout(this->grid_widget);
}
其他函数(){
    ui->scrollArea->setWidgetResizable(false);
    QPushButton * b = new QPushButton("123",this);
    b->setFixedSize(100,100);
    gridLayout->addWidget(b);
    ui->scrollArea->setWidget(this->grid_widget);
}

在这里插入图片描述

2. QScrollArea::setWidgetResizable 这个函数在某些情况下使用时,会出现与1类似的问题,同样会导致子widget的大小不符合我们的预期。
以下为 setWidgetResizable 具体内容:
当滚动区域的 widgetResizable 属性为False时,在Designer中或应用界面手工调整滚动区域部件的大小时,内容部署层不会跟随调整,但可以通过应用代码进行调整。

当滚动区域的 widgetResizable 属性为True时,在Designer中或应用界面手工调整滚动区域部件的大小时,内容部署层会跟随调整。

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值