Qt布局管理器的类有4种,它们分别为QHBoxLayout、QVBoxLayout、QGridLayout和QStackLayout。其中,QHBoxLayout实现水平布局,QVBoxLayout实现竖直布局,QGridLayout实现表格布局,QStackLayout实现分组布局。通过对这几种布局的嵌套组合,就可以实现复杂的对话框设计。

先看一个实现组件竖直布局的例子。

 1 #include <QApplication>
 2 #include<QLabel>
 3 #include<QPushButton>
 4 #include<QHBoxLayout>
 5 #include<QSlider>
 6 #include<QSpinBox>
 7 
 8 int main(int argc, char *argv[])
 9 {
10     QApplication a(argc, argv);
11     QLabel*w=new QLabel("<h2><i>Hello</i><font color=red>  World!</font></h2>");
12     //QLabel*w=new QLabel("Hello World!");
13     QWidget *window=new QWidget;
14     window->setWindowTitle("Enter Your Age");
15 
16     QPushButton *button=new QPushButton("Quit");
17     QObject::connect(button, SIGNAL(clicked()), &a, SLOT(quit()) );
18 
19     QSpinBox *spinbox = new QSpinBox;
20     QSlider *slider = new QSlider(Qt :: Horizontal);//(Qt::Vertical); 滑动条的摆放方式选择,水平还是竖直
21     spinbox->setRange(0 , 130);
22     slider->setRange(0 , 130);
23 
24     QObject::connect(spinbox , SIGNAL(valueChanged(int)) , slider , SLOT( setValue(int)));
25     QObject::connect(slider , SIGNAL(valueChanged(int)) , spinbox , SLOT( setValue(int)));
26 
27     spinbox->setValue(35);
28 
29     //QHBoxLayout为水平方摆放接下来的控件,QVBoxLayout为竖直摆放接下来的控件
30     QVBoxLayout *layout = new QVBoxLayout;
31     layout->addWidget(w);
32     layout->addWidget(spinbox);//addWidget()虽定义自类QHBoxLayout,但属于QHBoxLayout继承于类QLayout的函数
33     layout->addWidget(slider);
34     layout->addWidget(button);
35 
36     window->setLayout(layout);
37     window->show();
38 
39     return a.exec();
40 }

该程序比较简单,主要实现了hello world的显示、Spinbox和Slider之间的相互赋值(Qt的信号槽机制)、竖直布局以及PushButton的简单使用,运行程序,显示如下:

现实中应用到的对话框不仅仅是单一的布局,而是牵涉到水平布局和竖直布局的综合使用,下面通过一段程序来理解如何实现水平布局和竖直布局的嵌套使用。

 1 #include <QApplication>
 2 #include<QLabel>
 3 #include<QPushButton>
 4 #include<QHBoxLayout>
 5 #include<QSlider>
 6 #include<QSpinBox>
 7 
 8 int main(int argc, char *argv[])
 9 {
10     QApplication a(argc, argv);
11     QLabel*w=new QLabel("<h2><i>Hello</i><font color=red>  World!</font></h2>");
12     //QLabel*w=new QLabel("Hello World!");
13     QWidget *window=new QWidget;
14     window->setWindowTitle("Enter Your Age");
15 
16     QPushButton *button=new QPushButton("Quit");
17     QObject::connect(button, SIGNAL(clicked()), &a, SLOT(quit()) );
18 
19     QSpinBox *spinbox = new QSpinBox;
20     QSlider *slider = new QSlider(Qt :: Horizontal);//(Qt::Vertical); 滑动条的摆放方式选择,水平还是竖直
21     spinbox->setRange(0 , 130);
22     slider->setRange(0 , 130);
23 
24     QObject::connect(spinbox , SIGNAL(valueChanged(int)) , slider , SLOT( setValue(int)));
25     QObject::connect(slider , SIGNAL(valueChanged(int)) , spinbox , SLOT( setValue(int)));
26 
27     spinbox->setValue(35);
28 
29     QHBoxLayout *belowleftlayout = new QHBoxLayout;
30     belowleftlayout->addWidget(spinbox);
31     belowleftlayout->addWidget(slider);
32 
33     QVBoxLayout *leftlayout = new QVBoxLayout;
34     leftlayout->addWidget(w);
35     leftlayout->addLayout(belowleftlayout); //注意这里用的是addLayout()函数
36 
37     QVBoxLayout *rightlayout = new QVBoxLayout;
38     rightlayout->addStretch();// 伸展器,用来占据它所在布局过程中的空间
39     rightlayout->addWidget(button);
40 
41     QHBoxLayout *layout = new QHBoxLayout;
42     layout->addLayout(leftlayout);
43     layout->addLayout(rightlayout);
44 
45     window->setLayout(layout);
46     window->show();
47 
48     return a.exec();
49 }

运行程序结果显示:

通过上图,我们实现了组件按照我们不同的要求进行的相应布局。由于在对右侧进行竖直布局时,添加伸展器Stretch,因此Quit按键位于右下角。

当注释掉rightlayout->addStretch();语句后,可以清楚的看到伸展器Stretch所起的作用。运行程序结果显示如下:

posted on 2013-09-27 20:27  心寒若冰  阅读(531)  评论(0编辑  收藏  举报