刚开始用Qt布局管理的时候,发现界面管理没有先前认为的那样简单了,布局中控件的对齐、大小、间距等,都需要在代码中设置。但是当你用熟了这些设置,Qt的界面管理就变成一件轻松愉快的事情了。。。
先看代码:
RsData::RsData(QWidget *parent,Qt::WindowFlags fl)
:QWidget(parent,fl)
{ setStyleSheet( QString("QLineEdit {border: 2px solid gray;border-radius: 5px;padding: 0 8px;background: yellow;selection-background-color: darkgray;max-width: 120px;}")+QString ( "QComboBox{max-width: 120px}" )+
QString("QListView {alternate-background-color: yellow;background-color:rgb(85,123,205)}")+
QString("QPushButton{background-color:rgb(85,123,205);}")+
//QString("QLabel{background-color:rgb(85,123,205)}")+
//QString("QFrame, QLabel, QToolTip {border: 2px solid green;border-radius: 4px;padding: 2px;background-image: url(images/welcome.png);}")+
QString(""));//设置各部件样式
nodeListLabel=new QLabel(tr("节点选择"));
sensorListLabel=new QLabel(tr("传感器选择"));
nodeComboBox=new QComboBox;
sensorComboBox=new QComboBox;
tempeLabel=new QLabel(tr("温度"));
tempeUpLabel=new QLabel(tr("温度上限"));
tempeDownLabel=new QLabel(tr("温度下限")); 。。。。
tempeLEdit=new QLineEdit;
tempeUpLEdit=new QLineEdit;
tempeDownLEdit=new QLineEdit; 。。。。//省去了定义部分
;
//QSplitter *splitter = new QSplitter( Qt::Vertical, 0);
QHBoxLayout *hLayout=new QHBoxLayout;
QGridLayout *gridLayout=new QGridLayout;
//splitter->addWidget(hLayout);
//splitter->setLayout(gridLayout);
//addWidget(QWidget*,int row,int column,int rowStretch,int columnStretch,Qt::Alignment),
//row表示行,column表示列,rowStretch表示行方向占据的宽度,columnStretch表示列方向占的宽度,Qt::Alignment表示对齐方式,默认为0
gridLayout->addWidget(nodeListLabel,0,0,1,1);
gridLayout->addWidget(nodeComboBox,0,1,1,1);
gridLayout->addWidget(sensorListLabel,0,2,1,1);
gridLayout->addWidget(sensorComboBox,0,3,1,1);
//gridLayout->addLayout(hLayout,0,0,1,4);
gridLayout->addWidget(tempeLabel,1,0,1,1);
gridLayout->addWidget(tempeLEdit,1,1,1,1);
gridLayout->addWidget(tempeUpLabel,1,2,1,1);
gridLayout->addWidget(tempeUpLEdit,1,3,1,1);
gridLayout->addWidget(tempeDownLabel,1,4,1,1);
gridLayout->addWidget(tempeDownLEdit,1,5,1,1);
gridLayout->addWidget(dynamic_cast(new QSpacerItem(50,50)),1,6,1,3);//添加一个空白项,使布局显示更合理,可根据具体情况灵活应用 //上面的一行是我一分钟前写的,现在它只能作为一种尝试了,dynamic_cast用法,引申继续学习C++基础,O(∩_∩)O~,这样可以编译通过,但是效果没看到
gridLayout->addWidget(humiditLabel,2,0,1,1);
gridLayout->addWidget(humidityLEdit,2,1,1,1);
gridLayout->addWidget(humiditUpLabel,2,2,1,1);
gridLayout->addWidget(humidityUpLEdit,2,3,1,1);
gridLayout->addWidget(humiditDownLabel,2,4,1,1);
gridLayout->addWidget(humidityDownLEdit,2,5,1,1);
gridLayout->addWidget(lightLabel,3,0,1,1);
gridLayout->addWidget(lightLEdit,3,1,1,1);
gridLayout->addWidget(lightUpLabel,3,2,1,1);
gridLayout->addWidget(lightUpLEdit,3,3,1,1);
gridLayout->addWidget(lightDownLabel,3,4,1,1);
gridLayout->addWidget(lightDownLEdit,3,5,1,1);
gridLayout->addWidget(radioLabel,4,0,1,1);
gridLayout->addWidget(radioLEdit,4,1,1,1);
gridLayout->addWidget(radioUpLabel,4,2,1,1);
gridLayout->addWidget(radioUpLEdit,4,3,1,1);
gridLayout->addWidget(radioDownLabel,4,4,1,1);
gridLayout->addWidget(radioDownLEdit,4,5,1,1);
gridLayout->addWidget(co2Label,5,0,1,1);
gridLayout->addWidget(co2LEdit,5,1,1,1);
gridLayout->addWidget(co2UpLabel,5,2,1,1);
gridLayout->addWidget(co2UpLEdit,5,3,1,1);
gridLayout->addWidget(co2DownLabel,5,4,1,1);
gridLayout->addWidget(co2DownLEdit,5,5,1,1);
//gridLayout->addWidget(closeButton,2,1,1,1);
gridLayout->setContentsMargins(50,50,50,50);//设置布局内部四边的空隙
gridLayout->setAlignment(Qt::AlignHCenter);
gridLayout->setAlignment(Qt::AlignVCenter);
gridLayout->setColumnStretch(6,1);//设置列宽度,这里就实现了添加空白项的,自动调整居中效果,在第一列也可以设置同样的效果,看起来非常舒服
//gridLayout->setColumnStretch(0,3);
//gridLayout->setMargin(5);
gridLayout->setSpacing(25);//设置间距
this->setLayout(gridLayout);效果图: