qt中QHBoxLayout或QVBoxLayout布局内控件的动态生成与显示

---恢复内容开始---

#qt中QHBoxLayout或QVBoxLayout布局内控件的动态生成与显示 打个比方,我现在写个小例子,这个小例子是这样的,整个界面分为俩个部分,分为上半部分和下半部分,上半部分为5个按钮,点击5个按钮,下半部分分别会动态的出现不同的label显示内容。(显示内容为:2017到2018欧冠十六强队伍)效果如下图所示:效果如下图所示: ![点击“英超”按钮之后](https://upload-images.jianshu.io/upload_images/14361146-9f1c160e81843ae7.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) ![点击“西甲”按钮之后](https://upload-images.jianshu.io/upload_images/14361146-29fa03e674e605f7.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) ![点击“法甲”按钮之后](https://upload-images.jianshu.io/upload_images/14361146-390683d70ea5ecf4.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) ![点击“意甲”按钮之后](https://upload-images.jianshu.io/upload_images/14361146-32694abd99cc6476.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) ![点击“德甲”按钮之后](https://upload-images.jianshu.io/upload_images/14361146-e3c65840bc3318f5.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)  这个其实是一个很简单的例子,我写这个例子的最重要的目的是让大家来体会第二列的动态生成的layout,其中的控件是lable。 ## 1.重写你动态界面中需要存放的控件。  首先呢,最重要的一个实现就是对QLabel类进行一次封装,当你对这个类进行封装之后,这个QLabel类才可以重复的去new。 ``` #include "teamnamelabel.h"

TeamNameLabel::TeamNameLabel(QString name)
{
this->setText(name);
this->setFixedSize(70,20);
}

###头文件
PS:在头文件的定义中有一个坑需要注意一下,那就是**Q_OBJECT**。这处缺失的话在之后的**qobject_cast**转换的时候,编译的时候是会报错的。这个是其中比较大的一个坑。

ifndef TEAMNAMELABEL_H

define TEAMNAMELABEL_H

include

class TeamNameLabel : public QLabel
{
Q_OBJECT
public:
TeamNameLabel(QString name);
};

endif // TEAMNAMELABEL_H

##2.在主布局文件中相关的实现。
**先直接上代码再说吧!**

include "mainwindow.h"

include <windows.h>

MainWindow::MainWindow(QWidget *parent)
QMainWindow(parent)
{
this->setFixedSize(500,300);
this->InitUi();
s_PremierLeague<<"曼城";
QString pu = 0x6D66;
s_PremierLeague<<"利物"+ pu;
s_PremierLeague<<"热刺";
QString qie = 0x5207;
s_PremierLeague<<qie+"尔西";
s_PremierLeague<<"曼联";
QString de = 0x5FB7;
QString huang = 0x7687;
QString li = 0x91CC;
s_LaLiga<<"皇家马德" + li;
s_LaLiga<<"巴塞罗那";
s_LaLiga<<"塞维利亚";
s_Liguel<<"巴黎圣日耳曼";
s_SerieA<<"尤文图斯";
s_SerieA<<"罗马";
s_Bundesliga<<"拜仁";
}

MainWindow::~MainWindow()
{

}

void MainWindow::InitUi()
{
//注意setlayout在MainWindow中是不起作用的
centerWindow = new QWidget();
this->setCentralWidget(centerWindow);
v = new QVBoxLayout(this);
h_up = new QHBoxLayout(this);
h_down = new QHBoxLayout(this);
m_PremierLeague = new QPushButton(this);
m_PremierLeague->setText("英超");
connect(m_PremierLeague,SIGNAL(clicked(bool)),this,SLOT(clickPremierLeague()));
h_up->addWidget(m_PremierLeague);
m_LaLiga = new QPushButton("西甲",this);
m_Ligue1 = new QPushButton("法甲",this);
m_SerieA = new QPushButton("意甲",this);
m_Bundesliga = new QPushButton("德甲",this);
h_up->addWidget(m_LaLiga);
connect(m_LaLiga,SIGNAL(clicked(bool)),this,SLOT(clickLaLiga()));
h_up->addWidget(m_Ligue1);
connect(m_Ligue1,SIGNAL(clicked(bool)),this,SLOT(clickLigue1()));
h_up->addWidget(m_SerieA);
connect(m_SerieA,SIGNAL(clicked(bool)),this,SLOT(clickSerieA()));
h_up->addWidget(m_Bundesliga);
connect(m_Bundesliga,SIGNAL(clicked(bool)),this,SLOT(clickBundesliga()));
v->addLayout(h_up);
v->addLayout(h_down);
centerWindow->setLayout(v);
}

void MainWindow::DynamicLayout(QStringList name)
{
//判断此布局内的控件个数
if(h_down->count() > 0)
{
int count = h_down->count();
for(int i = count-1 ; i >= 0 ; i --)
{
QLayoutItem *it = h_down->layout()->takeAt(i);
TeamNameLabel oldHistory = qobject_cast<TeamNameLabel>(it->widget());
if(oldHistory != NULL)
delete oldHistory;
}
}
if(name.size() > 0)
{
for(int i = 0 ; i < name.size() ; i++)
{
TeamNameLabel *team = new TeamNameLabel(name[i]);
h_down->addWidget(team);
}
}
}

