生活随笔
收集整理的這篇文章主要介紹了
qt中创建控件布局以及删除原有布局和控件
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
引言
當根據數據來創建控件并布局時,如果數據更新,那么之前創建的控件便需要刪除后重新創建布局。該文主要說明將原來的布局和控件刪除,重新創建并布局。
示例
先看一下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();}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
);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;widget
->setFixedHeight(height
);
}
這里是tabwidget控件中創建控件并布局,先刪除之前的控件和布局,然后重新創建控件并布局。這里刪除的時候先獲取之前的布局,然后刪除布局,再刪除控件,控件刪除時是按照類型來刪除的,主要由其父窗口來看類型,找到某一類的控件,然后返回對應的列表,然后刪除,另外創建控件后根據控件的數量調整布局所在窗口的大小。
總結
以上是生活随笔為你收集整理的qt中创建控件布局以及删除原有布局和控件的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。