Qt 手撸setWidget(QWidget* widget)

博客介绍了如何创建一个名为ContainerWidget的QWidget子类,以避免QScrollArea在setWidget时删除原有控件的问题。ContainerWidget允许设置和显示新的控件,并保持旧控件的可见性,适合仅用于显示的场景。文中提供了ContainerWidget的实现代码,包括设置和获取控件的方法,以及确保控件正确显示的注意事项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

        Qt中自带setWidget(QWidget* widget)方法的控件有 悬浮窗口、滚动窗口等,这些控件有时附加功能太多有时反而不够方便,比如滚动窗口的 setWidget(QWidget* widget) 会导致之前set的widget被删除:

void QScrollArea::setWidget(QWidget *widget)

Sets the scroll area's 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.

 不清楚这个特性的情况下使用 QScrollArea 会欲哭无泪(深恶痛绝)。

        由于不需要一些高级的容器功能,只做显示用,所以可以自己写一个带有setWidget(QWidget* widget)方法的QWidget,把它叫做ContainerWidget。根据QScrollArea和QDockWidget中关于setWidget(QWidget *widget)相同的描述:

1.Note that you must add the layout of the widget before you call this function; if not, the widget will not be visible.

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

2.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.

参考以上两个描述,可以先做如下约定

1. ContainerWidget要放在布局里

2.通过setWidget(QWidget* widget)放在ContainerWidget里的控件,要显式调用show()来显示控件。

具体实现见代码和注释,注意要把ContainerWidget放在布局里,另外ContainerWidget不能对输入的控件有new和delete的操作,因为它只是个展示的容器

ContainerWidget.h

#ifndef CONTAINERWIDGET_H
#define CONTAINERWIDGET_H

#include <QObject>
#include <QWidget>
#include <QDebug>
class ContainerWidget:public QWidget
{
    Q_OBJECT
public:
    ContainerWidget(QWidget *parent = nullptr);
    //设置当前容器内的控件
    void setWidget(QWidget* w);
    //返回当前容器内的控件
    QWidget* widget();
    //之前设置的控件
    QWidget* m_formerWidget = nullptr;
    //之前设置的控件的父控件
    QWidget* m_formerWidgetParent = nullptr;
};

#endif // CONTAINERWIDGET_H

ContainerWidget.cpp

#include "ContainerWidget.h"

ContainerWidget::ContainerWidget(QWidget *parent):QWidget(parent)
{
    this->setGeometry(0,0,600,750);
}

void ContainerWidget::setWidget(QWidget *widget)
{
    //判断容器内之前是否存在控件
    if(m_formerWidget!=nullptr){
        //把控件交还给其原来的父控件
        m_formerWidget->setParent(m_formerWidgetParent);
        //隐藏控件
        m_formerWidget->hide();
    }
    //获得当前控件的父控件
    m_formerWidgetParent = (QWidget*)widget->parent();
    m_formerWidget = widget;
    widget->setGeometry(0,0,this->width(),this->height());
    //把当前控件的父控件设置为this,以便显示和其他操作
    widget->setParent(this);
    //显示调用show()
    widget->show();
}

QWidget *ContainerWidget::widget()
{
    return m_formerWidget;
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值