void MainWindow::clickPremierLeague()
{
DynamicLayout(s_PremierLeague);
}

void MainWindow::clickLaLiga()
{
DynamicLayout(s_LaLiga);
}

void MainWindow::clickLigue1()
{
DynamicLayout(s_Liguel);
}

void MainWindow::clickSerieA()
{
DynamicLayout(s_SerieA);
}

void MainWindow::clickBundesliga()
{
DynamicLayout(s_Bundesliga);
}

1. 大家可能会对“*QString pu = 0x6D66;s_PremierLeague<<"利物"+ pu;”*,这种类似的操作感到好奇,其实我这个是解决中文乱码问题的一个办法,如果各位如果有比这个还好的解决中文乱码的方法也可以留言展示一拨儿。
2. 其实实现QHBoxLayout布局内控件的动态显示的核心函数实现就是**DynamicLayout**。这个函数的核心的思路分为俩个部分,第一步就是先将原来布局内已经存在的控件先进行清空,第二步进行动态的控件生成。其中在清空布局内原有的控件时,有一个较大的坑,那就是:
    for(int i = count-1 ; i >= 0 ; i --)
    {
        QLayoutItem *it = h_down->layout()->takeAt(i);
        TeamNameLabel *oldHistory = qobject_cast<TeamNameLabel*>(it->widget());
        if(oldHistory != NULL)
            delete oldHistory;
    }
看到这里很多朋友可能还想不到这能有什么坑,事实上,刚开始的时候,我在写这个循环的时候,i的取值我是从0开始的,问题就出现在这块儿了,当你i从0开始取值的时候,在进行最后一次**qobject_cast**转换的时候就会报错。这块儿的话应该和内部实现有关系,没有时间深入进去看。各位想探究的朋友可以留言讨论。
3. 其实我在写这个小例子的时候还碰到了一个很无脑的错误,不过之前一直没有注意到,那就是:在MainWindow之下setLayout是不起作用的,断断续续写qt也写了挺长时间了,竟然没有发现这个错误。解决方法就是:
centerWindow = new QWidget();
this->setCentralWidget(centerWindow);
##项目源代码下载

