Qt4--FormLayout

来源:互联网 发布:好玩的贴吧有哪些 知乎 编辑:程序博客网 时间:2024/05/16 04:05

FormLayout显然对于Form非常适合。

QFormLayout *formLay=new QFormLayout;    

QLineEdit *name=new QLineEdit;    

QLineEdit *email=new QLineEdit;    

QLineEdit *address=new QLineEdit;
formLay->addRow(tr("&Name:"),name);    

formLay->addRow(tr("&Email:"),email);    

formLay->addRow(tr("&Address:"),address);

setLayout(formLay);

仅需要这些代码就可以显示如下:

formLay->setLabelAlignment(Qt::AlignRight);

formLay->setRowWrapPolicy(QFormLayout::WrapAllRows);(默认的是QFormLayout::DontWrapAllRows)之后变为

另外注意到tr("&Name:"),加这个'&'是什么意思呢,实际上在为一个Label setBuddy时必须加&,在FormLayout中自动把LineEdit设为Label的Buddy了。如果使用其他Layout需要用下面代码显示指定Buddy:

QLineEdit *nameEd  = new QLineEdit(this); QLabel    *nameLb  = new QLabel("&Name:", this); nameLb->setBuddy(nameEd); QLineEdit *phoneEd = new QLineEdit(this); QLabel    *phoneLb = new QLabel("&Phone:", this); phoneLb->setBuddy(phoneEd);
设置Buddy有神马用呢?当你按Alt+A时Address对应的LineEdit自动获得Focus。
另外只有Label才可以有Buddy
Layout之间可以相互嵌套,Vlayout->addLayout(hLayout);
但是注意FormLayout没有addLayout,只有addChildLayout,并且addChildLayout的访问权限还是protected。

0 0
原创粉丝点击