• 6505阅读
  • 0回复

【摘录】《KDE2/Qt编程金典》第三章:在窗口中布置部件——3.13 位于其它部件之内的部件(垂直方向上) [复制链接]

上一主题 下一主题
离线XChinux
 

只看楼主 倒序阅读 楼主  发表于: 2005-09-09
  下面的例子和前一个非常相似,只不过方向不同。此程序是在列中组织安排按钮部件。顶层部件是一个水平方向的KContainerLayout部件,在其间填充了一组垂直方向的KContainerLayout部件。
  这个例子中的每一个列都是按照前一个例子中相应行的设置方式进行设置的。

Main

1 /* main.cpp */
2 #include <kapp.h>
3 #include “vertlayout.h”
4
5 int main(int argc,char **argv)
6 {
7 KApplication app(argc,argv,”vertlayout”);
8 VertLayout *vertlayout = new VertLayout();
9 vertlayout->show();
10 app.setMainWidget(vertlayout);
11 return(app.exec());
12 }

VertLayout Header

1 /* vertlayout.h */
2 #ifndef VERTLAYOUT_H
3 #define VERTLAYOUT_H
4
5 #include <qwidget.h>
6 #include <kcontainer.h>
7 #include <qpushbutton.h>
8
9 class VertLayout: public QWidget
10 {
11 public:
12 VertLayout(QWidget *parent=0,const char *name=0);
13 ~VertLayout();
14 private:
15 void add(KContainerLayout *layout,int count,
16 bool homogeneous,bool expand,bool fill);
17 };
18
19 #endif

VertLayout

1 /* vertlayout.cpp */
2 #include “vertlayout.h”
3 #include <qlabel.h>
4
5 VertLayout::VertLayout(QWidget *parent,const char *name)
6 : QWidget(parent,name)
7 {
8 KContainerLayout *layout = new KContainerLayout(this,
9 NULL,
10 KContainerLayout::Horizontal,
11 FALSE,
12 3,
13 0,
14 TRUE);
15
16 int count = 1;
17 add(layout,count++,FALSE,TRUE,TRUE);
18 add(layout,count++,TRUE,TRUE,TRUE);
19 add(layout,count++,FALSE,FALSE,TRUE);
20 add(layout,count++,TRUE,FALSE,TRUE);
21 add(layout,count++,FALSE,TRUE,FALSE);
22 add(layout,count++,TRUE,TRUE,FALSE);
23 add(layout,count++,FALSE,FALSE,FALSE);
24 add(layout,count++,TRUE,FALSE,FALSE);
25
26 layout->sizeToFit();
27 }
28 void VertLayout::add(KContainerLayout *outer,int count,
29 bool homogeneous,bool expand,bool fill)
30 {
31 QPushButton *button;
32
33 KContainerLayout *inner = new KContainerLayout(outer,
34 NULL,
35 KContainerLayout::Vertical,
36 homogeneous,
37 5,
38 0,
39 TRUE);
40
41 QString str(tr(“%1. “).arg(count));
42 QLabel *label = new QLabel(str,outer);
43 label->setMinimumSize(label->sizeHint());
44 label->setMaximumSize(label->sizeHint());
45 outer->packStart(label,FALSE,FALSE);
46
47 button = new QPushButton(inner);
48 button->setText(“Btn 1”);
49 button->setMinimumSize(button->sizeHint());
50 inner->packStart(button,expand,fill);
51
52 button = new QPushButton(inner);
53 button->setText(“Btn\n2”);
54 button->setMinimumSize(button->sizeHint());
55 inner->packStart(button,expand,fill);
56
57 button = new QPushButton(inner);
58 button->setText(“Btn 3”);
59 button->setMinimumSize(button->sizeHint());
60 inner->packStart(button,expand,fill);
61
62 inner->sizeToFit();
63 outer->packStart(inner,TRUE);
64 }
65 VertLayout::~VertLayout() { }


  VertLayout类和前面例子中的HorizLayout类非常相似,惟一不同的就是方向。在这个例子中,顶层窗口是水平KContainerLayout对象,填充有标签和垂直的KContainerLayout对象。起描述作用的标签减少到只有序号,这样可以节省空间。
  在第8行,创建了用做顶层窗口的KContainerLayout部件,设置为水平方向。第16行到第24行重复调用add()方法创建一组带有标签和一个垂直方向的KContainerLayout部件,并把它们加入到第一个参数所代表的KContainerLayout部件中(先加入标签)。第2个按钮,在第52行到第55行中创建,它的文本中包含了一个换行参数,这样第2个按钮的显示文本将显示为两行——这样使得第2个按钮比其它两个大,以非统一大小的部件为例,便于说明此程序是怎样控制位置和大小的改变的。
二笔 openSUSE Vim N9 BB10 XChinux@163.com 网易博客 腾讯微博
承接C++/Qt、Qt UI界面、PHP及预算报销系统开发业务
快速回复
限100 字节
 
上一个 下一个