[点击下载](https://download.csdn.net/download/qq130106486/10707414)

下载地址:https://download.csdn.net/download/qq130106486/10707414


<p>---恢复内容结束---</p>#qt中QHBoxLayout或QVBoxLayout布局内控件的动态生成与显示
打个比方,我现在写个小例子,这个小例子是这样的,整个界面分为俩个部分,分为上半部分和下半部分,上半部分为5个按钮,点击5个按钮,下半部分分别会动态的出现不同的label显示内容。(显示内容为:2017到2018欧冠十六强队伍)效果如下图所示:效果如下图所示:
![点击“英超”按钮之后](https://upload-images.jianshu.io/upload_images/14361146-9f1c160e81843ae7.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![点击“西甲”按钮之后](https://upload-images.jianshu.io/upload_images/14361146-29fa03e674e605f7.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![点击“法甲”按钮之后](https://upload-images.jianshu.io/upload_images/14361146-390683d70ea5ecf4.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![点击“意甲”按钮之后](https://upload-images.jianshu.io/upload_images/14361146-32694abd99cc6476.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![点击“德甲”按钮之后](https://upload-images.jianshu.io/upload_images/14361146-e3c65840bc3318f5.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
&emsp;这个其实是一个很简单的例子,我写这个例子的最重要的目的是让大家来体会第二列的动态生成的layout,其中的控件是lable。
## 1.重写你动态界面中需要存放的控件。
&emsp;首先呢,最重要的一个实现就是对QLabel类进行一次封装,当你对这个类进行封装之后,这个QLabel类才可以重复的去new。

include "teamnamelabel.h"

TeamNameLabel::TeamNameLabel(QString name)
{
this->setText(name);
this->setFixedSize(70,20);
}

###头文件
PS:在头文件的定义中有一个坑需要注意一下,那就是**Q_OBJECT**。这处缺失的话在之后的**qobject_cast**转换的时候,编译的时候是会报错的。这个是其中比较大的一个坑。

ifndef TEAMNAMELABEL_H

define TEAMNAMELABEL_H

include

class TeamNameLabel : public QLabel
{
Q_OBJECT
public:
TeamNameLabel(QString name);
};

endif // TEAMNAMELABEL_H

##2.在主布局文件中相关的实现。
**先直接上代码再说吧!**

include "mainwindow.h"

include <windows.h>

MainWindow::MainWindow(QWidget *parent)
QMainWindow(parent)
{
this->setFixedSize(500,300);
this->InitUi();
s_PremierLeague<<"曼城";
QString pu = 0x6D66;
s_PremierLeague<<"利物"+ pu;
s_PremierLeague<<"热刺";
QString qie = 0x5207;
s_PremierLeague<<qie+"尔西";
s_PremierLeague<<"曼联";
QString de = 0x5FB7;
QString huang = 0x7687;
QString li = 0x91CC;
s_LaLiga<<"皇家马德" + li;
s_LaLiga<<"巴塞罗那";
s_LaLiga<<"塞维利亚";
s_Liguel<<"巴黎圣日耳曼";
s_SerieA<<"尤文图斯";
s_SerieA<<"罗马";
s_Bundesliga<<"拜仁";
}

MainWindow::~MainWindow()
{

}

void MainWindow::InitUi()
{
//注意setlayout在MainWindow中是不起作用的
centerWindow = new QWidget();
this->setCentralWidget(centerWindow);
v = new QVBoxLayout(this);
h_up = new QHBoxLayout(this);
h_down = new QHBoxLayout(this);
m_PremierLeague = new QPushButton(this);
m_PremierLeague->setText("英超");
connect(m_PremierLeague,SIGNAL(clicked(bool)),this,SLOT(clickPremierLeague()));
h_up->addWidget(m_PremierLeague);
m_LaLiga = new QPushButton("西甲",this);
m_Ligue1 = new QPushButton("法甲",this);
m_SerieA = new QPushButton("意甲",this);
m_Bundesliga = new QPushButton("德甲",this);
h_up->addWidget(m_LaLiga);
connect(m_LaLiga,SIGNAL(clicked(bool)),this,SLOT(clickLaLiga()));
h_up->addWidget(m_Ligue1);
connect(m_Ligue1,SIGNAL(clicked(bool)),this,SLOT(clickLigue1()));
h_up->addWidget(m_SerieA);
connect(m_SerieA,SIGNAL(clicked(bool)),this,SLOT(clickSerieA()));
h_up->addWidget(m_Bundesliga);
connect(m_Bundesliga,SIGNAL(clicked(bool)),this,SLOT(clickBundesliga()));
v->addLayout(h_up);
v->addLayout(h_down);
centerWindow->setLayout(v);
}

void MainWindow::DynamicLayout(QStringList name)
{
//判断此布局内的控件个数
if(h_down->count() > 0)
{
int count = h_down->count();
for(int i = count-1 ; i >= 0 ; i --)
{
QLayoutItem *it = h_down->layout()->takeAt(i);
TeamNameLabel oldHistory = qobject_cast<TeamNameLabel>(it->widget());
if(oldHistory != NULL)
delete oldHistory;
}
}
if(name.size() > 0)
{
for(int i = 0 ; i < name.size() ; i++)
{
TeamNameLabel *team = new TeamNameLabel(name[i]);
h_down->addWidget(team);
}
}
}

void MainWindow::clickPremierLeague()
{
DynamicLayout(s_PremierLeague);
}

void MainWindow::clickLaLiga()
{
DynamicLayout(s_LaLiga);
}

void MainWindow::clickLigue1()
{
DynamicLayout(s_Liguel);
}

void MainWindow::clickSerieA()
{
DynamicLayout(s_SerieA);
}

void MainWindow::clickBundesliga()
{
DynamicLayout(s_Bundesliga);
}

1. 大家可能会对“*QString pu = 0x6D66;s_PremierLeague<<"利物"+ pu;”*,这种类似的操作感到好奇,其实我这个是解决中文乱码问题的一个办法,如果各位如果有比这个还好的解决中文乱码的方法也可以留言展示一拨儿。
2. 其实实现QHBoxLayout布局内控件的动态显示的核心函数实现就是**DynamicLayout**。这个函数的核心的思路分为俩个部分,第一步就是先将原来布局内已经存在的控件先进行清空,第二步进行动态的控件生成。其中在清空布局内原有的控件时,有一个较大的坑,那就是:
    for(int i = count-1 ; i >= 0 ; i --)
    {
        QLayoutItem *it = h_down->layout()->takeAt(i);
        TeamNameLabel *oldHistory = qobject_cast<TeamNameLabel*>(it->widget());
        if(oldHistory != NULL)
            delete oldHistory;
    }
看到这里很多朋友可能还想不到这能有什么坑,事实上,刚开始的时候,我在写这个循环的时候,i的取值我是从0开始的,问题就出现在这块儿了,当你i从0开始取值的时候,在进行最后一次**qobject_cast**转换的时候就会报错。这块儿的话应该和内部实现有关系,没有时间深入进去看。各位想探究的朋友可以留言讨论。
3. 其实我在写这个小例子的时候还碰到了一个很无脑的错误,不过之前一直没有注意到,那就是:在MainWindow之下setLayout是不起作用的,断断续续写qt也写了挺长时间了,竟然没有发现这个错误。解决方法就是:
centerWindow = new QWidget();
this->setCentralWidget(centerWindow);
##项目源代码下载

[点击下载](https://download.csdn.net/download/qq130106486/10707414)

下载地址:https://download.csdn.net/download/qq130106486/10707414
原文地址:https://www.cnblogs.com/qinzhikai/p/9758180.html