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;
}