您的位置:首页 > 编程语言 > Qt开发

Qt4--FormLayout

2015-07-10 09:10 447 查看

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。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: