引言
当根据数据来创建控件并布局时,如果数据更新,那么之前创建的控件便需要删除后重新创建布局。该文主要说明将原来的布局和控件删除,重新创建并布局。
示例
先看一下ui文件:
下面是实现代码:
void StatusViewDialog::initSourcesView()
{deleteSourcesOrignalLayoutCtrls();QWidget *sourcesWidget = ui->scrollAreaWidgetContents;QVBoxLayout *vLayout = new QVBoxLayout(sourcesWidget);vLayout->setSpacing(5);vLayout->setContentsMargins(5,5,5,5);int page = 12;QLabel *curLabel = new QLabel(QStringLiteral("当前页:"));QLineEdit *curEdit = new QLineEdit(QString::number(page));curEdit->setReadOnly(true);curEdit->setFixedWidth(40);QLabel *totalLabel = new QLabel(QStringLiteral("总页数:"));QLineEdit *totalEdit = new QLineEdit(QString::number(page));totalEdit->setReadOnly(true);totalEdit->setFixedWidth(40);QHBoxLayout *smallLayouth = new QHBoxLayout;smallLayouth->addWidget(curLabel);smallLayouth->addWidget(curEdit);smallLayouth->addWidget(totalLabel);smallLayouth->addWidget(totalEdit);smallLayouth->addStretch();vLayout->addLayout(smallLayouth);//设置控件样式QString strLabelStyle = "QLabel{font-size: 17px;font-family: Microsoft YaHei;color: #FFFFFF;}";curLabel->setStyleSheet(strLabelStyle);totalLabel->setStyleSheet(strLabelStyle);QString strEditStyle = "QLineEdit{font-size: 17px;font-family: Microsoft YaHei;color: #FFFFFF;""border: 1px solid #01B5EF;border-radius: 2px;background: rgba(8, 7, 91, 0.08);}";curEdit->setStyleSheet(strEditStyle);totalEdit->setStyleSheet(strEditStyle);QList<QHBoxLayout*> hlayoutList;for (int i = 0 ; i < 3 ; ++i) {QLabel * inputLabel = new QLabel(QStringLiteral("幼儿园:"));QLineEdit *inputEdit = new QLineEdit(QStringLiteral("小孩子"));inputEdit->setReadOnly(true);inputEdit->setFixedWidth(150);QLabel *onlineLabel = new QLabel(QStringLiteral("是小孩子:"));QString boolStr = "true";QLineEdit *onlineEdit = new QLineEdit(boolStr);onlineEdit->setReadOnly(true);onlineEdit->setFixedWidth(80);QLabel *stateLabel = new QLabel(QStringLiteral("能独立:"));boolStr = "false";QLineEdit *stateEdit = new QLineEdit(boolStr);stateEdit->setReadOnly(true);stateEdit->setFixedWidth(80);QLabel *modeLabel = new QLabel(QStringLiteral("可爱不:"));QLineEdit *modeEdit = new QLineEdit(QStringLiteral("可爱"));modeEdit->setReadOnly(true);modeEdit->setFixedWidth(180);QLabel *nameLabel = new QLabel(QStringLiteral("天才:"));QLineEdit *nameEdit = new QLineEdit(QStringLiteral("天才"));nameEdit->setReadOnly(true);nameEdit->setFixedWidth(200);QLabel *tempretrueLabel = new QLabel(QStringLiteral("温度:"));qreal tempretrue = 12.12;QLineEdit *tempretrueEdit = new QLineEdit(QString::number(tempretrue));tempretrueEdit->setReadOnly(true);tempretrueEdit->setFixedWidth(50);inputLabel->setStyleSheet(strLabelStyle);onlineLabel->setStyleSheet(strLabelStyle);stateLabel->setStyleSheet(strLabelStyle);modeLabel->setStyleSheet(strLabelStyle);nameLabel->setStyleSheet(strLabelStyle);tempretrueLabel->setStyleSheet(strLabelStyle);inputEdit->setStyleSheet(strEditStyle);onlineEdit->setStyleSheet(strEditStyle);stateEdit->setStyleSheet(strEditStyle);modeEdit->setStyleSheet(strEditStyle);nameEdit->setStyleSheet(strEditStyle);tempretrueEdit->setStyleSheet(strEditStyle);QHBoxLayout *hLayout = new QHBoxLayout;hLayout->addWidget(inputLabel);hLayout->addWidget(inputEdit);hLayout->addWidget(onlineLabel);hLayout->addWidget(onlineEdit);hLayout->addWidget(stateLabel);hLayout->addWidget(stateEdit);hLayout->addWidget(modeLabel);hLayout->addWidget(modeEdit);hLayout->addWidget(nameLabel);hLayout->addWidget(nameEdit);hLayout->addWidget(tempretrueLabel);hLayout->addWidget(tempretrueEdit);vLayout->addLayout(hLayout);}adjustScrollWidgetHeight(3,ui->scrollAreaWidgetContents);
}void StatusViewDialog::initSeatsView()
{deleteSeatsOrignalLayoutCtrls();int row = 0;QWidget *widget = ui->scrollAreaWidgetContents_2;QVBoxLayout *vlayout = new QVBoxLayout(widget);vlayout->setSpacing(5);vlayout->setContentsMargins(5,5,5,5);QList<QHBoxLayout*> everyHlayoutList;for (int i = 0 ; i < 4 ; ++i) {QLabel *curLabel = new QLabel(QStringLiteral("当前页:"));int page = 11;QLineEdit *curEdit = new QLineEdit(QString::number(page));curEdit->setReadOnly(true);curEdit->setFixedWidth(40);QLabel *totalLabel = new QLabel(QStringLiteral("总页数:"));QLineEdit *totalEdit = new QLineEdit(QString::number(page));totalEdit->setReadOnly(true);totalEdit->setFixedWidth(40);QLabel *unidNameLabel = new QLabel(QStringLiteral("名称:"));QLineEdit *unidNameEdit = new QLineEdit(QStringLiteral("席位"));unidNameEdit->setReadOnly(true);unidNameEdit->setFixedWidth(130);QLabel *unisIdLabel = new QLabel(QStringLiteral("大小:"));QLineEdit *unisIdEdit = new QLineEdit(QStringLiteral("23"));unisIdEdit->setReadOnly(true);unisIdEdit->setFixedWidth(300);QLabel *userNameLabel = new QLabel(QStringLiteral("用户名:"));QLineEdit *userNameEdit = new QLineEdit(QStringLiteral("深度"));userNameEdit->setReadOnly(true);userNameEdit->setFixedWidth(150);QString strLabelStyle = "QLabel{font-size: 17px;font-family: Microsoft YaHei;color: #FFFFFF;}";QString strEditStyle = "QLineEdit{font-size: 17px;font-family: Microsoft YaHei;color: #FFFFFF;""border: 1px solid #01B5EF;border-radius: 2px;background: rgba(8, 7, 91, 0.08);}";curLabel->setStyleSheet(strLabelStyle);totalLabel->setStyleSheet(strLabelStyle);unidNameLabel->setStyleSheet(strLabelStyle);unisIdLabel->setStyleSheet(strLabelStyle);userNameLabel->setStyleSheet(strLabelStyle);curEdit->setStyleSheet(strEditStyle);totalEdit->setStyleSheet(strEditStyle);unidNameEdit->setStyleSheet(strEditStyle);unisIdEdit->setStyleSheet(strEditStyle);userNameEdit->setStyleSheet(strEditStyle);QHBoxLayout *hlayout1 = new QHBoxLayout;hlayout1->addWidget(curLabel);hlayout1->addWidget(curEdit);hlayout1->addWidget(totalLabel);hlayout1->addWidget(totalEdit);hlayout1->addWidget(unidNameLabel);hlayout1->addWidget(unidNameEdit);hlayout1->addWidget(unisIdLabel);hlayout1->addWidget(unisIdEdit);hlayout1->addWidget(userNameLabel);hlayout1->addWidget(userNameEdit);everyHlayoutList.append(hlayout1);++row;QString strGroupStyle = "QGroupBox{border: 1px solid #01B5EF;border-radius:2px;margin-top: 0.5px;}""QGroupBox::title{subcontrol-origin: margin;subcontrol-position: top left;""left: 10px;margin-left: 2px;padding: 0 0px;}";QGroupBox *groupBox = new QGroupBox;groupBox->setStyleSheet(strGroupStyle);QList<QHBoxLayout*> hlayoutList;for (int j = 0 ; j < 3; ++j) {QHBoxLayout *layoutStatus = new QHBoxLayout;hlayoutList.append(layoutStatus);QLabel *ipLabel = new QLabel(QStringLiteral("IP:"));QLineEdit *ipEdit = new QLineEdit(QStringLiteral("129.128.0.1"));ipEdit->setReadOnly(true);QLabel *tempretureLabel = new QLabel(QStringLiteral("温度:"));QLineEdit *tempretureEdit = new QLineEdit(QStringLiteral("24"));tempretureEdit->setReadOnly(true);QLabel *onlineLabel = new QLabel(QStringLiteral("是否在线:"));QString temp = "true";QLineEdit *onlineEdit = new QLineEdit(temp);onlineEdit->setReadOnly(true);ipLabel->setStyleSheet(strLabelStyle);tempretureLabel->setStyleSheet(strLabelStyle);onlineLabel->setStyleSheet(strLabelStyle);ipEdit->setStyleSheet(strEditStyle);tempretureEdit->setStyleSheet(strEditStyle);onlineEdit->setStyleSheet(strEditStyle);layoutStatus->addWidget(ipLabel);layoutStatus->addWidget(ipEdit);layoutStatus->addWidget(tempretureLabel);layoutStatus->addWidget(tempretureEdit);layoutStatus->addWidget(onlineLabel);layoutStatus->addWidget(onlineEdit);row++;}QVBoxLayout *vGroupboxLayout = new QVBoxLayout(groupBox);for (int i = 0 ; i < hlayoutList.size() ; ++i) {vGroupboxLayout->addLayout(hlayoutList[i]);}QHBoxLayout *hGroupLayout = new QHBoxLayout;hGroupLayout->addWidget(groupBox);everyHlayoutList.append(hGroupLayout);}for (int j = 0 ; j < everyHlayoutList.size() ; ++j) {vlayout->addLayout(everyHlayoutList[j]);}adjustScrollWidgetHeight(row,ui->scrollAreaWidgetContents_2);
}void StatusViewDialog::deleteSourcesOrignalLayoutCtrls()
{if (ui->scrollAreaWidgetContents->layout() != nullptr) {QLayout *oldLayout = ui->scrollAreaWidgetContents->layout();delete oldLayout;}QList<QWidget*> widgetList = ui->scrollAreaWidgetContents->findChildren<QWidget*>();foreach (QWidget* ctrl, widgetList) {delete ctrl;}
}void StatusViewDialog::deleteSeatsOrignalLayoutCtrls()
{if (ui->scrollAreaWidgetContents_2->layout() != nullptr) {delete ui->scrollAreaWidgetContents_2->layout();}//现删除groupbox,然后删除窗口上剩余的控件QList<QGroupBox*> groupBox = ui->scrollAreaWidgetContents_2->findChildren<QGroupBox*>();foreach (QGroupBox *gb, groupBox) {delete gb;}//删除其它的标签和行编辑器QList<QWidget*> widgetList = ui->scrollAreaWidgetContents_2->findChildren<QWidget*>();foreach (QWidget* ctrl, widgetList) {delete ctrl;}
}void StatusViewDialog::adjustScrollWidgetSize(QWidget *widget,QWidget *changedWidget)
{QGridLayout *gridLayout = new QGridLayout(widget);//使tab页中的窗口和tab页一样大gridLayout->setContentsMargins(0,0,0,0);gridLayout->addWidget(changedWidget,0,0,1,1);
}void StatusViewDialog::adjustScrollWidgetHeight(int num,QWidget *widget)
{int height = (num + 1) * 35;//平均每行的高度为35widget->setFixedHeight(height);
}
这里是tabwidget控件中创建控件并布局,先删除之前的控件和布局,然后重新创建控件并布局。这里删除的时候先获取之前的布局,然后删除布局,再删除控件,控件删除时是按照类型来删除的,主要由其父窗口来看类型,找到某一类的控件,然后返回对应的列表,然后删除,另外创建控件后根据控件的数量调整布局所在窗口的大小。